forked from WFCD/warframe-hub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
initialWorldstateUpdater.js
110 lines (92 loc) · 2.53 KB
/
initialWorldstateUpdater.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
'use strict';
/* eslint-disable no-console */
const fetch = require('node-fetch');
const fs = require('fs');
const jsonFileName = 'initialWorldstate.json';
const jsonFolder = './src/assets/json';
const apiBaseUrl = 'api.warframestat.us';
const apiPlatforms = ['pc', 'ps4', 'xb1', 'swi'];
function onError(error) {
console.error(error);
}
function parseArray(data) {
return Array.isArray(data) ? [] : null;
}
function parseBoolean(data, key) {
if (typeof data !== 'boolean') {
return null;
}
return key.toLowerCase().includes('expired');
}
function parseDate(data) {
return !isNaN(new Date(data)) ? '2000-01-01T01:00:00.000Z' : null;
}
function parseId(data, key) {
return key.toLowerCase().includes('id') ? '12345' : null;
}
function parseNumber(data) {
return !isNaN(data) ? '0.00' : null;
}
function parseETA(data) {
//Matches the format 0s with optional dates up to 0y 0d 0h 0m 0s
return /([-\d]+y |)([-\d]+d |)([-\d]+h |)([-\d]+m |)[-\d]+s/.test(data) ? '1h 1m 1s' : null;
}
function parseDefault(data, key, objectPath) {
const defaultOutput = 'Loading...';
console.info(`Defaulting ${objectPath} - ${data} to ${defaultOutput}`);
return defaultOutput;
}
function parseObject(data, key, objectPath = '') {
if (typeof data !== 'object') {
return null;
}
const orderedParseFunctions = [
parseArray,
parseObject,
parseBoolean,
parseDate,
parseId,
parseNumber,
parseETA,
parseDefault,
];
const cleanedData = {};
Object.entries(data).forEach(([key, prop]) => {
let temp;
for (let fun of orderedParseFunctions) {
temp = fun(prop, key, `${objectPath}.${key}`);
if (temp !== null) {
break;
}
}
cleanedData[key] = temp;
});
return cleanedData;
}
function parseBase(platform, data) {
const platformOutput = parseObject(data, platform, platform);
return [platform, platformOutput];
}
function main() {
Promise.all(
apiPlatforms.map((platform) => {
return fetch(`https://${apiBaseUrl}/${platform}`)
.then((t) => t.text())
.then(JSON.parse)
.then(parseBase.bind(null, platform))
.catch(onError);
})
).then((platformsData) => {
const output = platformsData.reduce((acc, data) => {
acc[data[0]] = data[1];
return acc;
}, {});
fs.writeFile(`${jsonFolder}/${jsonFileName}`, JSON.stringify(output, null, 2), function(err) {
if (err) {
return console.log(err);
}
console.info(`${jsonFileName} updated at ${jsonFolder}/${jsonFileName}`);
});
});
}
main();