-
Notifications
You must be signed in to change notification settings - Fork 0
/
medals.js
83 lines (63 loc) · 1.57 KB
/
medals.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
const got = require('got');
const fs = require('fs');
const path = require('path');
const os = require('os');
const filename = path.join(__dirname, 'medals.csv');
var options = {
headers: {
"AS-Key": "key-here"
},
responseType: 'json'
};
function getArray(obj) {
var keys = Object.keys(obj);
var arr = [];
for (var i = 0; i < keys.length; i++) {
var o = obj[keys[i]];
o.name = keys[i];
arr.push(o);
}
return arr;
}
got('https://api.agent-stats.com/medals', options)
.then(r => {
var data = JSON.parse(r.body);
delete data.ap;
var arr = getArray(data);
var csv_arr = [];
var keys = ['name'];
for (var i = 0; i < arr.length; i++) {
var k = Object.keys(arr[i]);
for (var j = 0; j < k.length; j++) {
if (keys.indexOf(k[j]) == -1)
keys.push(k[j]);
}
}
var line_header = [];
for (var j = 0; j < keys.length; j++) {
var el = ''+keys[j];
if (!el)
line_header.push('');
else if (el.indexOf(';') != -1 || el.indexOf('"') != -1)
line_header.push('"' + el + '"');
else
line_header.push(el);
}
csv_arr.push(line_header.join(';'));
for (var i = 0; i < arr.length; i++) {
var o = arr[i];
var line = [];
for (var j = 0; j < keys.length; j++) {
var el = ''+o[keys[j]];
if (!el)
line.push('');
else if (el.indexOf(';') != -1 || el.indexOf('"') != -1)
line.push('"' + el + '"');
else
line.push(el);
}
csv_arr.push(line.join(';'));
}
fs.writeFileSync(filename, csv_arr.join('\n'));
})
.catch(e => console.log(e));