diff --git a/docs/game.md b/docs/game.md index 1297d7ef6..fc6836a6f 100644 --- a/docs/game.md +++ b/docs/game.md @@ -97,6 +97,12 @@ Example: `https://store.steampowered.com/search/?specials=1&term=atelier` 中的 +## SteamGifts + +### Discussions + + + ## 旅法师营地 ### 旅法师营地 diff --git a/lib/router.js b/lib/router.js index d9487d576..faae72898 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1316,4 +1316,7 @@ router.get('/maxnews/dota2', require('./routes/maxnews/dota2')); // 数英网最新文章 router.get('/digitaling/index', require('./routes/digitaling/index')); +// Steamgifts +router.get('/steamgifts/discussions/:category?', require('./routes/steamgifts/discussions')); + module.exports = router; diff --git a/lib/routes/steamgifts/discussions.js b/lib/routes/steamgifts/discussions.js new file mode 100644 index 000000000..39f1f8841 --- /dev/null +++ b/lib/routes/steamgifts/discussions.js @@ -0,0 +1,58 @@ +const axios = require('@/utils/axios'); +const cheerio = require('cheerio'); +const url = require('url'); + +const host = 'https://www.steamgifts.com'; +const discussionsIndexUrl = url.resolve(host, '/discussions'); + +module.exports = async (ctx) => { + const category = ctx.params.category; + + const categoryUrl = category ? discussionsIndexUrl + `/${category}` : discussionsIndexUrl; + const response = await axios.get(categoryUrl); + const $ = cheerio.load(response.data); + const links = $('a.table__column__heading') + .slice(0, 20) + .map((i, e) => url.resolve(host, $(e).attr('href'))) + .get(); + + const out = await Promise.all( + links.map(async (link) => { + const cache = await ctx.cache.get(link); + if (cache) { + return JSON.parse(cache); + } + + const response = await axios({ + method: 'get', + url: link, + headers: { + Referer: categoryUrl, + }, + }); + + const $ = cheerio.load(response.data); + + const timestamp = Number($('.comment__actions span').data('timestamp')); + const date = new Date(timestamp * 1000); + + const item = { + title: $('title').text(), + description: $('.comment__description').html(), + pubDate: date.toUTCString(), + link, + guid: link, + }; + + ctx.cache.set(link, JSON.stringify(item)); + + return item; + }) + ); + + ctx.state.data = { + title: `Steamgifts - Discussions - ${category || 'All'}`, + link: categoryUrl, + item: out, + }; +};