From b1a20c3a17ce03ae2ae63ce35934ae4f5dc58e44 Mon Sep 17 00:00:00 2001 From: Tony Date: Sun, 23 Jan 2022 03:02:01 +0700 Subject: [PATCH] fix(route): fix gamer anti crawl (#8678) --- lib/router.js | 4 ++-- lib/{routes => v2}/gamer/hot.js | 19 +++++++++++++++---- lib/v2/gamer/maintainer.js | 3 +++ lib/v2/gamer/radar.js | 13 +++++++++++++ lib/v2/gamer/router.js | 3 +++ 5 files changed, 36 insertions(+), 6 deletions(-) rename lib/{routes => v2}/gamer/hot.js (72%) create mode 100644 lib/v2/gamer/maintainer.js create mode 100644 lib/v2/gamer/radar.js create mode 100644 lib/v2/gamer/router.js diff --git a/lib/router.js b/lib/router.js index d7aa5d44c7..49724e7bd1 100644 --- a/lib/router.js +++ b/lib/router.js @@ -3393,8 +3393,8 @@ router.get('/appsales/:caty?/:time?', lazyloadRouteHandler('./routes/appsales/in // Academy of Management router.get('/aom/journal/:id', lazyloadRouteHandler('./routes/aom/journal')); -// 巴哈姆特電玩資訊站 -router.get('/gamer/hot/:bsn', lazyloadRouteHandler('./routes/gamer/hot')); +// 巴哈姆特電玩資訊站 migrated to v2 +// router.get('/gamer/hot/:bsn', lazyloadRouteHandler('./routes/gamer/hot')); // iCity router.get('/icity/:id', lazyloadRouteHandler('./routes/icity/index')); diff --git a/lib/routes/gamer/hot.js b/lib/v2/gamer/hot.js similarity index 72% rename from lib/routes/gamer/hot.js rename to lib/v2/gamer/hot.js index 86229457c8..c55fabdc18 100644 --- a/lib/routes/gamer/hot.js +++ b/lib/v2/gamer/hot.js @@ -1,16 +1,19 @@ 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 rootUrl = `https://forum.gamer.com.tw/A.php?bsn=${ctx.params.bsn}`; const response = await got({ - method: 'get', url: rootUrl, + header: { + Referer: 'https://forum.gamer.com.tw', + }, }); const $ = cheerio.load(response.data); const list = $('div.FM-abox2A a.FM-abox2B') - .slice(0, 10) .map((_, item) => { item = $(item); return { @@ -23,8 +26,10 @@ module.exports = async (ctx) => { list.map((item) => ctx.cache.tryGet(item.link, async () => { const detailResponse = await got({ - method: 'get', url: item.link, + headers: { + Referer: rootUrl, + }, }); const content = cheerio.load(detailResponse.data); @@ -33,7 +38,7 @@ module.exports = async (ctx) => { item.title = content('.c-post__header__title').text(); item.description = content('div.c-post__body').html(); item.author = `${content('a.username').eq(0).text()} (${content('a.userid').eq(0).text()})`; - item.pubDate = new Date(content('a.edittime').eq(0).attr('data-mtime') + ' GMT+8').toUTCString(); + item.pubDate = timezone(parseDate(content('a.edittime').eq(0).attr('data-mtime'), +8)); return item; }) @@ -45,4 +50,10 @@ module.exports = async (ctx) => { link: rootUrl, item: items, }; + + ctx.state.json = { + title: $('title').text(), + link: rootUrl, + item: items, + }; }; diff --git a/lib/v2/gamer/maintainer.js b/lib/v2/gamer/maintainer.js new file mode 100644 index 0000000000..cbec4d6d1f --- /dev/null +++ b/lib/v2/gamer/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/hot/:bsn': ['nczitzk', 'TonyRL'], +}; diff --git a/lib/v2/gamer/radar.js b/lib/v2/gamer/radar.js new file mode 100644 index 0000000000..a5f23834d1 --- /dev/null +++ b/lib/v2/gamer/radar.js @@ -0,0 +1,13 @@ +module.exports = { + 'gamer.com.tw': { + _name: '巴哈姆特電玩資訊站', + forum: [ + { + title: '熱門推薦', + docs: 'https://docs.rsshub.app/bbs.html#ba-ha-mu-te-dian-wan-zi-xun-zhan', + source: ['/A.php', '/B.php'], + target: (params, url) => `/gamer/hot/${new URL(url).searchParams.get('bsn')}`, + }, + ], + }, +}; diff --git a/lib/v2/gamer/router.js b/lib/v2/gamer/router.js new file mode 100644 index 0000000000..878e3f400c --- /dev/null +++ b/lib/v2/gamer/router.js @@ -0,0 +1,3 @@ +module.exports = function (router) { + router.get('/hot/:bsn', require('./hot')); +};