From d50e0948dcf3dddabd576d1ed9dbf070cc3a6a1b Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Tue, 29 Mar 2022 22:03:13 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20add=20=E8=99=8E=E6=89=91?= =?UTF-8?q?=E9=A6=96=E9=A1=B5=20(#9416)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/bbs.md | 52 ++++++++--------- lib/router.js | 8 +-- lib/routes/hupu/all.js | 53 ----------------- lib/routes/hupu/bbs.js | 83 --------------------------- lib/routes/hupu/dept.js | 66 --------------------- lib/v2/hupu/all.js | 73 +++++++++++++++++++++++ lib/v2/hupu/bbs.js | 64 +++++++++++++++++++++ lib/v2/hupu/index.js | 72 +++++++++++++++++++++++ lib/v2/hupu/maintainer.js | 7 +++ lib/v2/hupu/radar.js | 35 +++++++++++ lib/v2/hupu/router.js | 7 +++ lib/v2/hupu/templates/description.art | 6 ++ 12 files changed, 292 insertions(+), 234 deletions(-) delete mode 100644 lib/routes/hupu/all.js delete mode 100644 lib/routes/hupu/bbs.js delete mode 100644 lib/routes/hupu/dept.js create mode 100644 lib/v2/hupu/all.js create mode 100644 lib/v2/hupu/bbs.js create mode 100644 lib/v2/hupu/index.js create mode 100644 lib/v2/hupu/maintainer.js create mode 100644 lib/v2/hupu/radar.js create mode 100644 lib/v2/hupu/router.js create mode 100644 lib/v2/hupu/templates/description.art diff --git a/docs/bbs.md b/docs/bbs.md index a5de64673..05fc4fb28 100644 --- a/docs/bbs.md +++ b/docs/bbs.md @@ -356,47 +356,43 @@ pageClass: routes ## 虎扑 -### 虎扑 BBS 论坛 +### 首页 - + -此路由与旧的 `/hupu/bxj/:id/:order?` 等价,但推荐使用 `/hupu/bbs/:id/:order?`,旧路由可能会在未来被删除。 +| NBA | CBA | 足球 | +| --- | --- | ------ | +| nba | cba | soccer | + +::: tip 提示 + +电竞分类参见 [游戏热帖](https://bbs.hupu.com/all-gg) 的对应路由 [`/hupu/all/all-gg`](https://rsshub.app/hupu/all/all-gg)。 + +::: -### 分类 +### 社区 - + -| 分类 | 名称 | -| ------ | -------- | -| NBA 论坛 | nba | -| CBA 论坛 | cba | -| 运动和装备 | gear | -| 国际足球论坛 | soccer | -| 中国足球论坛 | csl | -| 综合体育 | sports | -| 步行街 | gambia | -| 彩票中心 | lottery | -| 自建版块 | boards | -| 虎扑社团 | group | -| 站务管理 | feedback | +::: tip 提示 -更多参见 [虎扑社区版块目录](https://bbs.hupu.com/boards.php) 左侧导航栏各板块分类页 +更多社区参见 [社区](https://m.hupu.com/bbs) + +::: -### 子站 +### 热帖 - + -| 子站 | 名称 | -| ---- | ------------ | -| NBA | nba | -| 国际足球 | soccer | -| 中国足球 | soccer-china | -| CBA | cba | -| 电竞 | gg | +::: tip 提示 + +更多热帖版面参见 [论坛](https://bbs.hupu.com) + +::: diff --git a/lib/router.js b/lib/router.js index 1c34a6637..d66ab24a5 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1333,10 +1333,10 @@ router.get('/leetcode/submission/us/:user', lazyloadRouteHandler('./routes/leetc router.get('/leetcode/submission/cn/:user', lazyloadRouteHandler('./routes/leetcode/check-cn')); // 虎扑 -router.get('/hupu/bxj/:id/:order?', lazyloadRouteHandler('./routes/hupu/bbs')); -router.get('/hupu/bbs/:id/:order?', lazyloadRouteHandler('./routes/hupu/bbs')); -router.get('/hupu/all/:caty', lazyloadRouteHandler('./routes/hupu/all')); -router.get('/hupu/dept/:dept', lazyloadRouteHandler('./routes/hupu/dept')); +// router.get('/hupu/bxj/:id/:order?', lazyloadRouteHandler('./routes/hupu/bbs')); +// router.get('/hupu/bbs/:id/:order?', lazyloadRouteHandler('./routes/hupu/bbs')); +// router.get('/hupu/all/:caty', lazyloadRouteHandler('./routes/hupu/all')); +// router.get('/hupu/dept/:dept', lazyloadRouteHandler('./routes/hupu/dept')); // 牛客网 migrated to v2 // router.get('/nowcoder/discuss/:type/:order', lazyloadRouteHandler('./routes/nowcoder/discuss')); diff --git a/lib/routes/hupu/all.js b/lib/routes/hupu/all.js deleted file mode 100644 index ccf27265d..000000000 --- a/lib/routes/hupu/all.js +++ /dev/null @@ -1,53 +0,0 @@ -const got = require('@/utils/got'); -const cheerio = require('cheerio'); - -module.exports = async (ctx) => { - const rootUrl = 'https://bbs.hupu.com'; - const currentUrl = `${rootUrl}/all-${ctx.params.caty}`; - - const response = await got({ - method: 'get', - url: currentUrl, - }); - const $ = cheerio.load(response.data); - - const list = $('div.list ul li') - .slice(0, 20) - .map((_, item) => { - item = $(item); - const a = item.find('span.textSpan a[title]'); - return { - title: a.attr('title'), - link: `${rootUrl}${a.attr('href')}`, - }; - }) - .get(); - - const items = await Promise.all( - list.map((item) => - ctx.cache.tryGet(item.link, async () => { - try { - const detailResponse = await got({ - method: 'get', - url: item.link, - }); - const content = cheerio.load(detailResponse.data); - - item.description = content('div.quote-content').html(); - item.author = content('div.author div.left a.u').eq(0).text(); - item.pubDate = new Date(content('span.stime').eq(0).text() + ' GMT+8').toUTCString(); - - return item; - } catch (error) { - return Promise.resolve(''); - } - }) - ) - ); - - ctx.state.data = { - title: $('title').text(), - link: currentUrl, - item: items, - }; -}; diff --git a/lib/routes/hupu/bbs.js b/lib/routes/hupu/bbs.js deleted file mode 100644 index 11767c942..000000000 --- a/lib/routes/hupu/bbs.js +++ /dev/null @@ -1,83 +0,0 @@ -const got = require('@/utils/got'); -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 if (parseInt(order) === 2) { - order_name = '最新发帖'; - link += '-postdate-1'; - } else if (parseInt(order) === 3) { - order_name = '精华'; - link += '-digest'; - } - - const response = await got.get(link); - const $ = cheerio.load(response.data); - - const type_name = $('span.infoname').text(); - const description = $('#des_forum').text(); - - const list = $('ul.for-list li') - .slice(0, 10) - .map(function () { - const info = { - title: $(this).find('div.titlelink.box > a.truetit').text().trim(), - link: $(this).find('div.titlelink.box > a.truetit').attr('href'), - author: $(this).find('a.aulink').text(), - }; - 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 author = info.author; - - const cache = await ctx.cache.get(itemUrl); - if (cache) { - return Promise.resolve(JSON.parse(cache)); - } - - const response = await got.get(itemUrl); - const $ = cheerio.load(response.data); - - const date = $('#tpc span.stime').text(); - const description = $('div.quote-content').html(); - - const single = { - title, - link: itemUrl, - author, - description, - pubDate: new Date(date).toUTCString(), - }; - ctx.cache.set(itemUrl, JSON.stringify(single)); - return Promise.resolve(single); - }) - ); - - ctx.state.data = { - title: `${type_name}${order_name}——虎扑步行街`, - link, - description, - item: out, - }; -}; diff --git a/lib/routes/hupu/dept.js b/lib/routes/hupu/dept.js deleted file mode 100644 index 8dc8f9e6e..000000000 --- a/lib/routes/hupu/dept.js +++ /dev/null @@ -1,66 +0,0 @@ -const got = require('@/utils/got'); -const cheerio = require('cheerio'); - -module.exports = async (ctx) => { - let currentUrl = ''; - if (ctx.params.dept === 'soccer-china') { - currentUrl = `https://soccer.hupu.com/china`; - } else { - currentUrl = `https://${ctx.params.dept}.hupu.com`; - } - - const response = await got({ - method: 'get', - url: currentUrl, - }); - const $ = cheerio.load(response.data); - - const query = { - nba: 'div.list-item', - soccer: 'div.list-item', - 'soccer-china': '#focusNews', - cba: '#focusNews', - gg: 'div.fouce-news', - }; - - const list = $(query[ctx.params.dept]) - .find('a[href!=""]') - .slice(0, 20) - .map((_, item) => { - item = $(item); - - return { - title: item.text(), - link: item.attr('href'), - }; - }) - .get(); - - const items = await Promise.all( - list.map((item) => - ctx.cache.tryGet(item.link, async () => { - try { - const detailResponse = await got({ - method: 'get', - url: item.link, - }); - const content = cheerio.load(detailResponse.data); - - item.description = content('div.quote-content').html(); - item.author = content('div.author div.left a.u').eq(0).text(); - item.pubDate = new Date(content('span.stime').eq(0).text() + ' GMT+8').toUTCString(); - - return item; - } catch (error) { - return Promise.resolve(''); - } - }) - ) - ); - - ctx.state.data = { - title: $('title').text(), - link: currentUrl, - item: items, - }; -}; diff --git a/lib/v2/hupu/all.js b/lib/v2/hupu/all.js new file mode 100644 index 000000000..5f4702096 --- /dev/null +++ b/lib/v2/hupu/all.js @@ -0,0 +1,73 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const timezone = require('@/utils/timezone'); +const { parseDate, parseRelativeDate } = require('@/utils/parse-date'); +const { art } = require('@/utils/render'); +const path = require('path'); + +module.exports = async (ctx) => { + const id = ctx.params.id ?? 'topic-daily'; + + const rootUrl = 'https://bbs.hupu.com'; + const currentUrl = `${rootUrl}/${id}`; + + const response = await got({ + method: 'get', + url: currentUrl, + }); + + const $ = cheerio.load(response.data); + + let items = $('div.t-info > a, a.p-title') + .toArray() + .map((item) => { + item = $(item); + + return { + title: item.text(), + link: `https://m.hupu.com/bbs${item.attr('href')}`, + pubDate: timezone(parseDate(item.parent().parent().find('.post-time').text(), 'MM-DD HH:mm'), +8), + }; + }); + + items = await Promise.all( + items.map((item) => + ctx.cache.tryGet(item.link, async () => { + try { + const detailResponse = await got({ + method: 'get', + url: item.link, + }); + + const content = cheerio.load(detailResponse.data); + + const videos = []; + + content('.thread-video-wrap').each(function () { + videos.push(content('video').attr('src')); + content(this).remove(); + }); + + content('.bbs-user-info-time span').remove(); + + item.author = content('.bbs-user-info-name').first().text(); + item.pubDate = item.pubDate ?? timezone(parseRelativeDate(content('.bbs-user-info-time').first().text()), +8); + item.description = art(path.join(__dirname, 'templates/description.art'), { + videos, + description: content('.bbs-thread-content').html(), + }); + } catch (e) { + // no-empty + } + + return item; + }) + ) + ); + + ctx.state.data = { + title: `虎扑社区 - ${$('.middle-title, .bbs-sl-web-intro-detail-title').text()}`, + link: currentUrl, + item: items, + }; +}; diff --git a/lib/v2/hupu/bbs.js b/lib/v2/hupu/bbs.js new file mode 100644 index 000000000..1829cc1d4 --- /dev/null +++ b/lib/v2/hupu/bbs.js @@ -0,0 +1,64 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const timezone = require('@/utils/timezone'); +const { parseRelativeDate } = require('@/utils/parse-date'); +const { art } = require('@/utils/render'); +const path = require('path'); + +module.exports = async (ctx) => { + const id = ctx.params.id ?? '34'; + const order = ctx.params.order ?? '1'; + + const rootUrl = 'https://m.hupu.com'; + const currentUrl = `${rootUrl}/api/v1/bbs-forumthreadlist-frontend/${id}-1?postdate=${order}`; + + const response = await got({ + method: 'get', + url: currentUrl, + }); + + let items = response.data.data.t_list.map((item) => ({ + title: item.title, + link: `${rootUrl}/bbs/${item.tid}.html`, + pubDate: timezone(parseRelativeDate(item.t_post), +8), + })); + + items = await Promise.all( + items.map((item) => + ctx.cache.tryGet(item.link, async () => { + try { + const detailResponse = await got({ + method: 'get', + url: item.link, + }); + + const content = cheerio.load(detailResponse.data); + + const videos = []; + + content('.thread-video-wrap').each(function () { + videos.push(content('video').attr('src')); + content(this).remove(); + }); + + item.author = content('.bbs-user-info-name').first().text(); + item.description = art(path.join(__dirname, 'templates/description.art'), { + videos, + description: content('#bbs-thread-content').html(), + }); + } catch (e) { + // no-empty + } + + return item; + }) + ) + ); + + ctx.state.data = { + title: `虎扑社区 - ${response.data.data.keywords}`, + link: currentUrl, + item: items, + description: response.data.data.description, + }; +}; diff --git a/lib/v2/hupu/index.js b/lib/v2/hupu/index.js new file mode 100644 index 000000000..dda25a1f7 --- /dev/null +++ b/lib/v2/hupu/index.js @@ -0,0 +1,72 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const timezone = require('@/utils/timezone'); +const { parseDate } = require('@/utils/parse-date'); + +const categories = { + nba: { + title: 'NBA', + data: 'news', + }, + cba: { + title: 'CBA', + data: 'news', + }, + soccer: { + title: '足球', + data: 'soccerIndexData', + }, +}; + +module.exports = async (ctx) => { + const category = ctx.params.category ?? 'soccer'; + + const rootUrl = 'https://m.hupu.com'; + const currentUrl = `${rootUrl}/${category}`; + + const response = await got({ + method: 'get', + url: currentUrl, + }); + + const data = JSON.parse(response.data.match(/"pageProps":(.*)},"page":"\//)[1]); + + let items = data[categories[category].data].map((item) => ({ + title: item.title, + pubDate: timezone(parseDate(item.publishTime), +8), + link: item.link.replace(/bbs\.hupu.com/, 'm.hupu.com/bbs'), + })); + + items = await Promise.all( + items + .filter((item) => !/subject/.test(item.link)) + .map((item) => + ctx.cache.tryGet(item.link, async () => { + try { + const detailResponse = await got({ + method: 'get', + url: item.link, + }); + + const content = cheerio.load(detailResponse.data); + + item.author = content('.bbs-user-info-name').text(); + item.description = content('#bbs-thread-content').html(); + item.category = content('.bbs-link') + .toArray() + .map((c) => content(c).text()); + } catch (e) { + // no-empty + } + + return item; + }) + ) + ); + + ctx.state.data = { + title: `虎扑 - ${categories[category].title}`, + link: currentUrl, + item: items, + }; +}; diff --git a/lib/v2/hupu/maintainer.js b/lib/v2/hupu/maintainer.js new file mode 100644 index 000000000..6460c3daf --- /dev/null +++ b/lib/v2/hupu/maintainer.js @@ -0,0 +1,7 @@ +module.exports = { + '/all/:id?': ['nczitzk'], + '/bbs/:id?/:order?': ['LogicJake', 'nczitzk'], + '/bxj/:id?/:order?': ['LogicJake', 'nczitzk'], + '/dept/:category?': ['nczitzk'], + '/:category?': ['nczitzk'], +}; diff --git a/lib/v2/hupu/radar.js b/lib/v2/hupu/radar.js new file mode 100644 index 000000000..c4941233d --- /dev/null +++ b/lib/v2/hupu/radar.js @@ -0,0 +1,35 @@ +module.exports = { + 'hupu.com': { + _name: '虎扑', + '': [ + { + title: '首页', + docs: 'https://docs.rsshub.app/bbs.html#hu-pu-shou-ye', + source: ['/:category', '/'], + target: '/hupu/:category', + }, + ], + bbs: [ + { + title: '热帖', + docs: 'https://docs.rsshub.app/bbs.html#hu-pu-re-tie', + source: ['/:id'], + target: '/hupu/all/:id', + }, + ], + m: [ + { + title: '首页', + docs: 'https://docs.rsshub.app/bbs.html#hu-pu-shou-ye', + source: ['/:category', '/'], + target: '/hupu/:category', + }, + { + title: '社区', + docs: 'https://docs.rsshub.app/bbs.html#hu-pu-she-qu', + source: ['/bbs/:id', '/'], + target: '/hupu/bbs/:id/:order', + }, + ], + }, +}; diff --git a/lib/v2/hupu/router.js b/lib/v2/hupu/router.js new file mode 100644 index 000000000..e4da8e83b --- /dev/null +++ b/lib/v2/hupu/router.js @@ -0,0 +1,7 @@ +module.exports = function (router) { + router.get('/all/:id?', require('./all')); + router.get('/bbs/:id?/:order?', require('./bbs')); + router.get('/bxj/:id?/:order?', require('./bbs')); + router.get('/dept/:category?', require('./index')); + router.get('/:category?', require('./index')); +}; diff --git a/lib/v2/hupu/templates/description.art b/lib/v2/hupu/templates/description.art new file mode 100644 index 000000000..73610b2e6 --- /dev/null +++ b/lib/v2/hupu/templates/description.art @@ -0,0 +1,6 @@ +{{ if videos }} +{{ each videos video}} + +{{/each}} +{{ /if }} +{{@ description }} \ No newline at end of file