From 8e949ec548f458b690827fc05e6ee0ebeacaa2f8 Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Sun, 14 Mar 2021 14:35:11 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20add=20=E9=9B=86=E6=80=9D?= =?UTF-8?q?=E5=BD=95=E7=94=A8=E6=88=B7=E5=9B=9E=E5=A4=8D=20&=20=E4=B8=BB?= =?UTF-8?q?=E9=A2=98=20(#7168)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: add 集思录用户回复 & 主题 * add docs Co-authored-by: NeverBehave --- docs/bbs.md | 10 ++++++ lib/router.js | 4 +++ lib/routes/jisilu/reply.js | 64 +++++++++++++++++++++++++++++++++++ lib/routes/jisilu/topic.js | 69 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 147 insertions(+) create mode 100644 lib/routes/jisilu/reply.js create mode 100644 lib/routes/jisilu/topic.js diff --git a/docs/bbs.md b/docs/bbs.md index 64dffdbc0..de133d637 100644 --- a/docs/bbs.md +++ b/docs/bbs.md @@ -322,6 +322,16 @@ pageClass: routes +## 集思录 + +### 用户回复 + + + +### 用户主题 + + + ## 看雪 ### 论坛 diff --git a/lib/router.js b/lib/router.js index 262dd4a04..a76841cf2 100644 --- a/lib/router.js +++ b/lib/router.js @@ -4042,6 +4042,10 @@ router.get('/wainao-reads/all-articles', require('./routes/wainao/index')); // react router.get('/react/react-native-weekly', require('./routes/react/react-native-weekly')); +// 集思录 +router.get('/jisilu/reply/:user', require('./routes/jisilu/reply')); +router.get('/jisilu/topic/:user', require('./routes/jisilu/topic')); + // Constitutional Court of Baden-Württemberg (Germany) router.get('/verfghbw/press/:keyword?', require('./routes/verfghbw/press')); diff --git a/lib/routes/jisilu/reply.js b/lib/routes/jisilu/reply.js new file mode 100644 index 000000000..6b47bd57a --- /dev/null +++ b/lib/routes/jisilu/reply.js @@ -0,0 +1,64 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + const user = ctx.params.user; + + const rootUrl = 'https://www.jisilu.cn'; + const currentUrl = `${rootUrl}/people/${user}`; + const response = await got({ + method: 'get', + url: currentUrl, + }); + + const id = response.data.match(/var PEOPLE_USER_ID = '(.*)'/)[1]; + const name = response.data.match(/(.*) 的个人主页 - 集思录<\/title>/)[1]; + + const apiUrl = `${rootUrl}/people/ajax/user_actions/uid-${id}__actions-201__page-0`; + const apiResponse = await got({ + method: 'get', + url: apiUrl, + }); + + const $ = cheerio.load(apiResponse.data); + + const list = $('.aw-item') + .slice(0, 15) + .map((_, item) => { + item = $(item); + const a = item.find('.aw-hide-txt a'); + + return { + author: name, + title: a.text(), + link: a.attr('href'), + pubDate: new Date(item.find('.aw-text-color-999').text()).toUTCString(), + }; + }) + .get(); + + 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); + + content('.aw-dynamic-topic-more-operate').remove(); + + item.description = content('.aw-dynamic-topic-content').html(); + + return item; + }) + ) + ); + + ctx.state.data = { + title: `${name}的回复 - 集思录`, + link: currentUrl, + item: items, + }; +}; diff --git a/lib/routes/jisilu/topic.js b/lib/routes/jisilu/topic.js new file mode 100644 index 000000000..25cc1971a --- /dev/null +++ b/lib/routes/jisilu/topic.js @@ -0,0 +1,69 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + const user = ctx.params.user; + + const rootUrl = 'https://www.jisilu.cn'; + const currentUrl = `${rootUrl}/people/${user}`; + const response = await got({ + method: 'get', + url: currentUrl, + }); + + const id = response.data.match(/var PEOPLE_USER_ID = '(.*)'/)[1]; + const name = response.data.match(/<title>(.*) 的个人主页 - 集思录<\/title>/)[1]; + + const apiUrl = `${rootUrl}/people/ajax/user_actions/uid-${id}__actions-101__page-0`; + const apiResponse = await got({ + method: 'get', + url: apiUrl, + }); + + const $ = cheerio.load(apiResponse.data); + + const list = $('.aw-item') + .slice(0, 15) + .map((_, item) => { + item = $(item); + const a = item.find('.aw-hide-txt a'); + + return { + author: name, + title: a.text(), + link: a.attr('href'), + pubDate: new Date( + item + .find('.aw-text-color-999') + .text() + .match(/\d{4}-\d{2}-\d{2} \d{2}:\d{2}/)[0] + ).toUTCString(), + }; + }) + .get(); + + 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); + + content('.aw-dynamic-topic-more-operate').remove(); + + item.description = content('.aw-question-detail-txt').html(); + + return item; + }) + ) + ); + + ctx.state.data = { + title: `${name}的主题 - 集思录`, + link: currentUrl, + item: items, + }; +};