From b75fcee06461bae03aef8bccb5437e309a1075bb Mon Sep 17 00:00:00 2001 From: Fan Shen <1370275510@qq.com> Date: Sat, 11 May 2019 13:38:42 +0800 Subject: [PATCH] =?UTF-8?q?add=20=E8=A7=82=E5=AF=9F=E8=80=85=E9=A3=8E?= =?UTF-8?q?=E9=97=BB=E8=AF=9D=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/other.md | 4 +++ lib/router.js | 3 ++ lib/routes/guanchazhe/topic.js | 52 ++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 lib/routes/guanchazhe/topic.js diff --git a/docs/other.md b/docs/other.md index f4053b2d7c..32eca8c2e0 100644 --- a/docs/other.md +++ b/docs/other.md @@ -12,6 +12,10 @@ +## 观察者风闻话题 + + + ## 99% Invisible diff --git a/lib/router.js b/lib/router.js index f762681baa..4781e97f54 100755 --- a/lib/router.js +++ b/lib/router.js @@ -1347,4 +1347,7 @@ router.get('/paidai', require('./routes/paidai/index')); router.get('/paidai/bbs', require('./routes/paidai/bbs')); router.get('/paidai/news', require('./routes/paidai/news')); +// 观察者风闻话题 +router.get('/guancha/topic/:id', require('./routes/guancha/topic')); + module.exports = router; diff --git a/lib/routes/guanchazhe/topic.js b/lib/routes/guanchazhe/topic.js new file mode 100644 index 0000000000..bc9208a04f --- /dev/null +++ b/lib/routes/guanchazhe/topic.js @@ -0,0 +1,52 @@ +const cheerio = require('cheerio'); +const axios = require('../../utils/axios'); +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 res = await axios.get(absoluteUrl); + const $ = cheerio.load(res.data); + + const item = { + title: $('.article-content h1').text(), + author: $('.user-main h4') + .first() + .find('a') + .text(), + link: absoluteUrl, + description: $('.article-txt-content').html(), + pubDate: $('.user-main .time1').text(), + }; + const cache = await ctx.cache.get(absoluteUrl); + if (cache) { + return Promise.resolve(JSON.parse(cache)); + } + ctx.cache.set(absoluteUrl, JSON.stringify(item), 24 * 60 * 60); + + return Promise.resolve(item); + }) + ); + + ctx.state.data = { + title: `风闻话题-${title}`, + link: link, + item: out, + }; +};