-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
374 lines (312 loc) · 12.3 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// Author: NikkelM
// Description: Fetches all Game Pass game Id's and properties for a given market and formats them according to a given configuration.
// API URL's taken from https://www.reddit.com/r/XboxGamePass/comments/jt214y/public_api_for_fetching_the_list_of_game_pass/
// Suppresses the warning about the fetch API being unstable
process.removeAllListeners('warning');
// Utility libraries
import fs from 'fs';
import jsonschema from 'jsonschema';
// Utility for getting the directory of the current file
import { fileURLToPath } from 'url';
import { dirname } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// ---------- Setup ----------
// ----- Config -----
try {
let configFileName;
if (fs.existsSync(__dirname + '/config.json')) {
console.log("Loading configuration file \"config.json\"...");
configFileName = 'config.json';
} else if (fs.existsSync(__dirname + '/config.default.json')) {
console.log("!!! No custom configuration file found! Loading default configuration file \"config.default.json\"...");
configFileName = 'config.default.json';
}
var CONFIG = JSON.parse(fs.readFileSync(__dirname + '/' + configFileName));
} catch (error) {
console.error("Error loading configuration file: " + error);
process.exit(1);
}
// Validate the config file against the schema
console.log("Validating configuration file...\n");
try {
const validator = new jsonschema.Validator();
validator.validate(CONFIG, JSON.parse(fs.readFileSync(__dirname + '/config.schema.json')), { throwError: true });
} catch (error) {
console.error("Error validating configuration file: " + error);
process.exit(1);
}
// ----- Output -----
if (!fs.existsSync(__dirname + '/output')) {
fs.mkdirSync(__dirname + '/output');
}
// ---------- Main ----------
const emptyValuePlaceholder = CONFIG.treatEmptyStringsAsNull ? null : "";
main();
async function main() {
// Fetch Game Pass game ID's and properties for each pass type and market specified in the configuration
// We do this in parallel to speed up the process
// While the functions do return the formatted properties, we currently do not use them here, as writing the output files is handled by the functions themselves
for (const market of CONFIG.markets) {
if (CONFIG.platformsToFetch.includes("console")) {
const consoleFormattedProperties = runScriptForPassTypeAndMarket("console", market);
}
if (CONFIG.platformsToFetch.includes("pc")) {
const pcFormattedProperties = runScriptForPassTypeAndMarket("pc", market);
}
if (CONFIG.platformsToFetch.includes("eaPlay")) {
const eaPlayFormattedProperties = runScriptForPassTypeAndMarket("eaPlay", market);
}
}
}
async function runScriptForPassTypeAndMarket(passType, market) {
const gameIds = await fetchGameIDs(passType, market);
const gameProperties = await fetchGameProperties(gameIds, passType, market);
const formattedData = formatData(gameProperties, passType);
fs.writeFileSync(`./output/formattedGameProperties_${passType}_${market}.json`, JSON.stringify(formattedData, null, 2));
return formattedData;
}
// ---------- Fetch game ID's & properties ----------
// Get all Game Pass Game ID's for this market
async function fetchGameIDs(passType, market) {
const APIIds = {
"console": "f6f1f99f-9b49-4ccd-b3bf-4d9767a77f5e",
"pc": "fdd9e2a7-0fee-49f6-ad69-4354098401ff",
"eaPlay": "b8900d09-a491-44cc-916e-32b5acae621b"
}
console.log(`Fetching ${passType} Game Pass game ID's for market "${market}"...`);
let gameIds = await fetch(`https://catalog.gamepass.com/sigls/v2?id=${APIIds[passType]}&language=${CONFIG.language}&market=${market}`)
.then((response) => response.json())
.then((data) => data.filter((entry) => entry.id).map((entry) => entry.id));
return gameIds;
}
async function fetchGameProperties(gameIds, passType, market) {
console.log(`Fetching game properties for ${gameIds.length} ${passType} games for market "${market}"...`);
return await fetch(`https://displaycatalog.mp.microsoft.com/v7.0/products?bigIds=${gameIds}&market=${market}&languages=${CONFIG.language}`)
.then((response) => response.json())
.then((data) => {
if (CONFIG.keepCompleteProperties) {
fs.writeFileSync(`./output/completeGameProperties_${passType}_${market}.json`, JSON.stringify(data, null, 2));
}
return data;
});
}
// Format the data according to the configuration
function formatData(gameProperties, passType) {
console.log(`Formatting game properties for ${gameProperties.Products.length} ${passType} games...`);
let formattedData = CONFIG.outputFormat === "array" ? [] : {};
for (const game of gameProperties.Products) {
let index;
switch (CONFIG.outputFormat) {
case "array":
index = formattedData.length;
break;
case "productId":
index = game.ProductId;
break;
case "productTitle":
index = game.LocalizedProperties[0].ProductTitle;
break;
case "0-indexed":
index = Object.keys(formattedData).length;
break;
}
formattedData[index] = {};
for (const [property, propertyValue] of Object.entries(CONFIG.includedProperties)) {
const result = getPropertyValue(game, property, propertyValue);
// Add the property to the object, only if it is not undefined. undefined indicates the property was present in the config, but disabled.
// Null values are valid, e.g. if a given name does not have a value for the requested property.
if (result !== undefined) {
formattedData[index][property] = result;
}
}
}
return formattedData;
}
// Get the value of the property for the given game according to the specification in propertyValue
function getPropertyValue(game, property, propertyValue) {
let result;
switch (property) {
case "productTitle":
result = getProductTitle(game, propertyValue);
break;
case "productId":
result = getProductId(game, propertyValue);
break;
case "developerName":
result = getDeveloperName(game, propertyValue);
break;
case "publisherName":
result = getPublisherName(game, propertyValue);
break;
case "productDescription":
result = getProductDescription(game, propertyValue);
break;
case "images":
result = getImages(game, propertyValue);
break;
case "releaseDate":
result = getReleaseDate(game, propertyValue);
break;
case "userRating":
result = getUserRating(game, propertyValue);
break;
case "pricing":
result = getPricing(game, propertyValue);
break;
case "categories":
result = getCategories(game, propertyValue);
break;
case "storePage":
result = getStorePageUrl(game, propertyValue);
break;
default:
// Due to our config validation, this should never happen, but just in case...
console.log(`Invalid property: ${property}`);
return undefined;
}
return result;
}
// ---------- Utility functions for the various property types ----------
function getProductTitle(game, productTitleProperty) {
if (!productTitleProperty) { return undefined; }
return game.LocalizedProperties[0].ProductTitle.length > 0
? game.LocalizedProperties[0].ProductTitle
: emptyValuePlaceholder;
}
function getProductId(game, productIdProperty) {
if (!productIdProperty) { return undefined; }
return game.ProductId.length > 0
? game.ProductId
: emptyValuePlaceholder;
}
function getDeveloperName(game, developerNameProperty) {
if (!developerNameProperty) { return undefined; }
return game.LocalizedProperties[0].DeveloperName.length > 0
? game.LocalizedProperties[0].DeveloperName
: emptyValuePlaceholder;
}
function getPublisherName(game, publisherNameProperty) {
if (!publisherNameProperty) { return undefined; }
return game.LocalizedProperties[0].PublisherName.length > 0
? game.LocalizedProperties[0].PublisherName
: emptyValuePlaceholder;
}
function getProductDescription(game, productDescriptionProperty) {
if (!productDescriptionProperty.enabled) { return undefined; }
if (productDescriptionProperty.preferShort && game.LocalizedProperties[0].ShortDescription?.length > 0) {
return game.LocalizedProperties[0].ShortDescription;
} else {
return game.LocalizedProperties[0].ProductDescription?.length > 0
? game.LocalizedProperties[0].ProductDescription
: emptyValuePlaceholder;
}
}
function getImages(game, imageProperty) {
if (!imageProperty.enabled) { return undefined; }
let images = {};
const numImagesByType = {
"TitledHeroArt": 0,
"SuperHeroArt": 0,
"Logo": 0,
"Poster": 0,
"Screenshot": 0,
"BoxArt": 0,
"Hero": 0,
"BrandedKeyArt": 0,
"FeaturePromotionalSquareArt": 0
};
for (const image of game.LocalizedProperties[0].Images) {
if (imageProperty.imageTypes[image.ImagePurpose] && (imageProperty.imageTypes[image.ImagePurpose] === -1 || numImagesByType[image.ImagePurpose] < imageProperty.imageTypes[image.ImagePurpose])) {
if (!images[image.ImagePurpose]) {
images[image.ImagePurpose] = [];
}
images[image.ImagePurpose].push(image.Uri.startsWith('https:') ? image.Uri : `https:${image.Uri}`);
numImagesByType[image.ImagePurpose] = numImagesByType[image.ImagePurpose] ? numImagesByType[image.ImagePurpose] + 1 : 1;
}
}
return images;
}
function getReleaseDate(game, releaseDateProperty) {
if (!releaseDateProperty.enabled) { return undefined; }
if (releaseDateProperty.format === "date") {
return game.MarketProperties[0].OriginalReleaseDate.length > 0
? game.MarketProperties[0].OriginalReleaseDate?.split("T")[0]
: emptyValuePlaceholder;
} else if (releaseDateProperty.format === "dateTime") {
return game.MarketProperties[0].OriginalReleaseDate.length > 0
? game.MarketProperties[0].OriginalReleaseDate
: emptyValuePlaceholder;
} else {
// Due to our config validation, this should never happen, but just in case...
console.log(`Invalid release date format: ${releaseDateProperty.format}`);
return undefined;
}
}
function getUserRating(game, userRatingProperty) {
if (!userRatingProperty.enabled) { return undefined; }
const intervalMapping = {
"7Days": 0,
"30Days": 1,
"AllTime": 2
}
// Get the x-out-of-5 stars rating
let userRating = game.MarketProperties[0].UsageData[intervalMapping[userRatingProperty.aggregationInterval]]?.AverageRating;
// Convert to a percentage if requested
if (userRatingProperty.format === "percentage") {
userRating = parseFloat((userRating / 5).toFixed(2));
}
return userRating;
}
function getPricing(game, pricingProperty) {
if (!pricingProperty.enabled) { return undefined; }
let missingPricePlaceholder;
switch (pricingProperty.missingPricePolicy) {
case "useZero":
missingPricePlaceholder = 0;
break;
case "useNull":
missingPricePlaceholder = null;
break;
case "useEmptyString":
missingPricePlaceholder = "";
break;
default:
// Due to our config validation, this should never happen, but just in case...
console.log(`Invalid missing price policy: ${pricingProperty.missingPricePolicy}`);
return undefined;
}
let prices = {};
prices["currencyCode"] = game.DisplaySkuAvailabilities[0]?.Availabilities[0]?.OrderManagementData?.Price?.CurrencyCode;
for (const priceType of pricingProperty.priceTypes) {
// Small workaround to not exclude 0-values
prices[priceType] = typeof game.DisplaySkuAvailabilities[0].Availabilities[0].OrderManagementData.Price[priceType] === 'number'
? game.DisplaySkuAvailabilities[0].Availabilities[0].OrderManagementData.Price[priceType]
: missingPricePlaceholder;
}
return prices;
}
function getCategories(game, categoriesProperty) {
if (!categoriesProperty) { return undefined; }
let categories = [];
if (game.Properties.Categories) {
categories = game.Properties.Categories;
}
// Each game also has a "main" category, which may or may not be included in the list of categories
if (!categories.includes(game.Properties.Category)) {
categories.push(game.Properties.Category);
}
return categories;
}
function getStorePageUrl(game, storePageUrlProperty) {
if (!storePageUrlProperty) { return undefined; }
if(!game.LocalizedProperties[0].ProductTitle || !game.ProductId) {
return undefined;
}
// 1. Convert to lowercase
// 2. Replace all non-alphanumeric characters with a dash
// 3. Replace multiple dashes with a single dash
// 4. Remove leading and trailing dashes
const formattedGameName = game.LocalizedProperties[0].ProductTitle.toLowerCase().replace(/[^a-z0-9]/g, '-').replace(/-+/g, '-').replace(/^-|-$/g, '');
return `https://www.xbox.com/${CONFIG.language}/games/store/${formattedGameName}/${game.ProductId}`;
}