From 763a9e869ba5f395c7f3869e24e2ba123013f9a6 Mon Sep 17 00:00:00 2001 From: nczitzk Date: Sat, 2 Jan 2021 21:52:58 +0800 Subject: [PATCH] feat: add cgtn editors'pick --- docs/en/new-media.md | 4 ++++ docs/new-media.md | 4 ++++ lib/router.js | 1 + lib/routes/cgtn/pick.js | 46 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+) create mode 100644 lib/routes/cgtn/pick.js diff --git a/docs/en/new-media.md b/docs/en/new-media.md index 070815cbe9..d97bf28812 100644 --- a/docs/en/new-media.md +++ b/docs/en/new-media.md @@ -75,6 +75,10 @@ Compared to the official one, the RSS feed generated by RSSHub not only has more +### Editors' Pick + + + ## China Labour Bulletin ### Commentary and Analysis diff --git a/docs/new-media.md b/docs/new-media.md index dea896d573..e29b17b178 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -130,6 +130,10 @@ pageClass: routes +### Editors' Pick + + + ## cnBeta ### 最新 diff --git a/lib/router.js b/lib/router.js index 5bb0f656c3..856d53726d 100644 --- a/lib/router.js +++ b/lib/router.js @@ -3338,6 +3338,7 @@ router.get('/fulinian/:caty?', require('./routes/fulinian/index')); // CGTN router.get('/cgtn/most/:type?/:time?', require('./routes/cgtn/most')); +router.get('/cgtn/pick', require('./routes/cgtn/pick')); // AppSales router.get('/appsales/:caty?/:time?', require('./routes/appsales/index')); diff --git a/lib/routes/cgtn/pick.js b/lib/routes/cgtn/pick.js new file mode 100644 index 0000000000..ea4dce2376 --- /dev/null +++ b/lib/routes/cgtn/pick.js @@ -0,0 +1,46 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + const rootUrl = 'https://www.cgtn.com/'; + const response = await got({ + method: 'get', + url: rootUrl, + }); + + const $ = cheerio.load(response.data); + const list = $('.titleArea') + .map((_, item) => { + item = $(item); + const a = item.find('a'); + return { + title: a.text(), + link: a.attr('href'), + pubDate: new Date(parseInt(a.attr('data-time'))).toUTCString(), + }; + }) + .get(); + + const items = await Promise.all( + list.map( + async (item) => + await ctx.cache.tryGet(item.link, async () => { + const detailResponse = await got({ + method: 'get', + url: item.link, + }); + const content = cheerio.load(detailResponse.data); + + item.description = content('#cmsMainContent').html(); + + return item; + }) + ) + ); + + ctx.state.data = { + title: "CGTN - Editors' Pick", + link: rootUrl, + item: items, + }; +};