Merge pull request #6573 from zphw/feature/ap

feat: add top stories route for Associated Press News
This commit is contained in:
NeverBehave
2021-01-10 01:36:37 -05:00
committed by GitHub
4 changed files with 63 additions and 0 deletions
+4
View File
@@ -20,6 +20,10 @@ Site
## AP News
### Top Stories
<RouteEn author="zphw" example="/apnews" path="/apnews" />
### Topics
<RouteEn author="zoenglinghou" example="/apnews/topics/apf-topnews" path="/apnews/topics/:topic" :paramsDesc="['Topic name,can be found in URL. For example: the topic name of AP Top News [https://apnews.com/apf-topnews](https://apnews.com/apf-topnews) is `apf-topnews`']" radar="1" rssbud="1"/>
+4
View File
@@ -26,6 +26,10 @@ pageClass: routes
## AP News
### 首页头条
<Route author="zphw" example="/apnews" path="/apnews" />
### 话题
<Route author="zoenglinghou" example="/apnews/topics/apf-topnews" path="/apnews/topics/:topic" :paramsDesc="['话题名称,可在 URL 中找到,例如 AP Top News [https://apnews.com/apf-topnews](https://apnews.com/apf-topnews) 的话题为 `apf-topnews`']" radar="1" rssbud="1"/>
+1
View File
@@ -2491,6 +2491,7 @@ router.get('/gbcc/trust', require('./routes/gbcc/trust'));
// Associated Press
router.get('/apnews/topics/:topic', require('./routes/apnews/topics'));
router.get('/apnews', require('./routes/apnews/index'));
// CBC
router.get('/cbc/topics/:topic?', require('./routes/cbc/topics'));
+54
View File
@@ -0,0 +1,54 @@
const cheerio = require('cheerio');
const got = require('@/utils/got');
module.exports = async (ctx) => {
const response = await got.get('https://apnews.com/');
const $ = cheerio.load(response.data);
const list = [];
// main story component
const main = $('div[data-key=main-story]').first();
// headline news
const headline = {};
headline.title = $(main).find('a[data-key=card-headline] h1').first().text();
headline.link = 'https://apnews.com' + $(main).find('a[data-key=card-headline]').first().attr('href');
list.push(headline);
// related stories
$(main)
.find('a[data-key=related-story-link]')
.each(function (_, e) {
const item = {};
item.title = $(e).find('div[data-key=related-story-headline]').first().text();
item.link = 'https://apnews.com' + $(e).attr('href');
list.push(item);
});
const result = await Promise.all(
list.map(
async (item) =>
await ctx.cache.tryGet(item.link, async () => {
const content = await got.get(item.link);
const description = cheerio.load(content.data);
const metadata = JSON.parse(description('script[type="application/ld+json"]').html());
const featureImageURL = metadata.image;
item.description = `<img src="${featureImageURL}">`;
item.description += description('div[class=Article]')
.html()
.replace(/ADVERTISEMENT/g, '');
item.pubDate = new Date(metadata.datePublished).toISOString();
item.author = metadata.author[0];
return item;
})
)
);
ctx.state.data = {
title: 'Associated Press News',
link: 'https://apnews.com/',
item: result,
};
};