feat: add 观察者网头条 (#6152)

This commit is contained in:
Ethan Shen 2020-11-13 01:45:06 +08:00 committed by GitHub
parent abc86f6efb
commit 23d2824509
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 0 deletions

View File

@ -777,6 +777,10 @@ area 分区选项
## 观察者网
### 头条
<Route author="nczitzk" example="/guancha/headline" path="/guancha/headline" />
### 首页
<Route author="nczitzk Jeason0228" example="/guancha" path="/guancha/:caty?" :paramsDesc="['分类,见下表,默认为全部']">

View File

@ -1700,6 +1700,7 @@ router.get('/zfrontier/postlist/:type', require('./routes/zfrontier/postlist'));
router.get('/zfrontier/board/:boardId', require('./routes/zfrontier/board_postlist'));
// 观察者网
router.get('/guancha/headline', require('./routes/guancha/headline'));
router.get('/guancha/topic/:id/:order?', require('./routes/guancha/topic'));
router.get('/guancha/member/:caty?', require('./routes/guancha/member'));
router.get('/guancha/personalpage/:uid', require('./routes/guancha/personalpage'));

View File

@ -0,0 +1,49 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const rootUrl = 'https://www.guancha.cn';
const currentUrl = `${rootUrl}/GuanChaZheTouTiao/list_1.shtml`;
const response = await got({
method: 'get',
url: currentUrl,
});
const $ = cheerio.load(response.data);
const list = $('.headline-list li .content-headline h3 a')
.map((_, item) => {
item = $(item);
return {
title: item.text(),
link: `${rootUrl}${item.attr('href')}`,
description: item.parent().next().html(),
pubDate: new Date(item.parents('div').eq(0).find('span').text()).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('.all-txt').html();
return item;
})
)
);
ctx.state.data = {
title: '观察者网 - 头条',
link: currentUrl,
item: items,
};
};