feat(route): add Gamebase 新聞 (#12091)

* feat(route): add Gamebase 新聞

* apply suggestions from code review
---------
This commit is contained in:
Ethan Shen 2023-03-14 00:22:42 +08:00 committed by GitHub
parent 633aec3dac
commit ece1350254
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 111 additions and 5 deletions

View File

@ -109,6 +109,19 @@ pageClass: routes
<Route author="TonyRL" example="/gameapps" path="/gameapps"/>
## 遊戲基地 Gamebase
### 新聞
<Route author="nczitzk" example="/gamebase/news" path="/gamebase/news/:type?/:category?" :paramsDesc="['类型,见下表,默认为 newslist', '分类,可在对应分类页 URL 中找到,默认为 `all` 即全部']">
类型
| newslist | r18list |
| -------- | ------- |
</Route>
## Gamer Secret
### 最新資訊
@ -236,9 +249,9 @@ pageClass: routes
### 游戏折扣
<Route author="zytomorrow" path="/jump/discount/:platform/:filter?/:countries?" example="/jump/discount/ps5/all" :paramsDesc="['平台:switch,ps4,ps5,xbox,steam,epic', '过滤参数,all-全部jx-精选sd-史低dl-独立vip-会员', '地区,具体支持较多,可自信查看地区简写']">
| switch | ps4 | ps5 | xbox | steam | epic |
| ------ | --- | ---- | ---- | ---- | ---- |
| 可用 | 可用 | 可用 | 不可用 | 可用 | 不可用 |
| switch | ps4 | ps5 | xbox | steam | epic |
| ------ | ---- | ---- | ------ | ----- | ------ |
| 可用 | 可用 | 可用 | 不可用 | 可用 | 不可用 |
| filter | switch | ps4 | ps5 | steam |
| ------ | ------ | --- | --- | ----- |
@ -311,8 +324,8 @@ pageClass: routes
### Feed The Beast (FTB) 模组包更新
<Route author="gucheen" example="/feed-the-beast/modpack/ftb_presents_direwolf20_1_16" path="/feed-the-beast/modpack/:modpackEntry" :paramsDesc="['模组包的短名.']">
| 参数 | 说明 |
| ------| ------------ |
| 参数 | 说明 |
| ------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------- |
| modpackEntry | 模组包的短名从模组包的页面链接中找到,例如 `https://www.feed-the-beast.com/modpack/ftb_presents_direwolf20_1_16`,短名就是 `ftb_presents_direwolf20_1_16`。 |
</Route>

View File

@ -0,0 +1,3 @@
module.exports = {
'/news/:type?/:category?': ['nczitzk'],
};

74
lib/v2/gamebase/news.js Normal file
View File

@ -0,0 +1,74 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const timezone = require('@/utils/timezone');
const { parseDate } = require('@/utils/parse-date');
const types = {
newslist: 'newsList',
r18list: 'newsPornList',
};
module.exports = async (ctx) => {
const type = ctx.params.type ?? 'newslist';
const category = ctx.params.category ?? 'all';
const limit = ctx.query.limit ? parseInt(ctx.query.limit) : 20;
const rootUrl = 'https://news.gamebase.com.tw';
const currentUrl = `${rootUrl}/news/${type}?type=${category}`;
const apiRootUrl = 'https://api.gamebase.com.tw';
const apiUrl = `${apiRootUrl}/api/news/getNewsList`;
const response = await got({
method: 'post',
url: apiUrl,
json: {
GB_type: types[type],
category,
page: 1,
},
});
const titleResponse = await got({
method: 'get',
url: currentUrl,
});
const $ = cheerio.load(titleResponse.data);
const items = await Promise.all(
response.data.return_msg.list.slice(0, limit).map((item) =>
ctx.cache.tryGet(`gamebase:news:${type}:${category}:${item.news_no}`, async () => {
const i = {};
i.author = item.nickname;
i.title = item.news_title;
i.link = `${rootUrl}/news/detail/${item.news_no}`;
i.description = item.news_meta?.meta_des ?? '';
i.pubDate = timezone(parseDate(item.post_time), +8);
i.category = [item.system];
if (i.description) {
return i;
}
const detailResponse = await got({
method: 'get',
url: i.link,
});
const description = detailResponse.data.match(/(\\u003C.*?)","/)[1].replace(/\\"/g, '"');
i.description = description.replace(/\\u[\dA-F]{4}/gi, (match) => String.fromCharCode(parseInt(match.replace(/\\u/g, ''), 16)));
return i;
})
)
);
ctx.state.data = {
title: $('title').text(),
link: currentUrl,
item: items,
};
};

13
lib/v2/gamebase/radar.js Normal file
View File

@ -0,0 +1,13 @@
module.exports = {
'gamebase.com.tw': {
_name: '遊戲基地 Gamebase',
news: [
{
title: '新聞',
docs: 'https://docs.rsshub.app/game.html#gamebase-xin-wen',
source: ['/news/:type'],
target: (params, url) => `/gamebase/news/${params.type}/${new URL(url).searchParams.get('type')}`,
},
],
},
};

View File

@ -0,0 +1,3 @@
module.exports = function (router) {
router.get('/news/:type?/:category?', require('./news'));
};