From 9a3a99eb901104160df815df24f4615be4c4df9a Mon Sep 17 00:00:00 2001 From: Yu Jin Date: Mon, 13 Apr 2020 04:25:33 -0700 Subject: [PATCH] feat: added steamdb free games (#4372) --- docs/game.md | 10 +++++ lib/router.js | 3 ++ lib/routes/steam/steamdb/free.js | 64 ++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 lib/routes/steam/steamdb/free.js diff --git a/docs/game.md b/docs/game.md index 4c7beaafd..437cee621 100644 --- a/docs/game.md +++ b/docs/game.md @@ -190,6 +190,16 @@ Example: `https://store.steampowered.com/search/?specials=1&term=atelier` 中的 +## SteamDB + +### 免费游戏 + + + +| 全部 | 周末 | 永久 | +| ------ | ------- | ---- | +| <留空> | weekend | keep | + ## SteamGifts ### Discussions diff --git a/lib/router.js b/lib/router.js index 91b4f7943..24c723e73 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1069,6 +1069,9 @@ router.get('/steam/news/:appids', require('./routes/steam/news')); // Steamgifts router.get('/steamgifts/discussions/:category?', require('./routes/steam/steamgifts/discussions')); +// SteamDB +router.get('/steamdb/free/:type?', require('./routes/steam/steamdb/free')); + // 扇贝 router.get('/shanbay/checkin/:id', require('./routes/shanbay/checkin')); router.get('/shanbay/footprints/:category?', require('./routes/shanbay/footprints')); diff --git a/lib/routes/steam/steamdb/free.js b/lib/routes/steam/steamdb/free.js new file mode 100644 index 000000000..9754e4ba8 --- /dev/null +++ b/lib/routes/steam/steamdb/free.js @@ -0,0 +1,64 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + const { type } = ctx.params; + const url = 'https://steamdb.info/upcoming/free'; + const { data: html } = await got.get(url); + const $ = cheerio.load(html); + + const $trs = $('.container table.table-products:first-of-type tbody tr'); + const getItems = $trs + .toArray() + .reverse() + .map(async (el) => { + const $el = $(el); + const $tds = $el.children('td'); + + // link & logo - 0 + const linkEl = $($tds.get(0)).find('a').get(0); + const link = (linkEl && linkEl.attribs.href) || ''; + + // appid + const found = /app\/(\d+)/.exec(link); + const appid = found ? found[1] : ''; + + // title - 1 + const title = $($tds.get(1)).text().trim(); + + // promotion type - 2 + const promoType = $($tds.get(2)).text().trim(); + + // startDate - 3 + const startDate = $tds.get(3).attribs.title; + + // endDate - 4 + // const endDate = $tds.get(4).attribs.title; + + // ignore items without appid or not matched the type + if (!appid || (type && type.toLowerCase() !== promoType.toLocaleLowerCase())) { + return null; + } + + const hoverLink = `https://steamdb.info/api/RenderAppHover/?appid=${appid}`; + const description = await ctx.cache.tryGet(hoverLink, async () => { + const { data: description } = await got.get(hoverLink); + return description; + }); + + return { + title: `[${promoType}] ${title}`, + link, + pubDate: new Date(startDate).toUTCString(), + description, + }; + }); + + const item = await Promise.all(getItems); + + ctx.state.data = { + title: 'Steam 免费游戏 [SteamDB]', + link: url, + item: item.filter((itm) => !!itm), + }; +};