From ece1350254f9a0f2e8b56db8667489bdd0ded46c Mon Sep 17 00:00:00 2001
From: Ethan Shen <42264778+nczitzk@users.noreply.github.com>
Date: Tue, 14 Mar 2023 00:22:42 +0800
Subject: [PATCH] =?UTF-8?q?feat(route):=20add=20Gamebase=20=E6=96=B0?=
=?UTF-8?q?=E8=81=9E=20(#12091)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
* feat(route): add Gamebase 新聞
* apply suggestions from code review
---------
---
docs/game.md | 23 ++++++++---
lib/v2/gamebase/maintainer.js | 3 ++
lib/v2/gamebase/news.js | 74 +++++++++++++++++++++++++++++++++++
lib/v2/gamebase/radar.js | 13 ++++++
lib/v2/gamebase/router.js | 3 ++
5 files changed, 111 insertions(+), 5 deletions(-)
create mode 100644 lib/v2/gamebase/maintainer.js
create mode 100644 lib/v2/gamebase/news.js
create mode 100644 lib/v2/gamebase/radar.js
create mode 100644 lib/v2/gamebase/router.js
diff --git a/docs/game.md b/docs/game.md
index e3d7f32de..dfba566ca 100644
--- a/docs/game.md
+++ b/docs/game.md
@@ -109,6 +109,19 @@ pageClass: routes
+## 遊戲基地 Gamebase
+
+### 新聞
+
+
+
+类型
+
+| newslist | r18list |
+| -------- | ------- |
+
+
+
## Gamer Secret
### 最新資訊
@@ -236,9 +249,9 @@ pageClass: routes
### 游戏折扣
-| 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) 模组包更新
-| 参数 | 说明 |
-| ------| ------------ |
+| 参数 | 说明 |
+| ------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------- |
| modpackEntry | 模组包的短名从模组包的页面链接中找到,例如 `https://www.feed-the-beast.com/modpack/ftb_presents_direwolf20_1_16`,短名就是 `ftb_presents_direwolf20_1_16`。 |
diff --git a/lib/v2/gamebase/maintainer.js b/lib/v2/gamebase/maintainer.js
new file mode 100644
index 000000000..aa6c6618a
--- /dev/null
+++ b/lib/v2/gamebase/maintainer.js
@@ -0,0 +1,3 @@
+module.exports = {
+ '/news/:type?/:category?': ['nczitzk'],
+};
diff --git a/lib/v2/gamebase/news.js b/lib/v2/gamebase/news.js
new file mode 100644
index 000000000..629d5e4c9
--- /dev/null
+++ b/lib/v2/gamebase/news.js
@@ -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,
+ };
+};
diff --git a/lib/v2/gamebase/radar.js b/lib/v2/gamebase/radar.js
new file mode 100644
index 000000000..17a8eff10
--- /dev/null
+++ b/lib/v2/gamebase/radar.js
@@ -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')}`,
+ },
+ ],
+ },
+};
diff --git a/lib/v2/gamebase/router.js b/lib/v2/gamebase/router.js
new file mode 100644
index 000000000..8b0471905
--- /dev/null
+++ b/lib/v2/gamebase/router.js
@@ -0,0 +1,3 @@
+module.exports = function (router) {
+ router.get('/news/:type?/:category?', require('./news'));
+};