-
Notifications
You must be signed in to change notification settings - Fork 14
/
generate_api_cache.mjs
76 lines (65 loc) · 1.69 KB
/
generate_api_cache.mjs
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
import * as fs from 'fs';
import {
API_LANGUAGES,
fetch_api,
initPrettier,
compare_strings,
} from './node_api_helpers.mjs';
const ENDPOINTS = [
['/v2/skills', true],
['/v2/specializations', true],
['/v2/traits', true],
['/v2/items', true],
['/v2/professions', false],
['/v2/races', false],
];
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
async function run() {
await initPrettier();
let fetched = 0;
for (let [endpoint, fetch_ids] of ENDPOINTS) {
let ids = ['all'];
if (fetch_ids) {
console.log('Fetching ids for', endpoint);
ids = await fetch_api(endpoint);
}
for (let lang of API_LANGUAGES) {
let filename = `gw2apicache/${endpoint
.replaceAll('/', '_')
.toLowerCase()}_${lang}.json`;
if (fs.existsSync(filename)) {
console.log(filename, 'exists, skipping');
continue;
}
console.log('\t', lang);
let remaining = [...ids];
let all = [];
while (remaining.length > 0) {
let fetch_ids = remaining.splice(0, 200);
let arr = await fetch_api(
`${endpoint}?lang=${lang}&ids=${fetch_ids.join(',')}`,
);
all = all.concat(arr);
fetched++;
if (fetched >= 500) {
console.log(
'Waiting a minute to prevent getting rate-limited by the API',
);
await sleep(60 * 1000);
fetched = 0;
}
}
all.sort((a, b) => compare_strings(a.id, b.id));
fs.writeFileSync(filename, JSON.stringify(all, undefined, '\t'));
}
}
console.log('All files generated');
}
run().catch((e) => {
console.error(e);
process.exit(1);
});