mirror of
https://github.com/DIYgod/RSSHub.git
synced 2026-09-24 23:12:34 +08:00
feat: add tieba post (#535)
ref (https://github.com/MoeFE/GoogleTranslate/commit/f734cd59e570c09625ebd0623aa845855f2a7132#commitcomment-30275584)
This commit is contained in:
@@ -59,6 +59,8 @@ RSSHub 是一个轻量、易于扩展的 RSS 生成器,可以给任何奇奇
|
||||
- 贴吧
|
||||
- 帖子列表
|
||||
- 精品帖子
|
||||
- 帖子动态
|
||||
- 楼主动态
|
||||
- 即刻
|
||||
- 主题
|
||||
- 用户动态
|
||||
|
||||
@@ -769,6 +769,26 @@ rid: 排行榜分区 id,默认 0
|
||||
|
||||
`cid`: 精品分类,如果不传 `cid` 则获取全部分类
|
||||
|
||||
### 帖子动态 <Author uid="u3u"/>
|
||||
|
||||
举例: [https://rsshub.app/tieba/post/5853240586](https://rsshub.app/tieba/post/5853240586)
|
||||
|
||||
路由: `/tieba/post/:id`
|
||||
|
||||
参数:
|
||||
|
||||
`id`: 帖子 ID
|
||||
|
||||
### 楼主动态 <Author uid="u3u"/>
|
||||
|
||||
举例: [https://rsshub.app/tieba/post/lz/5853240586](https://rsshub.app/tieba/post/lz/5853240586)
|
||||
|
||||
路由: `/tieba/post/lz/:id`
|
||||
|
||||
参数:
|
||||
|
||||
`id`: 帖子 ID
|
||||
|
||||
## 即刻
|
||||
|
||||
### 主题-精选 <Author uid="DIYgod"/>
|
||||
|
||||
@@ -103,6 +103,7 @@ ctx.state.data = {
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
## 参与讨论
|
||||
|
||||
1. [Telegram 群](https://t.me/rsshub)
|
||||
|
||||
@@ -119,6 +119,12 @@ router.get('/weibo/user/:uid', require('./routes/weibo/user'));
|
||||
router.get('/weibo/user2/:uid', require('./routes/weibo/user2'));
|
||||
router.get('/weibo/keyword/:keyword', require('./routes/weibo/keyword'));
|
||||
|
||||
// 贴吧
|
||||
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('/ncm/playlist/:id', require('./routes/ncm/playlist'));
|
||||
router.get('/ncm/user/playlist/:uid', require('./routes/ncm/userplaylist'));
|
||||
@@ -149,10 +155,6 @@ router.get('/zhihu/people/answers/:id', require('./routes/zhihu/answers'));
|
||||
router.get('/zhihu/zhuanlan/:id', require('./routes/zhihu/zhuanlan'));
|
||||
router.get('/zhihu/daily', require('./routes/zhihu/daily'));
|
||||
|
||||
// 贴吧
|
||||
router.get('/tieba/forum/:kw', require('./routes/tieba/forum'));
|
||||
router.get('/tieba/forum/good/:kw/:cid?', require('./routes/tieba/forum'));
|
||||
|
||||
// 妹子图
|
||||
router.get('/mzitu', require('./routes/mzitu/category'));
|
||||
router.get('/mzitu/tags', require('./routes/mzitu/tags'));
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
const cheerio = require('cheerio');
|
||||
const axios = require('../../utils/axios');
|
||||
|
||||
/**
|
||||
* 获取最新的帖子回复(倒序查看)
|
||||
*
|
||||
* @param {*} id 帖子ID
|
||||
* @param {number} [lz=0] 是否只看楼主(0: 查看全部, 1: 只看楼主)
|
||||
* @param {number} [pn=7e6] 帖子最大页码(默认假设为 7e6,如果超出假设则根据返回的最大页码再请求一次,否则可以节省一次请求)
|
||||
* 这个默认值我测试下来 7e6 是比较接近最大值了,因为当我输入 8e6 就会返回第一页的数据而不是最后一页了
|
||||
* @returns
|
||||
*/
|
||||
async function getPost(id, lz = 0, pn = 7e6) {
|
||||
const { data } = await axios({
|
||||
method: 'get',
|
||||
url: `https://tieba.baidu.com/p/${id}?see_lz=${lz}&pn=${pn}&ajax=1`,
|
||||
headers: {
|
||||
Referer: 'https://tieba.baidu.com/',
|
||||
},
|
||||
});
|
||||
const $ = cheerio.load(data);
|
||||
const max = Number.parseInt($('[max-page]').attr('max-page'));
|
||||
if (max > pn) {
|
||||
return getPost(id, max);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
module.exports = async (ctx) => {
|
||||
const { id } = ctx.params;
|
||||
const lz = ctx._matchedRoute.includes('lz') ? 1 : 0;
|
||||
const html = await getPost(id, lz);
|
||||
const $ = cheerio.load(html);
|
||||
const title = $('.core_title_txt')
|
||||
.attr('title')
|
||||
.substr(3);
|
||||
const list = $('.p_postlist > [data-field]:not(:has(".ad_bottom_view"))');
|
||||
|
||||
ctx.state.data = {
|
||||
title: lz ? `【只看楼主】${title}` : title,
|
||||
link: `https://tieba.baidu.com/p/${id}?see_lz=${lz}`,
|
||||
description: `${title}的最新回复`,
|
||||
item:
|
||||
list &&
|
||||
list
|
||||
.map((index, element) => {
|
||||
const item = $(element);
|
||||
const { author, content } = item.data('field');
|
||||
const [from, num, time] = item
|
||||
.find('.post-tail-wrap > .tail-info')
|
||||
.map((index, element) => $(element).text())
|
||||
.get();
|
||||
return {
|
||||
title: `${author.user_name}回复了帖子《${title}》`,
|
||||
description: [
|
||||
`<p>${content.content}</p>`,
|
||||
`作者:${author.user_name}`,
|
||||
`楼层:${num}`,
|
||||
`${from}`
|
||||
].join('<br />'), // prettier-ignore
|
||||
pubDate: new Date(time).toUTCString(),
|
||||
link: `https://tieba.baidu.com/p/${id}?pid=${content.post_id}#${content.post_id}`,
|
||||
};
|
||||
})
|
||||
.get(),
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user