From 2eb30f7aa7821549be91c00924fe7711b2aa2ddd Mon Sep 17 00:00:00 2001 From: nczitzk Date: Wed, 16 Sep 2020 18:03:40 +0800 Subject: [PATCH] feat: add CGTN most read & most share --- docs/en/new-media.md | 6 +++++ docs/new-media.md | 6 +++++ lib/router.js | 3 +++ lib/routes/cgtn/most.js | 49 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+) create mode 100644 lib/routes/cgtn/most.js diff --git a/docs/en/new-media.md b/docs/en/new-media.md index bce314739..c2b1fc6a8 100644 --- a/docs/en/new-media.md +++ b/docs/en/new-media.md @@ -47,6 +47,12 @@ Compared to the official one, the RSS feed generated by RSSHub not only has more +## CGTN + +### Most Read & Most Share + + + ## Engadget ### Chinese diff --git a/docs/new-media.md b/docs/new-media.md index eca437d3a..810f39eeb 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -102,6 +102,12 @@ pageClass: routes +## CGTN + +### Most Read & Most Share + + + ## cnBeta ### 最新 diff --git a/lib/router.js b/lib/router.js index cd9a06d2e..f6e74d18a 100644 --- a/lib/router.js +++ b/lib/router.js @@ -3206,4 +3206,7 @@ router.get('/feixuew/:id?', require('./routes/feixuew/index')); // 1X router.get('/1x/:type?/:caty?', require('./routes/1x/index')); +// CGTN +router.get('/cgtn/most/:type?/:time?', require('./routes/cgtn/most')); + module.exports = router; diff --git a/lib/routes/cgtn/most.js b/lib/routes/cgtn/most.js new file mode 100644 index 000000000..f2b3828d6 --- /dev/null +++ b/lib/routes/cgtn/most.js @@ -0,0 +1,49 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + ctx.params.type = ctx.params.type || 'read'; + ctx.params.time = ctx.params.time || 'all'; + + const rootUrl = `https://www.cgtn.com/most-${ctx.params.type}`; + const response = await got({ + method: 'get', + url: rootUrl, + }); + + const $ = cheerio.load(response.data); + const list = $(`#${ctx.params.time}Items div.most-read-item-box div.cg-title`) + .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 - Most ${ctx.params.type}`, + link: rootUrl, + item: items, + }; +};