From ea41fd8ece6f540354f0606e2ffe50ebffadc10d Mon Sep 17 00:00:00 2001
From: Ethan Shen <42264778+nczitzk@users.noreply.github.com>
Date: Mon, 10 Jul 2023 18:13:14 +0800
Subject: [PATCH] =?UTF-8?q?feat(route):=20add=20=E5=85=B6=E4=B9=90?=
=?UTF-8?q?=E8=AE=BA=E5=9D=9B=20(#12768)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
* feat(route): add 其乐论坛
* update lib/v2/keylol/radar.js
* fix: remove unnecessary type casting
---------
---
docs/game.md | 16 ++++++++
lib/v2/keylol/index.js | 82 +++++++++++++++++++++++++++++++++++++
lib/v2/keylol/maintainer.js | 3 ++
lib/v2/keylol/radar.js | 18 ++++++++
lib/v2/keylol/router.js | 3 ++
5 files changed, 122 insertions(+)
create mode 100644 lib/v2/keylol/index.js
create mode 100644 lib/v2/keylol/maintainer.js
create mode 100644 lib/v2/keylol/radar.js
create mode 100644 lib/v2/keylol/router.js
diff --git a/docs/game.md b/docs/game.md
index bca386100..407082e22 100644
--- a/docs/game.md
+++ b/docs/game.md
@@ -775,6 +775,22 @@ Example:`https://www.iyingdi.com/tz/people/55547` ,id 是 `55547`
+## 其乐
+
+### 论坛
+
+
+
+::: tip 提示
+
+若订阅 [热点聚焦](https://keylol.com/f161-1),网址为 。截取 `https://keylol.com/` 到末尾的部分 `f161-1` 作为参数,此时路由为 [`/keylol/f161-1`](https://rsshub.app/keylol/f161-1)。
+
+若订阅子分类 [试玩免费 - 热点聚焦](https://keylol.com/forum.php?mod=forumdisplay&fid=161&filter=typeid&typeid=459),网址为 。截取 `https://keylol.com/forum.php?mod=forumdisplay&` 到末尾的部分 `fid=161&filter=typeid&typeid=459` 作为参数,此时路由为 [`/keylol/fid=161&filter=typeid&typeid=459`](https://rsshub.app/keylol/fid=161&filter=typeid&typeid=459)。
+
+:::
+
+
+
## 少女前线
### 情报局
diff --git a/lib/v2/keylol/index.js b/lib/v2/keylol/index.js
new file mode 100644
index 000000000..1479380b2
--- /dev/null
+++ b/lib/v2/keylol/index.js
@@ -0,0 +1,82 @@
+const got = require('@/utils/got');
+const cheerio = require('cheerio');
+const timezone = require('@/utils/timezone');
+const { parseDate } = require('@/utils/parse-date');
+
+module.exports = async (ctx) => {
+ let thePath = ctx.path.replace(/^\//, '');
+
+ if (/^f\d+-\d+/.test(thePath)) {
+ thePath = `fid=${thePath.match(/^f(\d+)-\d+/)[1]}`;
+ }
+
+ const limit = ctx.query.limit ? parseInt(ctx.query.limit, 10) : 30;
+
+ const rootUrl = 'https://keylol.com';
+ const currentUrl = new URL(`forum.php?mod=forumdisplay&${thePath.replace(/mod=\w+&/g, '')}`, rootUrl).href;
+
+ const { data: response } = await got(currentUrl);
+
+ const $ = cheerio.load(response);
+
+ let items = $('a.xst')
+ .slice(0, limit)
+ .toArray()
+ .map((item) => {
+ item = $(item);
+
+ return {
+ title: item.text(),
+ link: new URL(item.prop('href').split('&extra=')[0], rootUrl).href,
+ };
+ });
+
+ items = await Promise.all(
+ items.map((item) =>
+ ctx.cache.tryGet(item.link, async () => {
+ const { data: detailResponse } = await got(item.link);
+
+ const content = cheerio.load(detailResponse);
+
+ item.description = content('td.t_f').html();
+ item.author = content('a.xw1').first().text();
+ item.category = content('#keyloL_thread_tags a')
+ .toArray()
+ .map((c) => content(c).text());
+
+ const pubDateEm = content('img.authicn').first().next();
+ const pubDateText = pubDateEm.find('span').prop('title') ?? pubDateEm.text();
+ const pubDateMatches = pubDateText.match(/(\d{4}-\d{1,2}-\d{1,2} \d{2}:\d{2}:\d{2})/) ?? undefined;
+ if (pubDateMatches) {
+ item.pubDate = timezone(parseDate(pubDateMatches[1], 'YYYY-M-D HH:mm:ss'), +8);
+ }
+
+ const updatedMatches =
+ content('i.pstatus')
+ .text()
+ .match(/(\d{4}-\d{1,2}-\d{1,2} \d{2}:\d{2}:\d{2})/) ?? undefined;
+ if (updatedMatches) {
+ item.updated = timezone(parseDate(updatedMatches[1], 'YYYY-M-D HH:mm:ss'), +8);
+ }
+
+ item.comments = content('div.subforum_right_title_left_down').text() ? parseInt(content('div.subforum_right_title_left_down').text(), 10) : 0;
+
+ return item;
+ })
+ )
+ );
+
+ const icon = $('link[rel="apple-touch-icon"]').prop('href');
+
+ ctx.state.data = {
+ item: items,
+ title: $('title').text(),
+ link: currentUrl,
+ description: $('meta[name="description"]').prop('content'),
+ language: 'zh-cn',
+ icon,
+ logo: icon,
+ subtitle: $('meta[name="application-name"]').prop('content'),
+ author: $('meta[name="author"]').prop('content'),
+ };
+};
diff --git a/lib/v2/keylol/maintainer.js b/lib/v2/keylol/maintainer.js
new file mode 100644
index 000000000..57f872453
--- /dev/null
+++ b/lib/v2/keylol/maintainer.js
@@ -0,0 +1,3 @@
+module.exports = {
+ '/:path+': ['nczitzk'],
+};
diff --git a/lib/v2/keylol/radar.js b/lib/v2/keylol/radar.js
new file mode 100644
index 000000000..b3c812dd3
--- /dev/null
+++ b/lib/v2/keylol/radar.js
@@ -0,0 +1,18 @@
+module.exports = {
+ 'keylol.com': {
+ _name: '其乐',
+ '.': [
+ {
+ title: '论坛',
+ docs: 'https://docs.rsshub.app/game.html#qi-le-lun-tan',
+ source: ['/:category', '/'],
+ target: (params, url) => {
+ url = new URL(url);
+ const path = url.href.match(/keylol\.com\/(forum.php\?.*|f\d+-\d+)?/).replace(/forum.php\?/, '')[1];
+
+ return `/keylol${path ? `/${path}` : ''}`;
+ },
+ },
+ ],
+ },
+};
diff --git a/lib/v2/keylol/router.js b/lib/v2/keylol/router.js
new file mode 100644
index 000000000..4443c20f6
--- /dev/null
+++ b/lib/v2/keylol/router.js
@@ -0,0 +1,3 @@
+module.exports = function (router) {
+ router.get(/([\w-/]+)?/, require('./'));
+};