Files
RSSHub/lib/v2/epicgames/index.js
KotaHv d719977c30 fix(route): Epic Games Store (#9836)
* fix(route): Epic Games Store

* modify categories.forEach to categories.some

* fix(route): Epic Games Store - fix item image url

* fix(route): Epic Games Store - fix item description
2022-05-27 16:37:50 +08:00

81 lines
3.3 KiB
JavaScript

const got = require('@/utils/got');
const { parseDate } = require('@/utils/parse-date');
const { art } = require('@/utils/render');
const path = require('path');
const dayjs = require('dayjs');
module.exports = async (ctx) => {
const locale = ctx.params.locale ?? 'en-US';
const country = ctx.params.country ?? 'US';
const rootUrl = 'https://store.epicgames.com';
const currentUrl = `${rootUrl}/${locale}/free-games?lang=${locale}`;
const apiUrl = `https://store-site-backend-static-ipv4.ak.epicgames.com/freeGamesPromotions?locale=${locale}&country=${country}&allowCountries=${country}`;
const contentBaseUrl = `https://store-content-ipv4.ak.epicgames.com/api/${locale}/content`;
const response = await got({
method: 'get',
url: apiUrl,
});
const now = dayjs();
const items = response.data.data.Catalog.searchStore.elements
.filter(
(item) =>
item.promotions &&
item.promotions.promotionalOffers &&
item.promotions.promotionalOffers[0] &&
dayjs(item.promotions.promotionalOffers[0].promotionalOffers[0].startDate) <= now &&
dayjs(item.promotions.promotionalOffers[0].promotionalOffers[0].endDate) > now
)
.map(async (item) => {
let link = `${rootUrl}/${locale}/p/`;
let contentUrl = `${contentBaseUrl}/products/`;
let isBundles = false;
item.categories.some((category) => {
if (category.path === 'bundles') {
link = `${rootUrl}/${locale}/bundles/`;
isBundles = true;
contentUrl = `${contentBaseUrl}/bundles/`;
return true;
}
return false;
});
const linkSlug = item.catalogNs.mappings.length > 0 ? item.catalogNs.mappings[0].pageSlug : item.offerMappings.length > 0 ? item.offerMappings[0].pageSlug : item.productSlug ? item.productSlug : item.urlSlug;
link += linkSlug;
contentUrl += linkSlug;
let description = item.description;
if (item.offerType !== 'BASE_GAME') {
const contentResp = await got({
method: 'get',
url: contentUrl,
});
description = isBundles ? contentResp.data.data.about.shortDescription : contentResp.data.pages[0].data.about.shortDescription;
}
let image = item.keyImages[0].url;
item.keyImages.some((keyImage) => {
if (keyImage.type === 'DieselStoreFrontWide') {
image = keyImage.url;
return true;
}
return false;
});
return {
title: item.title,
author: item.seller.name,
link,
description: art(path.join(__dirname, 'templates/description.art'), {
description,
image,
}),
pubDate: parseDate(item.promotions.promotionalOffers[0].promotionalOffers[0].startDate),
};
});
ctx.state.data = {
title: 'Epic Games Store - Free Games',
link: currentUrl,
item: await Promise.all(items),
};
};