From 5a59ec4521d370c5e1710aeb93ab57d1fdcd2f7e Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Tue, 22 Feb 2022 04:28:57 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20add=20=E7=9B=B4=E6=92=AD?= =?UTF-8?q?=E5=90=A7=E6=BB=9A=E5=8A=A8=E6=96=B0=E9=97=BB=20(#7621)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(route): add 直播吧滚动新闻 * refactor: migrate to v2 Co-authored-by: TonyRL --- docs/bbs.md | 8 ++- lib/router.js | 8 +-- lib/routes/zhibo8/more.js | 45 ---------------- lib/{routes => v2}/zhibo8/forum.js | 24 +++------ lib/v2/zhibo8/maintainer.js | 5 ++ lib/v2/zhibo8/more.js | 82 ++++++++++++++++++++++++++++++ lib/{routes => v2}/zhibo8/post.js | 2 +- lib/v2/zhibo8/radar.js | 27 ++++++++++ lib/v2/zhibo8/router.js | 5 ++ 9 files changed, 139 insertions(+), 67 deletions(-) delete mode 100644 lib/routes/zhibo8/more.js rename lib/{routes => v2}/zhibo8/forum.js (62%) create mode 100644 lib/v2/zhibo8/maintainer.js create mode 100644 lib/v2/zhibo8/more.js rename lib/{routes => v2}/zhibo8/post.js (95%) create mode 100644 lib/v2/zhibo8/radar.js create mode 100644 lib/v2/zhibo8/router.js diff --git a/docs/bbs.md b/docs/bbs.md index f427aa984..16ed9c986 100644 --- a/docs/bbs.md +++ b/docs/bbs.md @@ -885,7 +885,13 @@ pageClass: routes ### 滚动新闻 - + + +| NBA | 足球 | 电竞 | 综合 | +| --- | ----- | -------- | ------ | +| nba | zuqiu | dianjing | zonghe | + + ## 中国灵异网 diff --git a/lib/router.js b/lib/router.js index 215e334d6..6cc37e781 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1781,10 +1781,10 @@ router.get('/polimi/news/:language?', lazyloadRouteHandler('./routes/polimi/news // dekudeals router.get('/dekudeals/:type', lazyloadRouteHandler('./routes/dekudeals')); -// 直播吧 -router.get('/zhibo8/forum/:id', lazyloadRouteHandler('./routes/zhibo8/forum')); -router.get('/zhibo8/post/:id', lazyloadRouteHandler('./routes/zhibo8/post')); -router.get('/zhibo8/more/:caty', lazyloadRouteHandler('./routes/zhibo8/more')); +// 直播吧 migrated to v2 +// router.get('/zhibo8/forum/:id', lazyloadRouteHandler('./routes/zhibo8/forum')); +// router.get('/zhibo8/post/:id', lazyloadRouteHandler('./routes/zhibo8/post')); +// router.get('/zhibo8/more/:category?', lazyloadRouteHandler('./routes/zhibo8/more')); // 东方网-上海 router.get('/eastday/sh', lazyloadRouteHandler('./routes/eastday/sh')); diff --git a/lib/routes/zhibo8/more.js b/lib/routes/zhibo8/more.js deleted file mode 100644 index 14345a16c..000000000 --- a/lib/routes/zhibo8/more.js +++ /dev/null @@ -1,45 +0,0 @@ -const got = require('@/utils/got'); -const cheerio = require('cheerio'); - -module.exports = async (ctx) => { - const currentUrl = `https://news.zhibo8.cc/${ctx.params.caty}/more.htm`; - const response = await got({ - method: 'get', - url: currentUrl, - }); - const $ = cheerio.load(response.data); - const list = $('ul.articleList li') - .slice(0, 30) - .map((_, item) => { - item = $(item); - const a = item.find('a'); - - return { - title: a.text(), - link: `http:${a.attr('href')}`, - pubDate: new Date(item.find('span.postTime').text() + ' GMT+8').toUTCString(), - }; - }) - .get(); - - const items = await Promise.all( - list.map((item) => - ctx.cache.tryGet(item.link, async () => { - const res = await got({ - method: 'get', - url: item.link, - }); - const content = cheerio.load(res.data); - - item.description = content('div.content').html(); - return item; - }) - ) - ); - - ctx.state.data = { - title: `${$('span.title_g').text()} - 直播吧`, - link: currentUrl, - item: items, - }; -}; diff --git a/lib/routes/zhibo8/forum.js b/lib/v2/zhibo8/forum.js similarity index 62% rename from lib/routes/zhibo8/forum.js rename to lib/v2/zhibo8/forum.js index 5cbe46540..e63ebb5b0 100644 --- a/lib/routes/zhibo8/forum.js +++ b/lib/v2/zhibo8/forum.js @@ -1,5 +1,7 @@ const got = require('@/utils/got'); const cheerio = require('cheerio'); +const { parseDate } = require('@/utils/parse-date'); +const timezone = require('@/utils/timezone'); module.exports = async (ctx) => { const id = ctx.params.id; @@ -13,7 +15,7 @@ module.exports = async (ctx) => { const out = await Promise.all( list - .map(async (index, item) => { + .map(async (_, item) => { item = $(item); const title = item.find('td:nth-child(1) > a:nth-child(2)').text(); const author = item.find('td:nth-child(2) cite a').text(); @@ -24,25 +26,15 @@ module.exports = async (ctx) => { title, author, link, - pubDate: new Date(date).toUTCString(), + pubDate: timezone(parseDate(date, 'YYYY-MM-DD HH:mm'), +8), }; - const key = link; - const value = await ctx.cache.get(key); - if (value) { - single.description = value; - } else { + single.description = await ctx.cache.tryGet(link, async () => { const response = await got.get(link); const $ = cheerio.load(response.data); - $('.detail_ent img').each(function () { - const $img = $(this); - const src = $img.attr('src'); - $img.attr('src', 'https:' + src); - }); - single.description = $('.detail_ent').html(); - ctx.cache.set(key, single.description); - } - return Promise.resolve(single); + return $('.detail_ent').html(); + }); + return single; }) .get() ); diff --git a/lib/v2/zhibo8/maintainer.js b/lib/v2/zhibo8/maintainer.js new file mode 100644 index 000000000..5868c6346 --- /dev/null +++ b/lib/v2/zhibo8/maintainer.js @@ -0,0 +1,5 @@ +module.exports = { + '/forum/:id': ['LogicJake'], + '/more/:category?': ['nczitzk'], + '/post/:id': ['LogicJake'], +}; diff --git a/lib/v2/zhibo8/more.js b/lib/v2/zhibo8/more.js new file mode 100644 index 000000000..35787e77a --- /dev/null +++ b/lib/v2/zhibo8/more.js @@ -0,0 +1,82 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const timezone = require('@/utils/timezone'); +const { parseDate } = require('@/utils/parse-date'); + +const categories = { + nba: 'NBA', + zuqiu: '足球', + dianjing: '电竞', + other: '综合', +}; + +module.exports = async (ctx) => { + const category = ctx.params.category ?? 'nba'; + + const rootUrl = 'https://news.zhibo8.cc'; + + let list, + apiUrl = '', + currentUrl = '', + response; + + if (category === 'nba' || category === 'zuqiu') { + currentUrl = `${rootUrl}/${category}/more.htm`; + + response = await got({ + method: 'get', + url: currentUrl, + }); + + const $ = cheerio.load(response.data); + + list = $('ul.articleList li') + .slice(0, 30) + .map((_, item) => { + item = $(item); + const a = item.find('a'); + + return { + title: a.text(), + link: `http:${a.attr('href')}`, + pubDate: timezone(parseDate(item.find('span.postTime').text()), +8), + }; + }) + .get(); + } else { + currentUrl = `${rootUrl}/${category}`; + apiUrl = `https://api.qiumibao.com/application/app/index.php?_url=/news/${category}List`; + + response = await got({ + method: 'get', + url: apiUrl, + }); + + list = response.data.data.list.map((item) => ({ + title: item.title, + link: `https:${item.url}`, + pubDate: timezone(parseDate(item.createtime), +8), + })); + } + + const items = await Promise.all( + list.map((item) => + ctx.cache.tryGet(item.link, async () => { + const res = await got({ + method: 'get', + url: item.link, + }); + const content = cheerio.load(res.data); + + item.description = content('div.content').html(); + return item; + }) + ) + ); + + ctx.state.data = { + title: `${categories[category]} - 直播吧`, + link: currentUrl, + item: items, + }; +}; diff --git a/lib/routes/zhibo8/post.js b/lib/v2/zhibo8/post.js similarity index 95% rename from lib/routes/zhibo8/post.js rename to lib/v2/zhibo8/post.js index 57ac5d7cc..743a7a164 100644 --- a/lib/routes/zhibo8/post.js +++ b/lib/v2/zhibo8/post.js @@ -26,7 +26,7 @@ module.exports = async (ctx) => { guid: floor, }; - return Promise.resolve(single); + return single; }) .get() ); diff --git a/lib/v2/zhibo8/radar.js b/lib/v2/zhibo8/radar.js new file mode 100644 index 000000000..0123b7048 --- /dev/null +++ b/lib/v2/zhibo8/radar.js @@ -0,0 +1,27 @@ +module.exports = { + 'zhibo8.cc': { + _name: '直播吧', + bbs: [ + { + title: '子论坛', + docs: 'https://docs.rsshub.app/bbs.html#zhi-bo-ba', + source: ['/'], + target: (_params, url) => `/zhibo8/forum/${new URL(url).searchParams.get('fid')}`, + }, + { + title: '回帖', + docs: 'https://docs.rsshub.app/bbs.html#zhi-bo-ba', + source: ['/'], + target: (_params, url) => `/zhibo8/post/${new URL(url).searchParams.get('tid')}`, + }, + ], + news: [ + { + title: '滚动新闻', + docs: 'https://docs.rsshub.app/bbs.html#zhi-bo-ba', + source: ['/:category'], + target: '/zhibo8/more/:category', + }, + ], + }, +}; diff --git a/lib/v2/zhibo8/router.js b/lib/v2/zhibo8/router.js new file mode 100644 index 000000000..fbfce1c67 --- /dev/null +++ b/lib/v2/zhibo8/router.js @@ -0,0 +1,5 @@ +module.exports = (router) => { + router.get('/forum/:id', require('./forum')); + router.get('/more/:category?', require('./more')); + router.get('/post/:id', require('./post')); +};