From 6c6b22f76ffb2f6da2714661f255bcea99bb3a93 Mon Sep 17 00:00:00 2001 From: JimenezLi <75196426+JimenezLi@users.noreply.github.com> Date: Sun, 10 Sep 2023 09:02:49 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20=E6=B7=BB=E5=8A=A0=E7=99=BE?= =?UTF-8?q?=E5=BA=A6=E8=B4=B4=E5=90=A7=E6=90=9C=E7=B4=A2=20(#13238)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(route): add baidu tieba search * fix(route): delete ctx.query.limit in case of problems * fix(route): replace outdated querystring with URLSearchParams * fix(route): 修复中文乱码 * Update lib/v2/baidu/tieba/search.js Co-authored-by: Tony * chore(route): rename variable --------- --- lib/v2/baidu/maintainer.js | 1 + lib/v2/baidu/radar.js | 16 ++++++- lib/v2/baidu/router.js | 1 + lib/v2/baidu/templates/tieba_search.art | 1 + lib/v2/baidu/tieba/search.js | 63 +++++++++++++++++++++++++ website/docs/routes/bbs.md | 15 ++++++ 6 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 lib/v2/baidu/templates/tieba_search.art create mode 100644 lib/v2/baidu/tieba/search.js diff --git a/lib/v2/baidu/maintainer.js b/lib/v2/baidu/maintainer.js index 71e0b2ead05b92..7cbf506d1152fd 100644 --- a/lib/v2/baidu/maintainer.js +++ b/lib/v2/baidu/maintainer.js @@ -4,6 +4,7 @@ module.exports = { '/tieba/forum/:kw/:sortBy?': ['u3u'], '/tieba/post/:id': ['u3u'], '/tieba/post/lz/:id': ['u3u'], + '/tieba/search/:qw/:routeParams?': ['JimenezLi'], '/tieba/user/:uid': ['igxlin', 'nczitzk'], '/top/:board?': ['xyqfer'], }; diff --git a/lib/v2/baidu/radar.js b/lib/v2/baidu/radar.js index bac14960884f90..20b2b9e80973e1 100644 --- a/lib/v2/baidu/radar.js +++ b/lib/v2/baidu/radar.js @@ -46,7 +46,7 @@ module.exports = { }, { title: '用户帖子', - docs: 'https://docs.rsshub.app/routes/bbs#tie-ba', + docs: 'https://docs.rsshub.app/routes/bbs#bai-du-tie-ba', source: '/home/main', target: (params, url) => { const uid = new URL(url).searchParams.get('un'); @@ -55,6 +55,20 @@ module.exports = { } }, }, + { + title: '贴吧搜索', + docs: 'https://docs.rsshub.app/routes/bbs#bai-du-tie-ba', + source: '/f/search/res', + target: (params, url) => { + const searchParams = new URL(url).searchParams; + const qw = searchParams.get('qw'); + const kw = searchParams.get('kw'); + if (qw) { + const route = `/baidu/tieba/user/${qw}`; + return kw ? `${route}/kw=${kw}` : route; + } + }, + }, ], top: [ { diff --git a/lib/v2/baidu/router.js b/lib/v2/baidu/router.js index 0fde58e4e0dd6c..e838568aabf9d6 100644 --- a/lib/v2/baidu/router.js +++ b/lib/v2/baidu/router.js @@ -4,6 +4,7 @@ module.exports = (router) => { router.get('/tieba/forum/:kw/:sortBy?', require('./tieba/forum')); router.get('/tieba/post/:id', require('./tieba/post')); router.get('/tieba/post/lz/:id', require('./tieba/post')); + router.get('/tieba/search/:qw/:routeParams?', require('./tieba/search')); router.get('/tieba/user/:uid', require('./tieba/user')); router.get('/top/:board?', require('./top')); }; diff --git a/lib/v2/baidu/templates/tieba_search.art b/lib/v2/baidu/templates/tieba_search.art new file mode 100644 index 00000000000000..5628bcb0e62c4d --- /dev/null +++ b/lib/v2/baidu/templates/tieba_search.art @@ -0,0 +1 @@ +

{{ details }}

{{@ medias }}

贴吧:{{ tieba }}
作者:{{ author }}

diff --git a/lib/v2/baidu/tieba/search.js b/lib/v2/baidu/tieba/search.js new file mode 100644 index 00000000000000..6a1bfdb10001f2 --- /dev/null +++ b/lib/v2/baidu/tieba/search.js @@ -0,0 +1,63 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const iconv = require('iconv-lite'); +const timezone = require('@/utils/timezone'); +const { parseDate } = require('@/utils/parse-date'); +const { art } = require('@/utils/render'); +const path = require('path'); + +module.exports = async (ctx) => { + const qw = ctx.params.qw; + const query = new URLSearchParams(ctx.params.routeParams); + query.set('ie', 'utf-8'); + query.set('qw', qw); + query.set('rn', query.get('rn') || 20); // Number of returned items + const link = `https://tieba.baidu.com/f/search/res?${query.toString()}`; + + const response = await got.get(link, { + headers: { + Referer: 'https://tieba.baidu.com', + }, + responseType: 'buffer', + }); + const data = iconv.decode(response.data, 'gbk'); + + const $ = cheerio.load(data); + const resultList = $('div.s_post'); + + ctx.state.data = { + title: `${qw} - ${query.get('kw') || '百度贴'}吧搜索`, + link, + item: resultList.toArray().map((element) => { + const item = $(element); + const titleItem = item.find('.p_title a'); + const title = titleItem.text().trim(); + const link = titleItem.attr('href'); + const time = item.find('.p_date').text().trim(); + const details = item.find('.p_content').text().trim(); + const medias = item + .find('.p_mediaCont img') + .toArray() + .map((element) => { + const item = $(element); + return ``; + }) + .join(''); + const tieba = item.find('a.p_forum').text().trim(); + const author = item.find('a').last().text().trim(); + + return { + title, + description: art(path.join(__dirname, '../templates/tieba_search.art'), { + details, + medias, + tieba, + author, + }), + author, + pubDate: timezone(parseDate(time, 'YYYY-MM-DD HH:mm'), +8), + link, + }; + }), + }; +}; diff --git a/website/docs/routes/bbs.md b/website/docs/routes/bbs.md index 57101c8c4e097e..f56801f74ead67 100644 --- a/website/docs/routes/bbs.md +++ b/website/docs/routes/bbs.md @@ -480,6 +480,21 @@ When accessing Joeyray's Bar, `SCBOY_BBS_TOKEN` needs to be filled in `environme +### 贴吧搜索 {#bai-du-tie-ba-tie-ba-sou-suo} + + + +| 键 | 含义 | 接受的值 | 默认值 | +| -- | ---- | ------- | ------ | +| kw | 在名为 kw 的贴吧中搜索 | 任意名称/无 | 无 | +| only_thread | 只看主题帖,默认为 0 关闭 | 0/1 | 0 | +| rn | 返回条目的数量 | 1-20 | 20 | +| sm | 排序方式,0 为按时间顺序,1 为按时间倒序,2 为按相关性顺序 | 0/1/2 | 1 | + +用例:`/baidu/tieba/search/neuro/kw=neurosama&only_thread=1&sm=2` + + + ## 才符 {#cai-fu} ### 用户动态 {#cai-fu-yong-hu-dong-tai}