feat: add SteamGifts - Discussions (#2179)

This commit is contained in:
Wu Haotian
2019-05-20 11:08:43 +08:00
committed by DIYgod
parent 501b8febd5
commit 33ac079e59
3 changed files with 67 additions and 0 deletions

View File

@@ -97,6 +97,12 @@ Example: `https://store.steampowered.com/search/?specials=1&term=atelier` 中的
<Route author="maple3142" example="/steam/news/282800" path="/steam/news/:appids" :paramsDesc="['游戏 id']"/> <Route author="maple3142" example="/steam/news/282800" path="/steam/news/:appids" :paramsDesc="['游戏 id']"/>
## SteamGifts
### Discussions
<Route author="whtsky" example="/steamgifts/discussions" path="/steamgifts/discussions/:category?" :paramsDesc="['分类名称默认为All']"/>
## 旅法师营地 ## 旅法师营地
### 旅法师营地 ### 旅法师营地

View File

@@ -1316,4 +1316,7 @@ router.get('/maxnews/dota2', require('./routes/maxnews/dota2'));
// 数英网最新文章 // 数英网最新文章
router.get('/digitaling/index', require('./routes/digitaling/index')); router.get('/digitaling/index', require('./routes/digitaling/index'));
// Steamgifts
router.get('/steamgifts/discussions/:category?', require('./routes/steamgifts/discussions'));
module.exports = router; module.exports = router;

View File

@@ -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,
};
};