forked from WFCD/warframe-wikia-scrapers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
258 lines (227 loc) · 6.96 KB
/
index.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
'use strict';
const axios = require('axios');
const cmd = require('node-cmd');
const fs = require('fs-extra');
const cheerio = require('cheerio');
const transformWeapon = require('./transformers/transformWeapon');
const transformWarframe = require('./transformers/transformWarframe');
let imageUrls;
let imageFUrls;
const getLuaFrameData = async () => {
try {
const { data } = await axios.get('http://warframe.wikia.com/wiki/Module:Warframes/data?action=edit');
const $ = cheerio.load(data);
return $('#wpTextbox1').text();
} catch (err) {
console.error('Failed to fetch latest warframe data:');
console.error(err);
return '';
}
};
const convertFrameDataToJson = async (luaFramedata) => {
const scriptlines = luaFramedata.split('\n');
// Remove return statement
const modifiedScript = scriptlines
.slice(0, scriptlines.length - 2)
.join('\n');
// Add JSON conversion
const luaToJsonScript = `
JSON = (loadfile "JSON.lua")()\n
${modifiedScript}\n
print(JSON:encode(WarframeData))
`;
// Run updated JSON lua script
if (!await fs.exists('./tmp')) {
await fs.mkdir('./tmp');
}
await fs.writeFile('./tmp/framedataToJson.lua', luaToJsonScript, {
encoding: 'utf8',
flag: 'w',
});
try {
await new Promise((resolve, reject) => cmd.get('lua ./tmp/framedataToJson.lua > ./tmp/framedataraw.json', (err) => {
if (!err) {
resolve();
} else {
reject(err);
throw (new Error(err));
}
}));
} catch (err) {
console.error('Failed to execute modified lua script:');
console.error(err);
}
const warframedataRaw = await fs.readFile('./tmp/framedataraw.json', 'UTF-8');
return warframedataRaw;
};
const getWarframeImageUrls = async (warframes) => {
const titles = [];
Object.keys(warframes).forEach((warframeName) => {
titles.push(`File:${warframes[warframeName].Image}`);
});
// Split titles into batches of 50, the max allowed by the wikimedia API
const titleBatches = [];
while (titles.length > 0) {
titleBatches.push(titles.splice(0, 50));
}
const urlRequests = titleBatches.map(titleBatch =>
axios.get('http://warframe.wikia.com/api.php', {
params: {
action: 'query',
titles: titleBatch.join('|'),
prop: 'imageinfo',
iiprop: 'url',
format: 'json',
},
}));
try {
const fetchedImageUrls = await Promise.all(urlRequests).then((res) => {
const urls = {};
res.forEach(({ data }) => {
Object.keys(data.query.pages).forEach((id) => {
if (id > -1) {
const title = data.query.pages[id].title.replace('File:', '');
const { url } = data.query.pages[id].imageinfo[0];
urls[title] = url;
}
});
});
return urls;
});
return fetchedImageUrls;
} catch (err) {
console.error('Failed to fetch image URLs:');
console.error(err);
return [];
}
};
async function mainF() {
const luaFramedata = await getLuaFrameData();
const warframedata = JSON.parse(await convertFrameDataToJson(luaFramedata));
imageFUrls = await getWarframeImageUrls(warframedata.Warframes);
if (!await fs.exists('./transformers/tmp')) {
await fs.mkdir('./transformers/tmp');
}
let warframes = [];
try {
const genFrames = await Promise.all(Object.keys(warframedata.Warframes)
.map(async warframeName =>
transformWarframe(warframedata.Warframes[warframeName], imageFUrls)));
warframes = genFrames.filter(warframe => typeof warframe !== 'undefined');
} catch (e) {
console.error(e);
}
if (!await fs.exists('./build')) {
await fs.mkdir('./build');
}
fs.writeFile('./build/framedatafinal.json', JSON.stringify(warframes));
fs.remove('./tmp');
fs.remove('./transformers/tmp');
}
const getLuaWeaponData = async () => {
try {
const { data } = await axios.get('http://warframe.wikia.com/wiki/Module:Weapons/data?action=edit');
const $ = cheerio.load(data);
return $('#wpTextbox1').text();
} catch (err) {
console.error('Failed to fetch latest weapon data:');
console.error(err);
return '';
}
};
const convertWeaponDataToJson = async (luaWeapondata) => {
const scriptlines = luaWeapondata.split('\n');
// Remove return statement
const modifiedScript = scriptlines
.slice(0, scriptlines.length - 2)
.join('\n');
// Add JSON conversion
const luaToJsonScript = `
JSON = (loadfile "JSON.lua")()\n
${modifiedScript}\n
print(JSON:encode(WeaponData))
`;
// Run updated JSON lua script
if (!await fs.exists('./tmp')) {
await fs.mkdir('./tmp');
}
await fs.writeFile('./tmp/weapondataToJson.lua', luaToJsonScript, {
encoding: 'utf8',
flag: 'w',
});
try {
await new Promise((resolve, reject) => cmd.get('lua ./tmp/weapondataToJson.lua > ./tmp/weapondataraw.json', (err) => {
if (!err) {
resolve();
} else {
reject(err);
throw (new Error(err));
}
}));
} catch (err) {
console.error('Failed to execute modified lua script:');
console.error(err);
}
const weapondataRaw = await fs.readFile('./tmp/weapondataraw.json', 'UTF-8');
return weapondataRaw;
};
const getWeaponImageUrls = async (weapons) => {
const titles = [];
Object.keys(weapons).forEach((weaponName) => {
titles.push(`File:${weapons[weaponName].Image}`);
});
// Split titles into batches of 50, the max allowed by the wikimedia API
const titleBatches = [];
while (titles.length > 0) {
titleBatches.push(titles.splice(0, 50));
}
const urlRequests = titleBatches.map(titleBatch =>
axios.get('http://warframe.wikia.com/api.php', {
params: {
action: 'query',
titles: titleBatch.join('|'),
prop: 'imageinfo',
iiprop: 'url',
format: 'json',
},
}));
try {
const fetchedImageUrls = await Promise.all(urlRequests).then((res) => {
const urls = {};
res.forEach(({ data }) => {
Object.keys(data.query.pages).forEach((id) => {
if (id > -1) {
const title = data.query.pages[id].title.replace('File:', '');
const { url } = data.query.pages[id].imageinfo[0];
urls[title] = url;
}
});
});
return urls;
});
return fetchedImageUrls;
} catch (err) {
console.error('Failed to fetch image URLs:');
console.error(err);
return [];
}
};
async function main() {
const luaWeapondata = await getLuaWeaponData();
const weapondata = JSON.parse(await convertWeaponDataToJson(luaWeapondata));
if (!await fs.exists('./build')) {
await fs.mkdir('./build');
}
imageUrls = await getWeaponImageUrls(weapondata.Weapons);
let weapons = [];
try {
weapons = Object.keys(weapondata.Weapons).map(weaponName =>
transformWeapon(weapondata.Weapons[weaponName], imageUrls))
.filter(weapon => typeof weapon !== 'undefined');
} catch (e) {
console.error(e);
}
fs.writeFile('./build/weapondatafinal.json', JSON.stringify(weapons));
mainF();
}
main();