From b823e5f9e84b75c7538412d12d12032f5d8e96be Mon Sep 17 00:00:00 2001 From: zph Date: Wed, 30 Dec 2020 16:04:10 +0800 Subject: [PATCH] Added top stories route for AP --- docs/en/traditional-media.md | 4 +++ docs/traditional-media.md | 4 +++ lib/router.js | 1 + lib/routes/apnews/index.js | 54 ++++++++++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+) create mode 100644 lib/routes/apnews/index.js diff --git a/docs/en/traditional-media.md b/docs/en/traditional-media.md index 983211fe80..d28621dfd2 100644 --- a/docs/en/traditional-media.md +++ b/docs/en/traditional-media.md @@ -20,6 +20,10 @@ Site ## AP News +### Top Stories + + + ### Topics diff --git a/docs/traditional-media.md b/docs/traditional-media.md index 94db156aeb..a574998f57 100644 --- a/docs/traditional-media.md +++ b/docs/traditional-media.md @@ -26,6 +26,10 @@ pageClass: routes ## AP News +### 首页头条 + + + ### 话题 diff --git a/lib/router.js b/lib/router.js index 5bb0f656c3..7e1e6838ad 100644 --- a/lib/router.js +++ b/lib/router.js @@ -2490,6 +2490,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')); diff --git a/lib/routes/apnews/index.js b/lib/routes/apnews/index.js new file mode 100644 index 0000000000..a59c24f1de --- /dev/null +++ b/lib/routes/apnews/index.js @@ -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 = ``; + 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, + }; +};