feat: add 豆瓣日记最新回应 (#5653)

This commit is contained in:
Ethan Shen 2020-09-21 04:44:53 +08:00 committed by GitHub
parent 9e6c119009
commit 05f50edacc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 70 additions and 0 deletions

View File

@ -664,6 +664,10 @@ Tiny Tiny RSS 会给所有 iframe 元素添加 `sandbox="allow-scripts"` 属性
</Route>
### 日记最新回应
<Route author="nczitzk" example="/douban/replies/xiaoyaxiaoya" path="/douban/replies/:uid" :paramsDesc="['用户id可在用户日记页 URL 中找到']"/>
### 话题
<Route author="LogicJake" example="/douban/topic/48823" path="/douban/topic/:id/:sort?" :paramsDesc="['话题id','排序方式hot或new默认为new']"/>

View File

@ -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'));

View File

@ -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,
};
};