diff --git a/docs/social-media.md b/docs/social-media.md index 13acc5533..707cd3fae 100644 --- a/docs/social-media.md +++ b/docs/social-media.md @@ -664,6 +664,10 @@ Tiny Tiny RSS 会给所有 iframe 元素添加 `sandbox="allow-scripts"` 属性 +### 日记最新回应 + + + ### 话题 diff --git a/lib/router.js b/lib/router.js index fc2648e42..20a78549f 100644 --- a/lib/router.js +++ b/lib/router.js @@ -184,6 +184,7 @@ router.get('/douban/book/rank/:type?', require('./routes/douban/book/rank')); router.get('/douban/doulist/:id', require('./routes/douban/doulist')); router.get('/douban/explore/column/:id', require('./routes/douban/explore_column')); router.get('/douban/people/:userid/status', require('./routes/douban/people/status.js')); +router.get('/douban/replies/:uid', require('./routes/douban/replies')); router.get('/douban/topic/:id/:sort?', require('./routes/douban/topic.js')); router.get('/douban/channel/:id/:nav?', require('./routes/douban/channel/topic.js')); router.get('/douban/channel/:id/subject/:nav', require('./routes/douban/channel/subject.js')); diff --git a/lib/routes/douban/replies.js b/lib/routes/douban/replies.js new file mode 100644 index 000000000..6f1ba0b9e --- /dev/null +++ b/lib/routes/douban/replies.js @@ -0,0 +1,65 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + const currentUrl = `https://www.douban.com/people/${ctx.params.uid}/notes`; + const response = await got({ + method: 'get', + url: currentUrl, + }); + + const $ = cheerio.load(response.data); + const list = $('div.recent-replies-mod ul.comment-list li') + .map((_, item) => { + item = $(item); + const p = item.find('p'); + const match = p + .find('a') + .attr('href') + .match(/%2Fnote%2F(.*?)%2F%23(.*?)&type=note/); + const nid = match[1]; + const cid = match[2]; + p.remove(); + return { + link: `https://www.douban.com/note/${nid}/#${cid}`, + }; + }) + .get(); + + const items = await Promise.all( + list.map( + async (item) => + await ctx.cache.tryGet(item.link, async () => { + const detailResponse = await got({ + method: 'get', + url: item.link, + }); + const content = cheerio.load(detailResponse.data); + const commentsConfig = content('script').eq(16).html().replace('var _COMMENTS_CONFIG =', ''); + const comments = JSON.parse(commentsConfig.match(/'comments':(.*?)}],/)[1] + '}]'); + + let title, description, pubDate; + + for (const c of comments) { + if (c.id === item.link.split('#')[1]) { + (title = `${c.author.name} 于 ${c.create_time} 的回应`), (description = c.text); + pubDate = new Date(c.create_time + ' GMT+8').toUTCString(); + break; + } + } + + item.title = title; + item.description = description; + item.pubDate = pubDate; + + return item; + }) + ) + ); + + ctx.state.data = { + title: $('title').text() + ' - 最新回应', + link: currentUrl, + item: items, + }; +};