From 4be123c139130a4cb7f96bf1f46a8bb606a8c728 Mon Sep 17 00:00:00 2001
From: syrinka <61999173+syrinka@users.noreply.github.com>
Date: Mon, 18 Jul 2022 21:29:35 +0800
Subject: [PATCH] =?UTF-8?q?feat(route):=20NGA=E5=B8=96=E5=AD=90=E5=8F=AA?=
=?UTF-8?q?=E7=9C=8B=E4=BD=9C=E8=80=85=20(#10239)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
* feat(route): NGA帖子只看作者
* fix code style
* fix
* remove old router
* correct regex in getLastPageId
* fix
* change title
* refactor: parseDate utils
* fix: old radar rules
---
assets/radar-rules.js | 7 ------
docs/bbs.md | 2 +-
lib/radar-rules.js | 17 -------------
lib/router.js | 6 ++---
lib/{routes => v2}/nga/forum.js | 17 +++++++------
lib/v2/nga/maintainer.js | 4 ++++
lib/{routes => v2}/nga/post.js | 31 ++++++++++++------------
lib/v2/nga/radar.js | 42 +++++++++++++++++++++++++++++++++
lib/v2/nga/router.js | 4 ++++
9 files changed, 78 insertions(+), 52 deletions(-)
rename lib/{routes => v2}/nga/forum.js (80%)
create mode 100644 lib/v2/nga/maintainer.js
rename lib/{routes => v2}/nga/post.js (63%)
create mode 100644 lib/v2/nga/radar.js
create mode 100644 lib/v2/nga/router.js
diff --git a/assets/radar-rules.js b/assets/radar-rules.js
index 5f8587a7e..6fe1d1a96 100644
--- a/assets/radar-rules.js
+++ b/assets/radar-rules.js
@@ -215,13 +215,6 @@
},
'wineyun.com': { _name: '酒云网', www: [{ title: '最新商品', docs: 'https://docs.rsshub.app/other.html#jiu-yun-wang', source: ['/:category'], target: '/wineyun/:category' }] },
'epicgames.com': { _name: 'Epic Games', www: [{ title: '每周免费游戏', docs: 'https://docs.rsshub.app/game.html#epicgames-freegame', source: '/store/zh-CN/free-games', target: '/epicgames/freegames' }] },
- 'nga.cn': {
- _name: 'NGA',
- bbs: [
- { title: '分区帖子', docs: 'https://docs.rsshub.app/bbs.html#nga', source: '/thread.php', target: (params, url) => new URL(url).searchParams.get('fid') && `/nga/forum/${new URL(url).searchParams.get('fid')}` },
- { title: '帖子', docs: 'https://docs.rsshub.app/bbs.html#nga', source: '/read.php', target: (params, url) => new URL(url).searchParams.get('tid') && `/nga/post/${new URL(url).searchParams.get('tid')}` },
- ],
- },
'playstation.com': {
_name: 'PlayStation',
store: [
diff --git a/docs/bbs.md b/docs/bbs.md
index 5a6f8d1a5..299b692eb 100644
--- a/docs/bbs.md
+++ b/docs/bbs.md
@@ -284,7 +284,7 @@ pageClass: routes
### 帖子
-
+
## PLAYNO.1 玩樂達人
diff --git a/lib/radar-rules.js b/lib/radar-rules.js
index 11d034e6b..01b0862bb 100644
--- a/lib/radar-rules.js
+++ b/lib/radar-rules.js
@@ -369,23 +369,6 @@ module.exports = {
},
],
},
- 'nga.cn': {
- _name: 'NGA',
- bbs: [
- {
- title: '分区帖子',
- docs: 'https://docs.rsshub.app/bbs.html#nga',
- source: '/thread.php',
- target: (params, url) => new URL(url).searchParams.get('fid') && `/nga/forum/${new URL(url).searchParams.get('fid')}`,
- },
- {
- title: '帖子',
- docs: 'https://docs.rsshub.app/bbs.html#nga',
- source: '/read.php',
- target: (params, url) => new URL(url).searchParams.get('tid') && `/nga/post/${new URL(url).searchParams.get('tid')}`,
- },
- ],
- },
'playstation.com': {
_name: 'PlayStation',
store: [
diff --git a/lib/router.js b/lib/router.js
index 704752882..6fa07be24 100644
--- a/lib/router.js
+++ b/lib/router.js
@@ -1060,9 +1060,9 @@ router.get('/coolbuy/newest', lazyloadRouteHandler('./routes/coolbuy/newest'));
router.get('/miniflux/subscription/:parameters?', lazyloadRouteHandler('./routes/miniflux/get_feeds'));
router.get('/miniflux/:feeds/:parameters?', lazyloadRouteHandler('./routes/miniflux/get_entries'));
-// NGA
-router.get('/nga/forum/:fid/:recommend?', lazyloadRouteHandler('./routes/nga/forum'));
-router.get('/nga/post/:tid', lazyloadRouteHandler('./routes/nga/post'));
+// NGA migrated to v2
+// router.get('/nga/forum/:fid/:recommend?', lazyloadRouteHandler('./routes/nga/forum'));
+// router.get('/nga/post/:tid', lazyloadRouteHandler('./routes/nga/post'));
// Nautilus
router.get('/nautilus/topic/:tid', lazyloadRouteHandler('./routes/nautilus/topics'));
diff --git a/lib/routes/nga/forum.js b/lib/v2/nga/forum.js
similarity index 80%
rename from lib/routes/nga/forum.js
rename to lib/v2/nga/forum.js
index dd6b038e2..8ec97b702 100644
--- a/lib/routes/nga/forum.js
+++ b/lib/v2/nga/forum.js
@@ -1,5 +1,8 @@
const got = require('@/utils/got');
const config = require('@/config').value;
+const { parseDate } = require('@/utils/parse-date');
+
+const X_UA = 'NGA_skull/6.0.5(iPhone10,3;iOS 12.0.1)';
module.exports = async (ctx) => {
const { fid, recommend } = ctx.params;
@@ -15,11 +18,9 @@ module.exports = async (ctx) => {
return `
`;
})
.replace(/\[url\](.+?)\[\/url\]/g, `$1`);
- const homePage = await got({
- method: 'post',
- url: 'https://ngabbs.com/app_api.php?__lib=subject&__act=list',
+ const homePage = await got.post('https://ngabbs.com/app_api.php?__lib=subject&__act=list', {
headers: {
- 'X-User-Agent': 'NGA_skull/6.0.5(iPhone10,3;iOS 12.0.1)',
+ 'X-User-Agent': X_UA,
Cookie: cookieString,
},
form: {
@@ -39,15 +40,13 @@ module.exports = async (ctx) => {
title: subject,
description: '',
link,
- pubDate: new Date(postdate * 1000).toUTCString(),
+ pubDate: parseDate(postdate, 'X'),
};
const description = await ctx.cache.tryGet(`nga-forum: ${link}`, async () => {
- const response = await got({
- method: 'post',
- url: 'https://ngabbs.com/app_api.php?__lib=post&__act=list',
+ const response = await got.post('https://ngabbs.com/app_api.php?__lib=post&__act=list', {
headers: {
- 'X-User-Agent': 'NGA_skull/6.0.5(iPhone10,3;iOS 12.0.1)',
+ 'X-User-Agent': X_UA,
Cookie: cookieString,
},
form: {
diff --git a/lib/v2/nga/maintainer.js b/lib/v2/nga/maintainer.js
new file mode 100644
index 000000000..159db7af2
--- /dev/null
+++ b/lib/v2/nga/maintainer.js
@@ -0,0 +1,4 @@
+module.exports = {
+ '/forum/:fid/:recommend?': ['xyqfer'],
+ '/post/:tid/:authorId?': ['xyqfer', 'syrinka'],
+};
diff --git a/lib/routes/nga/post.js b/lib/v2/nga/post.js
similarity index 63%
rename from lib/routes/nga/post.js
rename to lib/v2/nga/post.js
index 60e20ed0c..f76679a81 100644
--- a/lib/routes/nga/post.js
+++ b/lib/v2/nga/post.js
@@ -1,20 +1,20 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const iconv = require('iconv-lite');
+const { parseDate } = require('@/utils/parse-date');
+const timezone = require('@/utils/timezone');
const config = require('@/config').value;
module.exports = async (ctx) => {
- const getPageUrl = (tid, page = 1, hash = '') => `https://nga.178.com/read.php?tid=${tid}&page=${page}&rand=${Math.random() * 1000}#${hash}`;
- const getPage = async (tid, pageId = 1) => {
- const link = getPageUrl(tid, pageId);
+ const getPageUrl = (tid, authorId, page = 1, hash = '') => `https://nga.178.com/read.php?tid=${tid}&page=${page}&authorid=${authorId}&rand=${Math.random() * 1000}#${hash}`;
+ const getPage = async (tid, authorId, pageId = 1) => {
+ const link = getPageUrl(tid, authorId, pageId);
const timestamp = Math.floor(Date.now() / 1000);
let cookieString = `guestJs=${timestamp};`;
if (config.nga.uid && config.nga.cid) {
cookieString = `ngaPassportUid=${config.nga.uid}; ngaPassportCid=${config.nga.cid};`;
}
- const response = await got({
- method: 'get',
- url: link,
+ const response = await got(link, {
responseType: 'buffer',
headers: {
Cookie: cookieString,
@@ -25,10 +25,10 @@ module.exports = async (ctx) => {
return cheerio.load(htmlString);
};
- const getLastPageId = async (tid) => {
- const $ = await getPage(tid);
+ const getLastPageId = async (tid, authorId) => {
+ const $ = await getPage(tid, authorId);
const nav = $('#pagebtop');
- const match = nav.html().match(/\{0:'\/read.php\?tid=(\d+)',1:(\d+).*?\}/);
+ const match = nav.html().match(/\{0:'\/read\.php\?tid=(\d+).*?',1:(\d+),.*?\}/);
return match ? match[2] : 1;
};
@@ -37,9 +37,10 @@ module.exports = async (ctx) => {
const pipeImg = (str) => str.replace(/\[img\](.+?)\[\/img\]/g, (m, src) => `
`);
const tid = ctx.params.tid;
- const pageId = await getLastPageId(tid);
+ const authorId = ctx.params.authorId;
+ const pageId = await getLastPageId(tid, authorId);
- const $ = await getPage(tid, pageId);
+ const $ = await getPage(tid, authorId, pageId);
const title = $('title').text() || '';
const items = $('#m_posts_c')
@@ -51,8 +52,8 @@ module.exports = async (ctx) => {
const title = eraseTag(content.text());
const description = eraseTag(pipeImg(eraseQuote(content.html() || '')));
const postId = content.attr('id');
- const link = getPageUrl(tid, pageId, postId);
- const pubDate = new Date(post.find('.postInfo > span').first().text()).toUTCString();
+ const link = getPageUrl(tid, authorId, pageId, postId);
+ const pubDate = timezone(parseDate(post.find('.postInfo > span').first().text(), 'YYYY-MM-DD HH:mm'), +8);
return {
title,
@@ -64,8 +65,8 @@ module.exports = async (ctx) => {
});
ctx.state.data = {
- title: `nga-${title}`,
- link: getPageUrl(tid, pageId),
+ title: authorId ? `nga-${title}-${authorId}` : `nga-${title}`,
+ link: getPageUrl(tid, authorId, pageId),
item: items.get(),
};
};
diff --git a/lib/v2/nga/radar.js b/lib/v2/nga/radar.js
new file mode 100644
index 000000000..f50a206e2
--- /dev/null
+++ b/lib/v2/nga/radar.js
@@ -0,0 +1,42 @@
+module.exports = {
+ 'nga.cn': {
+ _name: 'NGA',
+ bbs: [
+ {
+ title: '分区帖子',
+ docs: 'https://docs.rsshub.app/bbs.html#nga',
+ source: '/thread.php',
+ target: (params, url) => new URL(url).searchParams.get('fid') && `/nga/forum/${new URL(url).searchParams.get('fid')}`,
+ },
+ {
+ title: '帖子',
+ docs: 'https://docs.rsshub.app/bbs.html#nga',
+ source: '/read.php',
+ target: (params, url) => new URL(url).searchParams.get('tid') && `/nga/post/${new URL(url).searchParams.get('tid')}`,
+ },
+ ],
+ },
+ '178.com': {
+ _name: 'NGA',
+ nga: [
+ {
+ title: '分区帖子',
+ docs: 'https://docs.rsshub.app/bbs.html#nga-fen-qu-tie-zi',
+ source: ['/thread.php'],
+ target: (params, url) => {
+ const fid = new URL(url).searchParams.get('fid');
+ return `/nga/forum/${fid}`;
+ },
+ },
+ {
+ title: '帖子',
+ docs: 'https://docs.rsshub.app/bbs.html#nga-tie-zi',
+ source: ['/read.php'],
+ target: (params, url) => {
+ const tid = new URL(url).searchParams.get('tid');
+ return `/nga/post/${tid}`;
+ },
+ },
+ ],
+ },
+};
diff --git a/lib/v2/nga/router.js b/lib/v2/nga/router.js
new file mode 100644
index 000000000..3dca2c357
--- /dev/null
+++ b/lib/v2/nga/router.js
@@ -0,0 +1,4 @@
+module.exports = function (router) {
+ router.get('/forum/:fid/:recommend?', require('./forum'));
+ router.get('/post/:tid/:authorId?', require('./post'));
+};