diff --git a/docs/bbs.md b/docs/bbs.md index 7658adf74..ea5e48ad0 100644 --- a/docs/bbs.md +++ b/docs/bbs.md @@ -178,6 +178,12 @@ pageClass: routes +## 巴哈姆特電玩資訊站 + +### 熱門推薦 + + + ## 才符 ### 用户动态 diff --git a/lib/router.js b/lib/router.js index 8201ef675..d2b146196 100644 --- a/lib/router.js +++ b/lib/router.js @@ -3260,6 +3260,9 @@ router.get('/appsales/:caty?/:time?', require('./routes/appsales/index')); // CGTN router.get('/cgtn/top', require('./routes/cgtn/top')); +// 巴哈姆特電玩資訊站 +router.get('/gamer/hot/:bsn', require('./routes/gamer/hot')); + // iCity router.get('/icity/:id', require('./routes/icity/index')); diff --git a/lib/routes/gamer/hot.js b/lib/routes/gamer/hot.js new file mode 100644 index 000000000..55be88194 --- /dev/null +++ b/lib/routes/gamer/hot.js @@ -0,0 +1,49 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +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, + }); + + const $ = cheerio.load(response.data); + const list = $('div.FM-abox2A a.FM-abox2B') + .slice(0, 10) + .map((_, item) => { + item = $(item); + return { + link: `https:${item.attr('href')}`, + }; + }) + .get(); + + const items = await Promise.all( + list.map( + async (item) => + await ctx.cache.tryGet(item.link, async () => { + const detailResponse = await got({ + method: 'get', + url: item.link, + }); + const content = cheerio.load(detailResponse.data); + + content('div.c-post__body__buttonbar').remove(); + + 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(); + + return item; + }) + ) + ); + + ctx.state.data = { + title: $('title').text(), + link: rootUrl, + item: items, + }; +};