fix: 观察者网风闻话题 (#6074)

This commit is contained in:
Ethan Shen
2020-11-01 15:48:21 +00:00
committed by GitHub
parent 7556d40994
commit e0690e8a54
3 changed files with 59 additions and 44 deletions
+14 -2
View File
@@ -761,9 +761,21 @@ others = 热点新闻 + 滚动新闻
</Route>
### 观察者风闻话题
### 风闻话题
<Route author="occupy5" example="/guancha/topic/113" path="/guancha/topic/:id" :paramsDesc="['话题id, 可在URL中找到']" />
<Route author="occupy5 nczitzk" example="/guancha/topic/110/1" path="/guancha/topic/:id?/:order?" :paramsDesc="['话题 id,可在URL中找到,默认为全部,即为 `0`', '排序参数,见下表']">
| 最新回复 | 最新发布 | 24 小时最热 | 3 天最热 | 7 天最热 | 3 个月最热 | 专栏文章 |
| -------- | -------- | ----------- | -------- | -------- | ---------- | -------- |
| 1 | 2 | 3 | 6 | 7 | 8 | 5 |
::: tip 提示
仅在话题 id 为 0,即选择 全部 时,**3 个月最热**、**24 小时最热**、**3 天最热**、**7 天最热** 和 **专栏文章** 参数生效。
:::
</Route>
### 个人主页文章
+2 -2
View File
@@ -1693,11 +1693,11 @@ router.get('/zfrontier/postlist/:type', require('./routes/zfrontier/postlist'));
router.get('/zfrontier/board/:boardId', require('./routes/zfrontier/board_postlist'));
// 观察者网
router.get('/guancha/topic/:id', require('./routes/guancha/topic'));
router.get('/guancha/topic/:id/:order?', require('./routes/guancha/topic'));
router.get('/guancha/personalpage/:uid', require('./routes/guancha/personalpage'));
router.get('/guancha/:caty?', require('./routes/guancha/index'));
router.get('/guanchazhe/topic/:id', require('./routes/guancha/topic'));
router.get('/guanchazhe/topic/:id/:order?', require('./routes/guancha/topic'));
router.get('/guanchazhe/personalpage/:uid', require('./routes/guancha/personalpage'));
router.get('/guanchazhe/index/:caty?', require('./routes/guancha/index'));
+43 -40
View File
@@ -1,53 +1,56 @@
const cheerio = require('cheerio');
const got = require('@/utils/got');
const cheerio = require('cheerio');
const date = require('@/utils/date');
const url = require('url');
const host = 'https://user.guancha.cn';
module.exports = async (ctx) => {
const id = ctx.params.id;
const link = `https://user.guancha.cn/topic/attention-topic?follow-topic-id=${id}`;
const res = await got.get(encodeURI(link));
const $ = cheerio.load(res.data);
// 话题名称
const title = $('.topic-main').text();
// 文章列表
const list = $('.article-list .list-item h4')
.find('a')
.map((i, e) => $(e).attr('href'))
const id = ctx.params.id || '0';
const order = ctx.params.order || '1';
const rootUrl = 'https://user.guancha.cn';
const currentUrl = `${rootUrl}/${id === '0' ? 'main/index-list.json?' : 'topic/post-list?topic_id=' + id}&page=1&order=${order}`;
const response = await got({
method: 'get',
url: currentUrl,
});
const $ = cheerio.load(response.data);
const list = $('ul.home li .list-item h4 a')
.slice(0, 10)
.map((_, item) => {
item = $(item);
return {
title: item.text(),
link: `${rootUrl}${item.attr('href')}`,
};
})
.get();
const out = await Promise.all(
list.map(async (itemUrl) => {
// 将相对链接替换为绝对链接
const absoluteUrl = url.resolve(host, itemUrl);
const cache = await ctx.cache.get(absoluteUrl);
// 判断缓存
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const res = await got.get(absoluteUrl);
const $ = cheerio.load(res.data);
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);
const dateStr = $('.user-main .time1').text();
const item = {
title: $('.article-content h1').text(),
author: $('.user-main h4').first().find('a').text(),
link: absoluteUrl,
description: $('.article-txt-content').html(),
// 格式化日期
pubDate: date(dateStr),
};
ctx.cache.set(absoluteUrl, JSON.stringify(item));
item.pubDate = date(content('.time1').text(), 8);
item.author = content('.user-main h4 a').eq(0).text();
item.description = content('.article-txt-content').html();
return Promise.resolve(item);
})
return item;
})
)
);
$('h1.title span').remove();
ctx.state.data = {
title: `风闻话题-${title}`,
link: link,
item: out,
title: `观察者网 - ${id === '0' ? '风闻' : $('h1.title').text()}`,
link: currentUrl,
item: items,
};
};