diff --git a/lib/router.js b/lib/router.js index 0992a8628..cc97555e5 100644 --- a/lib/router.js +++ b/lib/router.js @@ -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')); diff --git a/lib/routes/epicgames/index.js b/lib/routes/epicgames/index.js new file mode 100644 index 000000000..d5b61db44 --- /dev/null +++ b/lib/routes/epicgames/index.js @@ -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}
`; + 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, + }; +}; diff --git a/lib/routes/epicgames/router.js b/lib/routes/epicgames/router.js new file mode 100644 index 000000000..13938515f --- /dev/null +++ b/lib/routes/epicgames/router.js @@ -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; +}; diff --git a/lib/routes/epicgames/supportedList.js b/lib/routes/epicgames/supportedList.js new file mode 100644 index 000000000..a37a1ba35 --- /dev/null +++ b/lib/routes/epicgames/supportedList.js @@ -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', + }, + }), + }, +}; diff --git a/package.json b/package.json index 18440bea9..57f24200c 100644 --- a/package.json +++ b/package.json @@ -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",