forked from kokarn/tarkov-image-generator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
get-json.js
85 lines (82 loc) · 3.5 KB
/
get-json.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
const fs = require('fs');
const got = require('got');
const api = require('./scanner-api');
module.exports = {
items: async () => {
let items = false;
try {
items = JSON.parse(fs.readFileSync('./items.json', 'utf8'));
const stats = fs.statSync('./items.json');
if (Date.now() - stats.mtimeMs > 1000*60*60*2) {
throw new Error('stale');
}
} catch (error) {
try {
if (process.env.API_USERNAME && process.env.API_PASSWORD && process.env.SCANNER_NAME) {
items = await api.getJson('items.json').catch(() => { return false; });
}
if (!items) {
console.log('Downloading SPT item data');
items = JSON.parse((await got('https://dev.sp-tarkov.com/SPT-AKI/Server/raw/branch/development/project/assets/database/templates/items.json')).body);
}
fs.writeFileSync('./items.json', JSON.stringify(items, null, 4));
} catch (downloadError) {
if (error.message != 'stale') {
return Promise.reject(downloadError);
}
}
}
return items;
},
presets: async () => {
let presets = false;
try {
presets = JSON.parse(fs.readFileSync('./presets.json', 'utf8'));
const stats = fs.statSync('./presets.json');
if (Date.now() - stats.mtimeMs > 1000*60*60*2) {
throw new Error('stale');
}
} catch (error) {
try {
if (process.env.API_USERNAME && process.env.API_PASSWORD && process.env.SCANNER_NAME) {
presets = await api.getJson('globals.json').catch(() => { return false; });
if (presets) presets = presets['ItemPresets'];
}
if (!presets) {
console.log('Downloading SPT preset data');
presets = JSON.parse((await got('https://dev.sp-tarkov.com/SPT-AKI/Server/raw/branch/development/project/assets/database/globals.json')).body)['ItemPresets'];
}
fs.writeFileSync('./presets.json', JSON.stringify(presets, null, 4));
} catch (downloadError) {
if (error.message != 'stale') {
return Promise.reject(downloadError);
}
}
}
return presets;
},
td_presets: async () => {
let presets = false;
try {
presets = JSON.parse(fs.readFileSync('./item_presets.json', 'utf8'));
const stats = fs.statSync('./item_presets.json');
if (Date.now() - stats.mtimeMs > 1000*60*60*2) {
throw new Error('stale');
}
} catch (error) {
try {
if (process.env.API_USERNAME && process.env.API_PASSWORD && process.env.SCANNER_NAME) {
downloadedPresets = await api.getJson('presets.json').catch(() => { return false; });
if (downloadedPresets) presets = downloadedPresets;
}
if (!presets) throw new Error('Error downloading Tarkov.dev presets');
fs.writeFileSync('./item_presets.json', JSON.stringify(presets, null, 4));
} catch (downloadError) {
if (error.message != 'stale') {
return Promise.reject(downloadError);
}
}
}
return presets;
}
}