From 216e59686ea45b13995ff5918637c35525888ba3 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Mon, 30 Sep 2019 16:44:16 +0800 Subject: [PATCH] feat: add mhw news --- docs/game.md | 4 ++++ lib/router.js | 3 ++- lib/routes/mhw/news.js | 42 ++++++++++++++++++++++++++++++++++++++++ lib/routes/mhw/update.js | 5 ++++- 4 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 lib/routes/mhw/news.js diff --git a/docs/game.md b/docs/game.md index 9646f1ee0..22a97e806 100644 --- a/docs/game.md +++ b/docs/game.md @@ -199,6 +199,10 @@ Example: `https://store.steampowered.com/search/?specials=1&term=atelier` 中的 +### 最新消息 + + + ## 旅法师营地 ### 旅法师营地 diff --git a/lib/router.js b/lib/router.js index e43c46f85..f8266a413 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1437,8 +1437,9 @@ router.get('/dilbert/strip', require('./routes/dilbert/strip')); router.get('/yxdzqb/:type', require('./routes/yxdzqb')); // 怪物猎人 -router.get('/monsterhunter/update', require('./routes/monsterhunter/update')); +router.get('/monsterhunter/update', require('./routes/mhw/update')); router.get('/mhw/update', require('./routes/mhw/update')); +router.get('/mhw/news', require('./routes/mhw/news')); // 005.tv router.get('/005tv/zx/latest', require('./routes/005tv/zx')); diff --git a/lib/routes/mhw/news.js b/lib/routes/mhw/news.js new file mode 100644 index 000000000..fac8cc8f6 --- /dev/null +++ b/lib/routes/mhw/news.js @@ -0,0 +1,42 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + const url = 'https://www.monsterhunter.com/world-iceborne/cn/news/'; + const response = await got({ + method: 'get', + url: `https://www.monsterhunter.com/world-iceborne/assets/data/cn/news.json?_=${+new Date()}`, + }); + + let result = response.data.topics.map((item) => ({ + title: item.text, + link: `https://www.monsterhunter.com${item.link}`, + })); + + result = await Promise.all( + result.map(async (item) => + Promise.resolve( + await ctx.cache.tryGet(item.link, async () => { + const response = await got.get(item.link.replace('/cn/', '/') + 'inc/contents_cn.html'); + const $ = cheerio.load(response.data); + + return { + title: + $('.h2') + .text() + .trim() || item.title, + description: $('#contents').html(), + link: item.link, + author: 'MONSTER HUNTER WORLD: ICEBORNE', + }; + }) + ) + ) + ); + + ctx.state.data = { + title: '怪物猎人世界最新消息', + link: url, + item: result, + }; +}; diff --git a/lib/routes/mhw/update.js b/lib/routes/mhw/update.js index 642de1a4c..13282607b 100644 --- a/lib/routes/mhw/update.js +++ b/lib/routes/mhw/update.js @@ -31,7 +31,10 @@ module.exports = async (ctx) => { .map((index, item) => { item = $(item); return { - title: item.find('.text').text(), + title: item + .find('.text') + .text() + .trim(), pubDate: new Date(item.find('.date').text()).toUTCString(), link: url2 + item.attr('href'), };