diff --git a/docs/programming.md b/docs/programming.md index 30f6883ed..1a60ae76b 100644 --- a/docs/programming.md +++ b/docs/programming.md @@ -220,6 +220,10 @@ GitHub 官方也提供了一些 RSS: +### 帖子 + + + ## 安全客 ::: tip 提示 diff --git a/lib/router.js b/lib/router.js index e85af999a..dcc8f44e3 100644 --- a/lib/router.js +++ b/lib/router.js @@ -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')); diff --git a/lib/routes/v2ex/post.js b/lib/routes/v2ex/post.js new file mode 100644 index 000000000..9a57170d4 --- /dev/null +++ b/lib/routes/v2ex/post.js @@ -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(), + }; +};