feat: add CGTN most read & most share (#5672)

Co-authored-by: Henry Wang <hi@henry.wang>
This commit is contained in:
Henry Wang 2020-09-20 21:17:04 +01:00 committed by GitHub
commit dc4e8d5e2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 64 additions and 0 deletions

View File

@ -47,6 +47,12 @@ Compared to the official one, the RSS feed generated by RSSHub not only has more
<RouteEn author="kt286" example="/cfan/news" path="/cfan/news"/>
## CGTN
### Most Read & Most Share
<RouteEn author="nczitzk" example="/cgtn/most/read/day" path="/cgtn/most/:type?/:time?" :paramsDesc="['Type, `read` as most read, `share` as most share, `read` by default', 'Time range, `all` as all the time, `day` as today, `week` as this week, `month` as this month, `year` as this year, `all` by default']"/>
## Engadget
### Chinese

View File

@ -102,6 +102,12 @@ pageClass: routes
<Route author="kt286" example="/cfan/news" path="/cfan/news"/>
## CGTN
### Most Read & Most Share
<Route author="nczitzk" example="/cgtn/most/read/day" path="/cgtn/most/:type?/:time?" :paramsDesc="['类型,`read` 指最多阅读,`share` 指最多分享,默认为 `read`', '时间,`all` 指所有时间,`day` 指今天,`week` 指本周,`month` 指本月,`year` 指今年,默认为 `all`']"/>
## cnBeta
### 最新

View File

@ -3213,6 +3213,9 @@ 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'));
// AppSales
router.get('/appsales/:caty?/:time?', require('./routes/appsales/index'));

49
lib/routes/cgtn/most.js Normal file
View File

@ -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,
};
};