From 0f4efa8a951d3b4fde83c7f1ff3329d1da63f863 Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Sat, 17 Jul 2021 17:27:56 +0800 Subject: [PATCH] =?UTF-8?q?fix(route):=20=E8=B4=B4=E5=90=A7=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E5=B8=96=E5=AD=90=20(#7678)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/routes/tieba/user.js | 45 ++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/lib/routes/tieba/user.js b/lib/routes/tieba/user.js index bbe5c5451..1913c2e15 100644 --- a/lib/routes/tieba/user.js +++ b/lib/routes/tieba/user.js @@ -1,31 +1,36 @@ 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: userUrl, + 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: `${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.thread_id}?pid=${item.post_id}&cid=#${item.post_id}`, - }; - }), + 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: Date.parse(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(), }; };