-
Notifications
You must be signed in to change notification settings - Fork 3
/
data-creater.js
215 lines (186 loc) · 8.42 KB
/
data-creater.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
'use strict'
const fs = require('fs')
const yargs = require('yargs');
const SwaggerClient = require('swagger-client');
const ESI_URL = 'https://esi.evetech.net/latest/swagger.json';
const argv = yargs
.option('corporations', {
alias: 'c',
description: 'get all NPC corporations and generate loyalty store offers json.',
type: 'boolean',
})
.option('cropdesc', {
alias: 'd',
description: 'generate NPC corporations name.',
type: 'boolean',
})
.option('itemdescription', {
alias: 'i',
description: 'generate loyalty store offers item description.',
type: 'boolean',
})
.help()
.alias('help', 'h')
.argv;
if (argv.corporations) {
// request ESI to get all NPC corporations
var hasLoyalty = [];
new SwaggerClient(ESI_URL).then(
client => {
client.apis.Corporation.get_corporations_npccorps()
.then(
ret => {
// request ESI to Check it has lp store or not
var promise = Promise.resolve();
var interval = 1000;
ret.body.forEach(element => {
promise = promise.then(function () {
client.apis.Loyalty.get_loyalty_stores_corporation_id_offers({ corporation_id: element })
.then(
offersRet => {
if (offersRet.data != "[]") {
console.log(element + ' has offers.');
hasLoyalty.push(element);
fs.access("./public/LPStore/" + element, fs.constants.F_OK, (err) => {
if (err)
fs.mkdirSync("./public/LPStore/" + element);
fs.open("./public/LPStore/" + element + "/loyalty.json", "w", (err, fd) => {
fs.write(fd, offersRet.data, (err, fd) => {
fs.close(fd, () => { });
});
});
});
}
else {
console.log(element + ' has offers,but is empty.');
}
},
reason => {
console.log(element + ' failed on api call: ' + reason.response.data)
}
)
return new Promise(function (resolve) {
setTimeout(resolve, interval);
});
});
});
promise.then(function () {
console.log('Finished.');
fs.open("./public/LPStore/corporation.json", "w", (err, fd) => {
fs.write(fd, JSON.stringify(hasLoyalty), (err, fd) => {
fs.close(fd, () => { });
});
});
});
},
reason => console.error('failed on api call: ' + reason)
)
},
reason => console.error('failed to load the spec: ' + reason)
);
}
if (argv.cropdesc) {
fs.open("./public/LPStore/corporation.json", "r+", (err, fd) => {
if (err) {
console.error("Can't find corporation.json,generate first");
return;
}
fs.readFile(fd, (err, data) => {
if (err) {
console.error("Read corporation.json error");
return;
}
fs.close(fd, () => { });
var corporations = JSON.parse(data);
new SwaggerClient(ESI_URL).then(
client => {
client.apis.Universe.post_universe_names(
{
ids: corporations
}
)
.then(
ret => {
fs.open("./public/LPStore/corporation_description.json", "w", (err, fd) => {
fs.write(fd, ret.data, (err, fd) => {
fs.close(fd, () => { });
});
});
},
reason => console.error('failed on api call: ' + reason)
);
},
reason => console.error('failed to load the spec: ' + reason)
);
});
});
}
if (argv.itemdescription) {
fs.open("./public/LPStore/corporation.json", "r+", (err, fd) => {
if (err) {
console.error("Can't find corporation.json,generate first");
return;
}
fs.readFile(fd, (err, data) => {
if (err) {
console.error("Read corporation.json error");
return;
}
fs.close(fd, () => { });
var corporationsID = JSON.parse(data);
var items = new Map();
new SwaggerClient(ESI_URL).then(
client => {
for (var i = 0; i < corporationsID.length; i++)
{
var element = corporationsID[i];
fd = fs.openSync("./public/LPStore/" + element + "/loyalty.json", "r+");
data = fs.readFileSync(fd);
fs.closeSync(fd);
var loyaltys = JSON.parse(data);
for (var j = 0; j < loyaltys.length; j++)
{
var loyalty = loyaltys[j];
items.set(loyalty.type_id, { name: "", desc: "" });
for (var k = 0; k < loyalty.required_items.length; k++)
{
var required_item = loyalty.required_items[k];
items.set(required_item.type_id, { name: "", desc: "" });
}
}
}
var promise = Promise.resolve();
var interval = 1000;
var index = 0;
var all = items.size;
items.forEach((value,key) => {
promise = promise.then(function () {
client.apis.Universe.get_universe_types_type_id({ "Accept-Language": "zh", language : "zh", type_id : key})
.then(
ret => {
index++;
console.log(index + '/' + all);
value.name = ret.body.name;
value.description = ret.body.description;
},
reason => {
console.log(element + ' failed on api call: ' + reason.response.data)
}
)
return new Promise(function (resolve) {
setTimeout(resolve, interval);
});
});
});
promise.then(function () {
console.log('Finished.');
fd = fs.openSync("./public/LPStore/loyalty_description.json", "w");
fs.writeSync(fd, JSON.stringify(Array.from(items.entries())));
fs.closeSync(fd);
});
},
reason => console.error('failed to load the spec: ' + reason)
);
});
});
}