feat: add 巴哈姆特電玩資訊站熱門推薦 (#5826)

Co-authored-by: Henry Wang <hi@henry.wang>
This commit is contained in:
Ethan Shen 2020-10-10 04:40:22 +08:00 committed by GitHub
parent e8b4980d46
commit ad6135b0d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 0 deletions

View File

@ -178,6 +178,12 @@ pageClass: routes
<Route author="liyefox" example="/v2ex/tab/hot" path="/v2ex/tab/:tabid" :paramsDesc="['tab标签ID,在 URL 可以找到']"/>
## 巴哈姆特電玩資訊站
### 熱門推薦
<Route author="nczitzk" example="/gamer/hot/47157" path="/gamer/hot/:bsn" :paramsDesc="['板块 id在 URL 可以找到']"/>
## 才符
### 用户动态

View File

@ -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'));

49
lib/routes/gamer/hot.js Normal file
View File

@ -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,
};
};