From e2e7648c0e3d25da04b35bdd6d11ac55ca160ae2 Mon Sep 17 00:00:00 2001 From: Humphrey Lin Date: Mon, 2 Nov 2020 19:22:24 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20add=20=E8=B4=B4=E5=90=A7=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E5=8F=91=E5=B8=96=20(#6072)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/bbs.md | 6 ++++++ lib/router.js | 1 + lib/routes/tieba/user.js | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 lib/routes/tieba/user.js diff --git a/docs/bbs.md b/docs/bbs.md index d8aa8b50e3..a9a0e3cad5 100644 --- a/docs/bbs.md +++ b/docs/bbs.md @@ -439,6 +439,12 @@ pageClass: routes +### 用户帖子 + + + +用户 ID 可以通过打开用户的主页后查看地址栏的 `un` 字段来获取。 + ## 万维读者 ### 焦点新闻 diff --git a/lib/router.js b/lib/router.js index 7fdb3c0f18..8d0dd9de59 100644 --- a/lib/router.js +++ b/lib/router.js @@ -113,6 +113,7 @@ router.get('/tieba/forum/:kw', require('./routes/tieba/forum')); router.get('/tieba/forum/good/:kw/:cid?', require('./routes/tieba/forum')); router.get('/tieba/post/:id', require('./routes/tieba/post')); router.get('/tieba/post/lz/:id', require('./routes/tieba/post')); +router.get('/tieba/user/:uid', require('./routes/tieba/user')); // 网易云音乐 router.get('/ncm/playlist/:id', require('./routes/ncm/playlist')); diff --git a/lib/routes/tieba/user.js b/lib/routes/tieba/user.js new file mode 100644 index 0000000000..452e96aaf5 --- /dev/null +++ b/lib/routes/tieba/user.js @@ -0,0 +1,36 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + const uid = ctx.params.uid; + const response = await got({ + method: 'get', + url: `https://tieba.baidu.com/home/main?un=${uid}`, + }); + + const data = response.data; + + const $ = cheerio.load(data); + const name = $('span.userinfo_username').text(); + const list = $('div.n_right.clearfix'); + let imgurl; + + ctx.state.data = { + title: `${name} 的贴吧`, + link: `https://tieba.baidu.com/home/main?un=${uid}`, + item: + list && + list + .map((index, item) => { + item = $(item).find('.n_contain'); + imgurl = item.find('ul.n_media.clearfix img').attr('original'); + return { + title: item.find('div.thread_name a').attr('title'), + pubDate: item.parent().find('div .n_post_time').text(), + description: `${item.find('div.n_txt').text()}
`, + link: item.find('div.thread_name a').attr('href'), + }; + }) + .get(), + }; +};