feat: add 万维读者 (#5123)

This commit is contained in:
Ethan Shen 2020-07-07 17:53:51 +08:00 committed by GitHub
parent 8c9d9d473e
commit 8bee770594
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 0 deletions

View File

@ -337,6 +337,12 @@ pageClass: routes
<Route author="u3u" example="/tieba/post/lz/5853240586" path="/tieba/post/lz/:id" :paramsDesc="['帖子 ID']"/>
## 万维读者
### 焦点新闻
<Route author="nczitzk" example="/creaders/headline" path="/creaders/headline"/>
## 小米社区
### 圈子

View File

@ -2932,6 +2932,9 @@ router.get('/gelonghui/keyword/:keyword', require('./routes/gelonghui/keyword'))
// 光谷社区
router.get('/guanggoo/:caty', require('./routes/guanggoo/index'));
// 万维读者
router.get('/creaders/headline', require('./routes/creaders/headline'));
// 金山词霸
router.get('/iciba/:days?/:img_type?', require('./routes/iciba/index'));

View File

@ -0,0 +1,46 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const iconv = require('iconv-lite');
module.exports = async (ctx) => {
const currentUrl = 'http://news.creaders.net/headline/';
const response = await got({
method: 'get',
url: currentUrl,
});
const $ = cheerio.load(iconv.decode(response.data, 'gbk'));
const list = $('ul.newslist li')
.slice(0, 10)
.map((_, item) => {
item = $(item);
const a = item.find('a').eq(1);
return {
title: a.text(),
link: a.attr('href'),
pubDate: new Date(item.find('time').text() + ' GMT+8').toUTCString(),
};
})
.get();
const items = await Promise.all(
list.map(
async (item) =>
await ctx.cache.tryGet(item.link, async () => {
const res = await got({
method: 'get',
url: item.link,
});
const content = cheerio.load(iconv.decode(res.data, 'gbk'));
item.description = content('#newsContent').html();
return item;
})
)
);
ctx.state.data = {
title: '万维读者 - 焦点新闻',
link: currentUrl,
item: items,
};
};