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 1/3] =?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, + }; +}; From f82fb61bef22ce5816340d7e719323d53ff21703 Mon Sep 17 00:00:00 2001 From: Fan Shen <1370275510@qq.com> Date: Sat, 11 May 2019 13:59:46 +0800 Subject: [PATCH 2/3] modify file name --- lib/router.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/router.js b/lib/router.js index 4781e97f54..704f25036d 100755 --- a/lib/router.js +++ b/lib/router.js @@ -1348,6 +1348,6 @@ 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')); +router.get('/guanchazhe/topic/:id', require('./routes/guanchazhe/topic')); module.exports = router; From be94b584834951e5717ed00e36d1ec525f0f790d Mon Sep 17 00:00:00 2001 From: Fan Shen <1370275510@qq.com> Date: Sun, 12 May 2019 16:09:51 +0800 Subject: [PATCH 3/3] fix date and cache --- lib/routes/guanchazhe/topic.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/routes/guanchazhe/topic.js b/lib/routes/guanchazhe/topic.js index bc9208a04f..539c3583d3 100644 --- a/lib/routes/guanchazhe/topic.js +++ b/lib/routes/guanchazhe/topic.js @@ -1,5 +1,6 @@ const cheerio = require('cheerio'); const axios = require('../../utils/axios'); +const date = require('../../utils/date'); const url = require('url'); const host = 'https://user.guancha.cn'; @@ -21,9 +22,15 @@ module.exports = async (ctx) => { 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') @@ -32,12 +39,9 @@ module.exports = async (ctx) => { .text(), link: absoluteUrl, description: $('.article-txt-content').html(), - pubDate: $('.user-main .time1').text(), + // 格式化日期 + pubDate: date(dateStr), }; - 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);