feat: add EpicGames get FreeGames (#3196)

This commit is contained in:
Zyx-A
2019-10-12 18:39:35 +08:00
committed by DIYgod
parent 572e3dea2e
commit 6a7d45945e
5 changed files with 118 additions and 0 deletions

View File

@@ -1,6 +1,23 @@
const Router = require('koa-router');
const router = new Router();
// 遍历整个 routes 文件夹,导入模块路由 router.js 和 router-custom.js 文件
// 格式参考用例routes/epicgames/router.js
const RouterPath = require('require-all')({
dirname: __dirname + '/routes',
filter: /^.*router([-_]custom[s]?)?\.js$/,
});
// 将收集到的自定义模块路由进行合并
for (const project in RouterPath) {
for (const routerName in RouterPath[project]) {
const proRouter = RouterPath[project][routerName]();
proRouter.stack.forEach((nestedLayer) => {
router.stack.push(nestedLayer);
});
}
}
// index
router.get('/', require('./routes/index'));

View File

@@ -0,0 +1,73 @@
const got = require('@/utils/got');
const supportedList = require('./supportedList');
module.exports = async (ctx) => {
// 获取用户传递进来的参数。
// 此处 supportedList.js 文件中的可选项 collection 值只有"freegames"可选
const collection = ctx.params.collection;
const config = supportedList[collection.toLowerCase()];
const desc = 'Epic 游戏限免';
const link = config.link;
const homeLink = config.homeLink;
const content = config.jsonData;
const response = await got({
method: 'post',
url: link,
headers: {
Referer: homeLink,
'Content-Type': 'application/json; charset=utf-8',
'Content-Length': content.length,
},
data: content,
});
const data = response.data.data;
let item = new Map();
const out = new Array();
let notHaveImageUrl = true;
data.Catalog.catalogOffers.elements.map((element) => {
if (element.promotions.promotionalOffers.length !== 0) {
element.promotions.promotionalOffers.forEach((isPromotionalOffer) => {
if (isPromotionalOffer.promotionalOffers.length !== 0) {
isPromotionalOffer.promotionalOffers.forEach((havePromotionalOffer) => {
if (havePromotionalOffer.discountSetting.discountType === 'PERCENTAGE' && havePromotionalOffer.discountSetting.discountPercentage === 0) {
item = {
title: element.title,
link: `https://www.epicgames.com/store/zh-CN/product/${element.productSlug}`,
pubDate: new Date(havePromotionalOffer.startDate).toUTCString(),
};
}
if (element.keyImages.length !== 0) {
element.keyImages.forEach((imageUrl) => {
if (imageUrl.type === 'DieselStoreFrontWide') {
item.description = `${element.description}<br><img src="${imageUrl.url}">`;
notHaveImageUrl = false;
}
});
}
if (notHaveImageUrl) {
item.description = element.description;
}
});
}
});
out.push(item);
item = {};
}
return true;
});
// out.pop(undefined);
ctx.state.data = {
title: desc,
link: homeLink,
description: desc,
item: out,
};
};

View File

@@ -0,0 +1,9 @@
// 文件名必须为 router.js。
// Epic Games
module.exports = () => {
const Router = require('koa-router');
const router = new Router();
router.get('/epicgames/:collection', require('./index'));
return router;
};

View File

@@ -0,0 +1,18 @@
module.exports = {
freegames: {
collection: 'freegames',
collectionCN: '免费游戏',
homeLink: 'https://www.epicgames.com/store/zh-CN/',
// link: 'https://www.epicgames.com/store/zh-CN/collection/free-games-collection',
link: 'https://graphql.epicgames.com/graphql',
jsonData: JSON.stringify({
query:
'\n query promotionsQuery($namespace: String!, $country: String!, $locale: String!) {\n Catalog {\n catalogOffers(namespace: $namespace, locale: $locale, params: {category: "freegames", country: $country, sortBy: "effectiveDate", sortDir: "asc"}) {\n elements {\n title\n description\n id\n namespace\n categories {\n path\n }\n keyImages {\n type\n url\n }\n productSlug\n promotions {\n promotionalOffers {\n promotionalOffers {\n startDate\n endDate\n discountSetting {\n discountType\n discountPercentage\n }\n }\n }\n upcomingPromotionalOffers {\n promotionalOffers {\n startDate\n endDate\n discountSetting {\n discountType\n discountPercentage\n }\n }\n }\n }\n }\n }\n }\n }\n ',
variables: {
namespace: 'epic',
country: 'US',
locale: 'zh-CN',
},
}),
},
};

View File

@@ -93,6 +93,7 @@
"puppeteer": "1.20.0",
"query-string": "6.8.3",
"redis": "2.8.0",
"require-all": "^3.0.0",
"rss-parser": "3.7.2",
"sharp": "0.23.1",
"socks-proxy-agent": "4.0.2",