Skip to content

Commit

Permalink
feat(route): add Gamebase 新聞 (DIYgod#12091)
Browse files Browse the repository at this point in the history
* feat(route): add Gamebase 新聞

* apply suggestions from code review
---------
  • Loading branch information
nczitzk authored Mar 13, 2023
1 parent 633aec3 commit ece1350
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 5 deletions.
23 changes: 18 additions & 5 deletions docs/game.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

### 最新資訊
Expand Down Expand Up @@ -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 |
| ------ | ------ | --- | --- | ----- |
Expand Down Expand Up @@ -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>

Expand Down
3 changes: 3 additions & 0 deletions lib/v2/gamebase/maintainer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
'/news/:type?/:category?': ['nczitzk'],
};
74 changes: 74 additions & 0 deletions lib/v2/gamebase/news.js
Original file line number Diff line number Diff line change
@@ -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 changes: 13 additions & 0 deletions lib/v2/gamebase/radar.js
Original file line number Diff line number Diff line change
@@ -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')}`,
},
],
},
};
3 changes: 3 additions & 0 deletions lib/v2/gamebase/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function (router) {
router.get('/news/:type?/:category?', require('./news'));
};

0 comments on commit ece1350

Please sign in to comment.