Skip to content

Commit

Permalink
Merge pull request #53 from DIYgod/master
Browse files Browse the repository at this point in the history
[pull] master from diygod:master
  • Loading branch information
pull[bot] authored Sep 10, 2023
2 parents e36e1c8 + 6c6b22f commit cb6ba86
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/v2/baidu/maintainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
};
16 changes: 15 additions & 1 deletion lib/v2/baidu/radar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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: [
{
Expand Down
1 change: 1 addition & 0 deletions lib/v2/baidu/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
};
1 change: 1 addition & 0 deletions lib/v2/baidu/templates/tieba_search.art
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>{{ details }}</p><p>{{@ medias }}</p><p>贴吧:{{ tieba }}<br/>作者:{{ author }}</p>
63 changes: 63 additions & 0 deletions lib/v2/baidu/tieba/search.js
Original file line number Diff line number Diff line change
@@ -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 `<img src="${item.attr('original')}">`;
})
.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,
};
}),
};
};
15 changes: 15 additions & 0 deletions website/docs/routes/bbs.md
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,21 @@ When accessing Joeyray's Bar, `SCBOY_BBS_TOKEN` needs to be filled in `environme

</Route>

### 贴吧搜索 {#bai-du-tie-ba-tie-ba-sou-suo}

<Route author="JimenezLi" example="/baidu/tieba/search/neuro" path="/baidu/tieba/search/:qw/:routeParams?" paramsDesc={['搜索关键词', '额外参数;请参阅以下说明和表格']} radar="1">

|| 含义 | 接受的值 | 默认值 |
| -- | ---- | ------- | ------ |
| 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`

</Route>

## 才符 {#cai-fu}

### 用户动态 {#cai-fu-yong-hu-dong-tai}
Expand Down

0 comments on commit cb6ba86

Please sign in to comment.