From 3fb58e7ea894743caeeaa237307be4d1b67fcca7 Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Tue, 29 Dec 2020 01:08:35 +0800 Subject: [PATCH] feat: add tieba users' post contents (#6553) --- docs/bbs.md | 2 +- lib/routes/tieba/user.js | 45 ++++++++++++++++++---------------------- 2 files changed, 21 insertions(+), 26 deletions(-) diff --git a/docs/bbs.md b/docs/bbs.md index 392e28a72..0a557cbee 100644 --- a/docs/bbs.md +++ b/docs/bbs.md @@ -463,7 +463,7 @@ pageClass: routes ### 用户帖子 - + 用户 ID 可以通过打开用户的主页后查看地址栏的 `un` 字段来获取。 diff --git a/lib/routes/tieba/user.js b/lib/routes/tieba/user.js index 452e96aaf..95f7d3229 100644 --- a/lib/routes/tieba/user.js +++ b/lib/routes/tieba/user.js @@ -1,36 +1,31 @@ const got = require('@/utils/got'); -const cheerio = require('cheerio'); module.exports = async (ctx) => { const uid = ctx.params.uid; + + const rootUrl = 'https://tieba.baidu.com'; + const userUrl = `${rootUrl}/home/get/getthread?un=${uid}&pn=1&ie=utf8`; const response = await got({ method: 'get', - url: `https://tieba.baidu.com/home/main?un=${uid}`, + url: userUrl, }); - 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(), + title: `${uid}的贴子 - 百度贴吧`, + link: `${rootUrl}/home/main?un=${uid}`, + item: response.data.data.thread_list.map((item) => { + let media = ''; + if (item.media) { + for (const m of item.media) { + media += ``; + } + } + return { + title: item.title, + description: `

${item.content}

${media}`, + pubDate: new Date(item.create_time * 1000).toUTCString(), + link: `https://tieba.baidu.com/p/${item.post_id}?pid=${item.thread_id}&cid=#${item.thread_id}`, + }; + }), }; };