diff --git a/docs/other.md b/docs/other.md index 71e26439ff..872eee0466 100644 --- a/docs/other.md +++ b/docs/other.md @@ -290,6 +290,10 @@ type 为 all 时,category 参数不支持 cost 和 free +## 观察者风闻话题 + + + ## 果壳网 diff --git a/lib/router.js b/lib/router.js index 694e280d99..4933a71cc4 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1354,4 +1354,7 @@ router.get('/boc/whpj', require('./routes/boc/whpj')); // 漫画db router.get('/manhuadb/comics/:id', require('./routes/manhuadb/comics')); +// 观察者风闻话题 +router.get('/guanchazhe/topic/:id', require('./routes/guanchazhe/topic')); + module.exports = router; diff --git a/lib/routes/guanchazhe/topic.js b/lib/routes/guanchazhe/topic.js new file mode 100644 index 0000000000..539c3583d3 --- /dev/null +++ b/lib/routes/guanchazhe/topic.js @@ -0,0 +1,56 @@ +const cheerio = require('cheerio'); +const axios = require('../../utils/axios'); +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 axios.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')) + .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 axios.get(absoluteUrl); + const $ = cheerio.load(res.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), 24 * 60 * 60); + + return Promise.resolve(item); + }) + ); + + ctx.state.data = { + title: `风闻话题-${title}`, + link: link, + item: out, + }; +};