From 67fe3a0cfab71f2f171234df7187a7f9c398bd4c Mon Sep 17 00:00:00 2001 From: Axojhf <20625305+axojhf@users.noreply.github.com> Date: Mon, 26 Sep 2022 20:52:50 +0800 Subject: [PATCH] fix(route): Fix saraba1st thread cannot get image (#10906) * add saraba1st Cookie Environment. fix thread img issue * add Saraba1st config to Install Doc * fix imgHtml * fix timezone * refactor: migrate to v2 --- assets/radar-rules.js | 14 -------- docs/install/README.md | 4 +++ lib/config.js | 3 ++ lib/radar-rules.js | 14 -------- lib/router.js | 2 +- lib/routes/saraba1st/thread.js | 45 ------------------------ lib/v2/saraba1st/maintainer.js | 3 ++ lib/v2/saraba1st/radar.js | 16 +++++++++ lib/v2/saraba1st/router.js | 3 ++ lib/v2/saraba1st/thread.js | 63 ++++++++++++++++++++++++++++++++++ 10 files changed, 93 insertions(+), 74 deletions(-) delete mode 100644 lib/routes/saraba1st/thread.js create mode 100644 lib/v2/saraba1st/maintainer.js create mode 100644 lib/v2/saraba1st/radar.js create mode 100644 lib/v2/saraba1st/router.js create mode 100644 lib/v2/saraba1st/thread.js diff --git a/assets/radar-rules.js b/assets/radar-rules.js index 4aac6e97a..bf9dec046 100644 --- a/assets/radar-rules.js +++ b/assets/radar-rules.js @@ -728,20 +728,6 @@ }, 'huya.com': { _name: '虎牙直播', '.': [{ title: '直播间开播', docs: 'https://docs.rsshub.app/live.html#hu-ya-zhi-bo-zhi-bo-jian-kai-bo', source: '/:id', target: '/huya/live/:id' }] }, 'craigslist.org': { _name: 'Craigslist', '.': [{ title: '商品搜索列表', docs: 'https://docs.rsshub.app/shopping.html#craigslist' }] }, - 'saraba1st.com': { - _name: 'Saraba1st', - bbs: [ - { - title: '帖子', - docs: 'https://docs.rsshub.app/bbs.html#saraba1st', - source: '/2b/:id', - target: (params) => { - const id = params.id.includes('thread') ? params.id.split('-')[1] : ''; - return id ? `/saraba1st/thread/${id}` : ''; - }, - }, - ], - }, 'scboy.com': { _name: 'scboy 论坛', www: [ diff --git a/docs/install/README.md b/docs/install/README.md index 30debc28a..4f6291522 100644 --- a/docs/install/README.md +++ b/docs/install/README.md @@ -717,6 +717,10 @@ RSSHub 支持使用访问密钥 / 码,白名单和黑名单三种方式进行 - `FANBOX_SESSION_ID`: 对应 cookies 中的`FANBOXSESSID`。 +- Saraba1st 用于获取帖子里的图片 + + - `SARABA1ST_COOKIE`: 对应网页端的Cookie。 + - Sci-hub 设置,用于科学期刊路由。 - `SCIHUB_HOST`: 可访问的 sci-hub 镜像地址,默认为 `https://sci-hub.se`。 diff --git a/lib/config.js b/lib/config.js index 75cb6f95d..706af92d2 100644 --- a/lib/config.js +++ b/lib/config.js @@ -227,6 +227,9 @@ const calculateValue = () => { pkubbs: { cookie: envs.PKUBBS_COOKIE, }, + saraba1st: { + cookie: envs.SARABA1ST_COOKIE, + }, scboy: { token: envs.SCBOY_BBS_TOKEN, }, diff --git a/lib/radar-rules.js b/lib/radar-rules.js index 770e01b0e..d020b6b0b 100644 --- a/lib/radar-rules.js +++ b/lib/radar-rules.js @@ -1516,20 +1516,6 @@ module.exports = { }, ], }, - 'saraba1st.com': { - _name: 'Saraba1st', - bbs: [ - { - title: '帖子', - docs: 'https://docs.rsshub.app/bbs.html#saraba1st', - source: '/2b/:id', - target: (params) => { - const id = params.id.includes('thread') ? params.id.split('-')[1] : ''; - return id ? `/saraba1st/thread/${id}` : ''; - }, - }, - ], - }, 'scboy.com': { _name: 'scboy 论坛', www: [ diff --git a/lib/router.js b/lib/router.js index 43470dd9f..6fcf93432 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1561,7 +1561,7 @@ router.get('/mit/csail/news', lazyloadRouteHandler('./routes/universities/mit/cs router.get('/kpmg/insights', lazyloadRouteHandler('./routes/kpmg/insights')); // Saraba1st -router.get('/saraba1st/thread/:tid', lazyloadRouteHandler('./routes/saraba1st/thread')); +// router.get('/saraba1st/thread/:tid', lazyloadRouteHandler('./routes/saraba1st/thread')); // gradcafe router.get('/gradcafe/result/:type', lazyloadRouteHandler('./routes/gradcafe/result')); diff --git a/lib/routes/saraba1st/thread.js b/lib/routes/saraba1st/thread.js deleted file mode 100644 index f3a03cba3..000000000 --- a/lib/routes/saraba1st/thread.js +++ /dev/null @@ -1,45 +0,0 @@ -const url = require('url'); -const got = require('@/utils/got'); -const cheerio = require('cheerio'); -const date = require('@/utils/date'); -const queryString = require('query-string'); - -module.exports = async (ctx) => { - const tid = ctx.params.tid; - - const res = await got.get('https://bbs.saraba1st.com/2b/forum.php', { - searchParams: queryString.stringify({ - mod: 'viewthread', - tid, - ordertype: 1, - }), - }); - - const $ = cheerio.load(res.data); - const title = $('#thread_subject').text(); - - const list = $('#postlist > div[id^=post_]:not(:first-of-type)'); - const count = []; - - for (let i = 0; i < Math.min(list.length, 10); i++) { - count.push(i); - } - const resultItems = count.map((i) => { - const each = $(list[i]); - const floor = each.find('td.plc .pi a > em').text(); - const floorUrl = each.find('td.plc .pi a').attr('href'); - return { - title: `${title} #${floor}`, - link: url.resolve('https://bbs.saraba1st.com/2b/', floorUrl), - description: each.find('td.t_f').html(), - author: each.find('.authi .xw1').text(), - pubDate: date(each.find('.authi em').text()), - }; - }); - - ctx.state.data = { - title: `Stage1 论坛 - ${title}`, - link: `https://bbs.saraba1st.com/2b/thread-${tid}-1-1.html`, - item: resultItems, - }; -}; diff --git a/lib/v2/saraba1st/maintainer.js b/lib/v2/saraba1st/maintainer.js new file mode 100644 index 000000000..18d2cc42a --- /dev/null +++ b/lib/v2/saraba1st/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/thread/:tid': ['zengxs'], +}; diff --git a/lib/v2/saraba1st/radar.js b/lib/v2/saraba1st/radar.js new file mode 100644 index 000000000..cc9170769 --- /dev/null +++ b/lib/v2/saraba1st/radar.js @@ -0,0 +1,16 @@ +module.exports = { + 'saraba1st.com': { + _name: 'Saraba1st', + bbs: [ + { + title: '帖子', + docs: 'https://docs.rsshub.app/bbs.html#saraba1st', + source: '/2b/:id', + target: (params) => { + const id = params.id.includes('thread') ? params.id.split('-')[1] : ''; + return id ? `/saraba1st/thread/${id}` : ''; + }, + }, + ], + }, +}; diff --git a/lib/v2/saraba1st/router.js b/lib/v2/saraba1st/router.js new file mode 100644 index 000000000..16c10606b --- /dev/null +++ b/lib/v2/saraba1st/router.js @@ -0,0 +1,3 @@ +module.exports = (router) => { + router.get('/thread/:tid', require('./thread')); +}; diff --git a/lib/v2/saraba1st/thread.js b/lib/v2/saraba1st/thread.js new file mode 100644 index 000000000..a7ae2875c --- /dev/null +++ b/lib/v2/saraba1st/thread.js @@ -0,0 +1,63 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const queryString = require('query-string'); +const config = require('@/config').value; +const { parseDate } = require('@/utils/parse-date'); +const timezone = require('@/utils/timezone'); + +module.exports = async (ctx) => { + const tid = ctx.params.tid; + const cookieString = config.saraba1st.cookie ? config.saraba1st.cookie : ''; + + const res = await got('https://bbs.saraba1st.com/2b/forum.php', { + searchParams: queryString.stringify({ + mod: 'viewthread', + tid, + ordertype: 1, + }), + headers: { + Cookie: cookieString, + }, + }); + + const $ = cheerio.load(res.data); + const title = $('#thread_subject').text(); + + const list = $('#postlist > div[id^=post_]:not(:first-of-type)'); + const count = []; + + for (let i = 0; i < Math.min(list.length, 20); i++) { + count.push(i); + } + const resultItems = count.map((i) => { + const each = $(list[i]); + const floor = each.find('td.plc .pi a > em').text(); + const floorUrl = each.find('td.plc .pi a').attr('href'); + const contentHtml = $(each.find('td.t_f')); + const imgsHtml = contentHtml.find('img'); + for (let i = 0; i < imgsHtml.length; i++) { + if (imgsHtml[i].attribs.src === 'https://static.saraba1st.com/image/common/none.gif') { + imgsHtml[i].attribs.src = imgsHtml[i].attribs.file; + const imgHtml = $(imgsHtml[i]); + imgHtml.removeAttr('zoomfile'); + imgHtml.removeAttr('file'); + imgHtml.removeAttr('onmouseover'); + imgHtml.removeAttr('onclick'); + } + } + contentHtml.find('div.aimg_tip').remove(); + return { + title: `${title} #${floor}`, + link: new URL(floorUrl, 'https://bbs.saraba1st.com/2b/').href, + description: contentHtml.html(), + author: each.find('.authi .xw1').text(), + pubDate: timezone(parseDate(each.find('.authi em').text()), +8), + }; + }); + + ctx.state.data = { + title: `Stage1 论坛 - ${title}`, + link: `https://bbs.saraba1st.com/2b/thread-${tid}-1-1.html`, + item: resultItems, + }; +};