From 741d0351210334a04ea43b0c0e1d4eb0fad6dc7f Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Tue, 29 Dec 2020 01:23:56 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20add=20=E7=8C=BF=E6=96=99=E6=A0=87?= =?UTF-8?q?=E7=AD=BE=20(#6557)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/program-update.md | 20 +++++++++++++++++ lib/router.js | 3 +++ lib/routes/yuanliao/index.js | 43 ++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 lib/routes/yuanliao/index.js diff --git a/docs/program-update.md b/docs/program-update.md index 1d9a236cde..5cddf1086a 100644 --- a/docs/program-update.md +++ b/docs/program-update.md @@ -413,3 +413,23 @@ pageClass: routes ### 金米奖 + +## 猿料 + +### 标签 + + + +标签 + +| uTools | 插件发布 | +| ------ | -------- | +| utools | plugins | + +排序 + +| 最新回复 | 热门回复 | 新鲜出炉 | 陈年旧贴 | +| -------- | ------------- | ---------- | --------- | +| | -commentCount | -createdAt | createdAt | + + diff --git a/lib/router.js b/lib/router.js index 1612e4b4b3..66d6984b4a 100644 --- a/lib/router.js +++ b/lib/router.js @@ -3767,6 +3767,9 @@ router.get('/sciencenet/blog/:type?/:time?/:sort?', require('./routes/sciencenet // DailyArt router.get('/dailyart/:language?', require('./routes/dailyart/index')); +// 猿料 +router.get('/yuanliao/:tag?/:sort?', require('./routes/yuanliao/index')); + // 中国政协网 router.get('/cppcc/:slug?', require('./routes/gov/cppcc/index')); diff --git a/lib/routes/yuanliao/index.js b/lib/routes/yuanliao/index.js new file mode 100644 index 0000000000..d9333428fa --- /dev/null +++ b/lib/routes/yuanliao/index.js @@ -0,0 +1,43 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + const tag = ctx.params.tag || 'utools'; + const sort = ctx.params.sort || ''; + + const rootUrl = 'https://yuanliao.info'; + const currentUrl = `${rootUrl}/api/discussions?tags=${tag}&sort=${sort}`; + const response = await got({ + method: 'get', + url: currentUrl, + }); + + const list = response.data.data.map((item) => ({ + title: item.attributes.title, + link: `${rootUrl}/d/${item.id}-${item.attributes.slug}`, + pubDate: new Date(item.attributes.lastPostedAt).toUTCString(), + })); + + 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); + + item.description = content('#flarum-content').html(); + + return item; + }) + ) + ); + + ctx.state.data = { + title: `${tag} - 猿料`, + link: currentUrl, + item: items, + }; +};