From 4a482f54ca6fb8eaec944dce86605376c5b0246d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=87=89=E5=87=89?= Date: Fri, 16 Nov 2018 18:52:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0:=20NGA=20(#1162)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #89 --- docs/README.md | 4 +++ router.js | 3 +++ routes/nga/forum.js | 60 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 routes/nga/forum.js diff --git a/docs/README.md b/docs/README.md index 3d14dfc36..5910cec46 100644 --- a/docs/README.md +++ b/docs/README.md @@ -557,6 +557,10 @@ RSSHub 提供下列 API 接口: +### NGA + + + ## 编程 ### 掘金 diff --git a/router.js b/router.js index d6e9cade8..a9b15322a 100644 --- a/router.js +++ b/router.js @@ -833,4 +833,7 @@ router.get('/testerhome/newest', require('./routes/testerhome/newest')); router.get('/weseepro/newest', require('./routes/weseepro/newest')); router.get('/weseepro/circle', require('./routes/weseepro/circle')); +// NGA +router.get('/nga/forum/:fid', require('./routes/nga/forum')); + module.exports = router; diff --git a/routes/nga/forum.js b/routes/nga/forum.js new file mode 100644 index 000000000..1fb02e04a --- /dev/null +++ b/routes/nga/forum.js @@ -0,0 +1,60 @@ +const axios = require('../../utils/axios'); +const qs = require('querystring'); + +module.exports = async (ctx) => { + const { fid } = ctx.params; + const axiosInstance = axios.create({ + baseURL: 'https://ngabbs.com/app_api.php', + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + 'X-User-Agent': 'NGA_skull/6.0.5(iPhone10,3;iOS 12.0.1)', + }, + }); + const homePage = await axiosInstance.request({ + method: 'post', + url: '?__lib=subject&__act=list', + data: qs.stringify({ + fid, + }), + }); + + const list = homePage.data.result.data.filter(({ tid }) => tid); + + const resultItem = await Promise.all( + list.map(async ({ subject, postdate, tid }) => { + const link = `https://nga.178.com/read.php?tid=${tid}`; + const item = { + title: subject, + description: '', + link, + pubDate: new Date(postdate * 1000).toUTCString(), + }; + + const description = await ctx.cache.tryGet( + `nga-forum: ${link}`, + async () => { + const response = await axiosInstance.request({ + method: 'post', + url: '?__lib=post&__act=list', + data: qs.stringify({ + tid, + }), + }); + + return response.data.result[0].content; + }, + 24 * 60 * 60 + ); + + item.description = description; + return Promise.resolve(item); + }) + ); + + ctx.state.data = { + title: `NGA-${fid}`, + link: `https://nga.178.com/thread.php?fid=${fid}`, + description: 'NGA是国内专业的游戏玩家社区,魔兽世界,英雄联盟,炉石传说,风暴英雄,暗黑破坏神3(D3)游戏攻略讨论,以及其他热门游戏玩家社区', + item: resultItem, + }; +};