[new route] 获取cnbeta topic feed (#8015)

Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
tianchen zhong
2021-11-27 09:06:01 +00:00
committed by GitHub
co-authored by GitHub Action
parent 1501116b46
commit 265a6907cf
3 changed files with 80 additions and 0 deletions
+4
View File
@@ -168,6 +168,10 @@ pageClass: routes
<Route author="kt286 HaitianLiu" example="/cnbeta" path="/cnbeta"/>
### 主题
<Route author="cczhong11" example="/cnbeta/topic/453" path="/cnbeta/topic/:topic_id"/>
## Common App
### Blog
+1
View File
@@ -1823,6 +1823,7 @@ router.get('/maoyan/upcoming', lazyloadRouteHandler('./routes/maoyan/upcoming'))
// cnBeta
router.get('/cnbeta', lazyloadRouteHandler('./routes/cnbeta/home'));
router.get('/cnbeta/topic/:topic_id', lazyloadRouteHandler('./routes/cnbeta/topic'));
// 国家退伍士兵信息
router.get('/gov/veterans/:type', lazyloadRouteHandler('./routes/gov/veterans/china'));
+75
View File
@@ -0,0 +1,75 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const date = require('@/utils/date');
module.exports = async (ctx) => {
const topicUrl = `https://www.cnbeta.com/topics/${ctx.params.topic_id}.htm`;
const response = await got({
method: 'get',
url: topicUrl,
});
const $ = cheerio.load(response.data);
const topicInfo = $('div.cate-brief h2 b').text();
const news_list = $('div.item')
.map((i, item) => {
const title = $(item).find('dl dt a').text();
let href = $(item).find('dl dt a').attr('href');
if (href === undefined) {
return null;
}
if (href.startsWith('//')) {
href = 'https:' + href;
}
const status = $(item).find('div.meta-data ul.status li')['0'].children[0].data;
const info = status.split(' ');
const author = info[0];
const pubDate = info[1] + ' ' + info[2];
return { title, href, author, pubDate };
})
.filter((x) => x)
.map((i, e) => e)
.get();
const ProcessFeed = (data) => {
const $ = cheerio.load(data);
// 移除6.18广告
$('.article-global').remove();
$('.article-topic').remove();
// 提取内容
return $('.article-summary p').html() + '<br>' + $('.article-content').html();
};
const out = await Promise.all(
news_list.map(async (item) => {
const cache = await ctx.cache.get(item.href);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const response = await got({
method: 'get',
url: item.href,
});
const description = ProcessFeed(response.data);
const single = {
title: item.title,
description,
pubDate: date(item.pubDate, +8),
link: item.href,
author: item.author,
};
ctx.cache.set(item.href, JSON.stringify(single));
return Promise.resolve(single);
})
);
ctx.state.data = {
title: `cnBeta专题 - ${topicInfo}`,
link: topicUrl,
item: out ?? [],
description: `cnBeta专题 - ${topicInfo}`,
};
};