diff --git a/docs/new-media.md b/docs/new-media.md index d8c7234158..a42c29f9a6 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -761,9 +761,21 @@ others = 热点新闻 + 滚动新闻 -### 观察者风闻话题 +### 风闻话题 - + + +| 最新回复 | 最新发布 | 24 小时最热 | 3 天最热 | 7 天最热 | 3 个月最热 | 专栏文章 | +| -------- | -------- | ----------- | -------- | -------- | ---------- | -------- | +| 1 | 2 | 3 | 6 | 7 | 8 | 5 | + +::: tip 提示 + +仅在话题 id 为 0,即选择 全部 时,**3 个月最热**、**24 小时最热**、**3 天最热**、**7 天最热** 和 **专栏文章** 参数生效。 + +::: + + ### 个人主页文章 diff --git a/lib/router.js b/lib/router.js index 8c6ef2c56b..db2f74dc8d 100644 --- a/lib/router.js +++ b/lib/router.js @@ -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')); diff --git a/lib/routes/guancha/topic.js b/lib/routes/guancha/topic.js index 94924cdb28..26e7f9a009 100644 --- a/lib/routes/guancha/topic.js +++ b/lib/routes/guancha/topic.js @@ -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, }; };