feat: add v2ex post (#2655)

This commit is contained in:
Cloud 2019-07-20 12:55:06 +08:00 committed by DIYgod
parent 26d08ee047
commit da7f756364
3 changed files with 44 additions and 0 deletions

View File

@ -220,6 +220,10 @@ GitHub 官方也提供了一些 RSS:
<Route author="WhiteWorld" example="/v2ex/topics/latest" path="/v2ex/topics/:type" :paramsDesc="['hot 或 latest']"/>
### 帖子
<Route author="kt286" example="/v2ex/post/584403" path="/v2ex/post/:postid" :paramsDesc="['帖子ID在 URL 可以找到']"/>
## 安全客
::: tip 提示

View File

@ -233,6 +233,7 @@ router.get('/kingkong/room/:id', require('./routes/kingkong/room'));
// v2ex
router.get('/v2ex/topics/:type', require('./routes/v2ex/topics'));
router.get('/v2ex/post/:postid', require('./routes/v2ex/post'));
// Telegram
router.get('/telegram/channel/:username', require('./routes/telegram/channel'));

39
lib/routes/v2ex/post.js Normal file
View File

@ -0,0 +1,39 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const postid = ctx.params.postid;
const pageUrl = `https://www.v2ex.com/t/${postid}`;
const response = await got({
method: 'get',
url: pageUrl,
});
const $ = cheerio.load(response.data);
const list = $('[id^="r_"]').get();
ctx.state.data = {
title: `V2EX-${$('.header h1').text()}`,
link: pageUrl,
description: $('.topic_content').text(),
item: list
.map((item) => {
const post = $(item);
const reply_content = post.find('.reply_content').first();
const no = post.find('.no').first();
return {
title: `#${no.text()} ${reply_content.text()}`,
description: reply_content.html(),
guid: post.attr('id'),
link: `${pageUrl}#${post.attr('id')}`,
author: post
.find('.dark')
.first()
.text(),
};
})
.reverse(),
};
};