diff --git a/docs/README.md b/docs/README.md index 3c17f58639..71465f538f 100644 --- a/docs/README.md +++ b/docs/README.md @@ -599,6 +599,10 @@ RSSHub 提供下列 API 接口: +### 虎扑 + + + ## 编程 ### 掘金 diff --git a/lib/router.js b/lib/router.js index 87561abb56..25fa81afbd 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1044,6 +1044,9 @@ router.get('/gaoqing/latest', require('./routes/gaoqing/latest')); // 轻小说文库 router.get('/wenku8/chapter/:id', require('./routes/wenku8/chapter')); +// 虎扑 +router.get('/hupu/bxj/:id/:order?', require('./routes/hupu/bxj')); + // 牛客网 router.get('/nowcoder/discuss/:type/:order', require('./routes/nowcoder/discuss')); diff --git a/lib/routes/hupu/bxj.js b/lib/routes/hupu/bxj.js new file mode 100644 index 0000000000..1a8e3dae87 --- /dev/null +++ b/lib/routes/hupu/bxj.js @@ -0,0 +1,82 @@ +const axios = require('../../utils/axios'); +const cheerio = require('cheerio'); +const url = require('url'); + +const host = 'https://bbs.hupu.com'; + +module.exports = async (ctx) => { + const id = ctx.params.id; + const order = ctx.params.order || 1; + + let link = `https://bbs.hupu.com/${id}`; + + let order_name; + if (parseInt(order) === 1) { + order_name = '最新回帖'; + } else { + order_name = '最新发帖'; + link += '-postdate-1'; + } + + const response = await axios.get(link); + const $ = cheerio.load(response.data); + + const type_name = $('span.infoname').text(); + + const list = $('ul.for-list li') + .slice(0, 10) + .map(function() { + const info = { + title: $(this) + .find('div.titlelink.box > a') + .text() + .trim(), + link: $(this) + .find('div.titlelink.box > a') + .attr('href'), + }; + return info; + }) + .get(); + + for (let i = list.length - 1; i >= 0; i--) { + if (!list[i].link.endsWith('html')) { + list.splice(i, 1); + } + } + + const out = await Promise.all( + list.map(async (info) => { + const title = info.title; + 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 = $('#tpc span.stime').text(); + const description = $('div.quote-content') + .html() + .trim(); + + const single = { + title: title, + link: itemUrl, + description: description, + pubDate: new Date(date).toUTCString(), + }; + 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, + }; +};