feat(route): add 新华社新闻 (#7183)

Co-authored-by: NeverBehave <gayhub@never.pet>
This commit is contained in:
Ethan Shen
2021-03-26 17:52:39 -04:00
committed by GitHub
co-authored by NeverBehave
parent 090a595cf5
commit 1edc3b7076
3 changed files with 61 additions and 0 deletions
+6
View File
@@ -2162,6 +2162,12 @@ column 为 third 时可选的 category:
</Route>
## 新华网
### 新华社新闻
<Route author="nczitzk" example="/news/whxw" path="/news/whxw"/>
## 新浪专栏
### 创事记
+3
View File
@@ -3941,6 +3941,9 @@ router.get('/tianyancha/hot', require('./routes/tianyancha/hot'));
// King Arthur
router.get('/kingarthur/:type', require('./routes/kingarthur/index'));
// 新华网
router.get('/news/whxw', require('./routes/news/whxw'));
// 游讯网
router.get('/yxdown/recommend', require('./routes/yxdown/recommend'));
+52
View File
@@ -0,0 +1,52 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const rootUrl = 'http://www.news.cn';
const currentUrl = `${rootUrl}/whxw.htm`;
const response = await got({
method: 'get',
url: currentUrl,
});
const $ = cheerio.load(response.data);
const list = $('h3 a')
.slice(0, 15)
.map((_, item) => {
item = $(item);
return {
title: item.text(),
link: item.attr('href'),
};
})
.get();
const items = await Promise.all(
list.map(
async (item) =>
await ctx.cache.tryGet(item.link, async () => {
try {
const detailResponse = await got({
method: 'get',
url: item.link,
});
const content = cheerio.load(detailResponse.data);
item.description = content('#detail').html();
item.pubDate = new Date(detailResponse.data.match(/(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})/)[1] + ' GMT+8').toUTCString();
return item;
} catch (e) {
return Promise.resolve('');
}
})
)
);
ctx.state.data = {
title: $('title').text(),
link: currentUrl,
item: items,
};
};