Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加豆瓣网站中关于某书的论坛页面(#13838) #13849

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions lib/v2/douban/other/discussion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* @Author: nightmare-mio wanglongwei2009@qq.com
* @Date: 2023-11-20 23:36:12
* @LastEditTime: 2023-11-21 22:13:01
* @Description:
*/
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');

module.exports = async (ctx) => {
const { id } = ctx.params;
const link = 'https://book.douban.com/subject';
const { data: response } = await got({
method: 'get',
url: `${link}/${id}/discussion`,
});
const $ = cheerio.load(response);

// 列表
const list = $('#posts-table>tbody>tr')
.toArray()
.map((item) => {
item = $(item);
const a = item.find('a').first();
return {
title: a.attr('title'),
link: a.attr('href'),
pubDate: parseDate(item.find('.time').text()),
author: item.find('a').eq(1).text(),
};
});
list.shift();
const title = $('#content>h1').text();

const items = await Promise.all(
list.map((item) =>
ctx.cache.tryGet(item.link, async () => {
const { data: response } = await got(item.link);
const $ = cheerio.load(response);
// 评论
const list = $('#comments>.comment-item').toArray();
let no = 0;
const replyContent = list
.map((item) => {
const post = $(item);
const content = post.find('.content>p').html();
const author = post.find('.author>a').text();
no++;
return `<p><div>#${no}: <i>${author}</i></div><div>${content}</div></p>`;
})
.join('');
item.description = `${$('#link-report>div').eq(1).html()}<div>${replyContent}</div>`;
return item;
})
)
);

ctx.state.data = {
title,
link: `${link}/${id}/discussion`,
item: items,
};
};
1 change: 1 addition & 0 deletions lib/v2/douban/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ module.exports = function (router) {
router.get('/replied/:uid', require('./other/replied'));
router.get('/replies/:uid', require('./other/replies'));
router.get('/topic/:id/:sort?', require('./other/topic.js'));
router.get('/:id/discussion', require('./other/discussion.js'));
};
Loading