diff --git a/docs/README.md b/docs/README.md index 2ef81a2e3a..66548f71f9 100644 --- a/docs/README.md +++ b/docs/README.md @@ -816,6 +816,16 @@ GitHub 官方也提供了一些 RSS: +### 牛客网 + + + +| 最新回复 | 最新发表 | 最新 | 精华 | +| -------- | -------- | ---- | ---- | +| 0 | 3 | 1 | 4 | + + + ## 直播 ### 哔哩哔哩直播 diff --git a/lib/router.js b/lib/router.js index fcbe0b21b4..c399874e76 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1037,4 +1037,7 @@ router.get('/gaoqing/latest', require('./routes/gaoqing/latest')); // 轻小说文库 router.get('/wenku8/chapter/:id', require('./routes/wenku8/chapter')); +// 牛客网 +router.get('/nowcoder/discuss/:type/:order', require('./routes/nowcoder/discuss')); + module.exports = router; diff --git a/lib/routes/nowcoder/discuss.js b/lib/routes/nowcoder/discuss.js new file mode 100644 index 0000000000..2babf197ff --- /dev/null +++ b/lib/routes/nowcoder/discuss.js @@ -0,0 +1,72 @@ +const axios = require('../../utils/axios'); +const cheerio = require('cheerio'); +const url = require('url'); +const date = require('../../utils/date'); + +const host = 'https://www.nowcoder.com'; + +module.exports = async (ctx) => { + const type = ctx.params.type; + const order = ctx.params.order; + + const link = `https://www.nowcoder.com/discuss?type=${type}&order=${order}`; + const response = await axios.get(link); + const $ = cheerio.load(response.data); + + const type_name = $('a.discuss-tab.selected').text(); + const order_name = $('li.selected a').text(); + + const list = $('li.clearfix') + .map(function() { + const info = { + title: $(this) + .find('div.discuss-main.clearfix a') + .text() + .trim() + .replace('\n', ' '), + link: $(this) + .find('div.discuss-main.clearfix a') + .attr('href'), + }; + return info; + }) + .get(); + + const out = await Promise.all( + list.map(async (info) => { + const title = info.title || 'tzgg'; + const itemUrl = url.resolve(host, info.link); + + const cache = await ctx.cache.get(itemUrl); + if (cache) { + return Promise.resolve(JSON.parse(cache)); + } + + const response = await axios.get(itemUrl); + const $ = cheerio.load(response.data); + + const date_value = $('span.post-time') + .text() + .split(' ')[1]; + + const description = $('.post-topic-main') + .html() + .trim(); + + const single = { + title: title, + link: itemUrl, + description: description, + pubDate: date(date_value, 8), + }; + ctx.cache.set(itemUrl, JSON.stringify(single), 24 * 60 * 60); + return Promise.resolve(single); + }) + ); + + ctx.state.data = { + title: `${type_name}${order_name}——牛客网讨论区`, + link: link, + item: out, + }; +};