-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathget_uniques - offical.js
689 lines (598 loc) · 19.3 KB
/
get_uniques - offical.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
var request = require("request");
var rp = require('request-promise');
var jsonfile = require('jsonfile');
var fs = require('fs');
var sleep = require('sleep');
var waitUntil = require('wait-until');
const util = require('util');
//var regex_hiddenmods = /(<br>)?.*\(Hidden\)(<br>)/i;
var regex_hiddenmods = /.*\(Hidden\).*/i;
var regex_wikilinks = /\[\[([^\]\|]*)\]\]|\[\[[^\]\|]*\|([^\]\|]*)\]\]/;
/*
matches formats "[[mod]]" or "[[wikipage|mod]] and stores "mod" as capture group 1 or 2, respectively.
Since only one capture group is filled each time, using both together in a replacement like r'\1\2' turns
both match variants into "mod".
*/
var regex_wiki_page_disamb = /([^\(]+) \([^\)]+\)/;
var regex_wiki_page_disamb_replace = /\s?\(.*\)$/;
/*
matches items named "item name (disambiguation)" and stores "item name" as capture group 1.
this format is used in the wiki to distinguish style variants of items, giving each variant its own page.
since the style variants ingame all have the same name, we want to filter these out and
put in a manually prepared version that covers all styles in one overview.
*/
var regex_single_range = /\+?\(((-?[\d\.]+)-([\d\.]+))\)%?/;
var regex_single_range_replace = /\((-?[\d\.]+-[\d\.]+)\)/;
/*
matches variants of the wiki's "(num-num)" format including the possibly leading "+" and trailing "%", such as:
(10-20)
+(10-20)
(10-20)%
(0.6-1)%
(0.6-0.8)%
(-40-40)% format found in ventor's gamble's rarity and some "flask charge used" mods
+(-25-50)% ventor's gamble again, now with resistances
The "num-num" part inside the brakets is stored as capture group 1
It intentionally leaves the leading "-" of mods like "-(20-10) Physical Damage Taken from Attacks"
The initial matching of double range damage mods like "Adds (10-20) to (30-40) Type Damage" is done
with another expression.
*/
var regex_double_range = /\(?(\d+)(?:-(\d+)\))? to \(?(\d+)(?:-(\d+)\))?/;
var regex_double_range_replace = /\(?(\d+)(?:-(\d+)\))? to \(?(\d+)(?:-(\d+)\))?/;
/*
matches the relevant variants for double range damage mods
(10-20) to (30-40)
15 to (30-40)
(10-20) to 35
15 to 35
Four named capture groups are used: lowmin, lowmax, highmin and highmax (numbers 10, 20, 30 and 40 above)
lowmax and/or highmax is None if the part is only a number and not a number range (cases 2-4 above; numbers 15 and 35)
*/
getItemClasses();
function getItemClasses() {
var classes = [];
var count = 0;
var urls = [];
var results = [];
var url_1 = "https://pathofexile.gamepedia.com/api.php?" +
'action=cargoquery' +
'&format=json' +
'&tables=items' +
'&fields=COUNT(DISTINCT items._pageName)=count, class' +
'&where=rarity="unique"' +
'&group_by=items.class' +
'&formatversion=1';
var url_2 = "https://pathofexile.gamepedia.com/api.php?" +
'action=cargoquery' +
'&format=json' +
'&tables=items' +
'&fields=class, tags' +
'&where=rarity="unique"' +
'&group_by=items.class' +
'&formatversion=1';
urls.push(encodeURI(url_1));
urls.push(encodeURI(url_2));
//console.log(urls);
urls.forEach(function(url_class) {
var options_class = {
uri: url_class,
headers: {
'User-Agent': 'Request-Promise'
},
json: true
};
rp(options_class)
.then(function (result) {
//console.log(util.inspect(result.cargoquery[0].title.class, false, null))
results.push(result.cargoquery);
count++;
})
.catch(function (err) {
count++;
console.log(err)
});
});
waitUntil()
.interval(500)
.times(Infinity)
.condition(function() {
return (urls.length == count ? true : false);
})
.done(function(result) {
var classes = mergeClassResults(results);
//console.log(util.inspect(classes, false, null));
scrape(classes);
});
}
function mergeClassResults(results) {
var merged = {};
results.forEach(function(result) {
result.forEach(function(c) {
if (!merged.hasOwnProperty(c.title.class)) {
merged[c.title.class] = {};
}
for (var key in c.title) {
if (key != "class") {
if (key == "tags") {
merged[c.title.class].type = [];
if (c.title[key].indexOf("weapon") > 0) {
merged[c.title.class].type.push("weapons");
}
if (c.title[key].indexOf("shield") > 0) {
merged[c.title.class].type.push("shields");
}
if (c.title[key].indexOf("armour") > 0) {
merged[c.title.class].type.push("armours");
}
} else {
merged[c.title.class][key] = c.title[key];
}
}
}
});
});
return merged;
}
function scrape(classes) {
// uniques - relics
var items = [[],[]];
var uniques = [];
var classCount = 0;
var requestCount = 0;
//console.log(classes)
for (var key in classes) {
classCount++;
resultLimit = 500;
var url_class = "https://pathofexile.gamepedia.com/api.php?" +
'action=cargoquery' +
'&format=json' +
'&limit=' + resultLimit +
'&tables=items';
var isWeapon = classes[key].type.indexOf("weapons") > -1 ? true : false;
var isArmour = classes[key].type.indexOf("armours") > -1 ? true : false;
var isShield = classes[key].type.indexOf("shields") > -1 ? true : false;
if (classes[key].type.length >= 1) {
classes[key].type.forEach(function(e) {
url_class += ',' + e;
});
}
url_class += '&fields=' +
'items.name, items.base_item, items.class, items.explicit_stat_text, items.implicit_stat_text';
if (isWeapon) {
url_class += ', weapons.attack_speed, weapons.attack_speed_range_maximum, weapons.attack_speed_range_minimum, weapons.attack_speed_range_text, ' +
'weapons.dps_range_maximum, weapons.dps_range_minimum, ' +
'weapons.physical_dps_range_maximum, weapons.physical_dps_range_minimum, ' +
'weapons.physical_damage_max_range_maximum, weapons.physical_damage_max_range_minimum, ' +
'weapons.physical_damage_min_range_maximum, weapons.physical_damage_min_range_minimum, ' +
'weapons.elemental_dps_range_maximum, weapons.elemental_dps_range_minimum';
}
if (isArmour) {
url_class += ', armours.evasion_range_maximum, armours.evasion_range_minimum, armours.energy_shield_range_maximum, armours.energy_shield_range_minimum, ' +
'armours.armour_range_maximum, armours.armour_range_minimum';
}
if (isShield) {
url_class += ', shields.block, shields.block_range_maximum, shields.block_range_minimum';
}
url_class += '&where=items.rarity="unique" AND items.class="' + key + '"';
if (key == "Maps") {
url_class += 'AND items.drop_enabled="1"';
}
if (classes[key].type.length >= 1) {
url_class += '&join_on=';
var i = 0;
classes[key].type.forEach(function(e) {
if (i > 0) {
url_class += ','
}
url_class += 'items._pageName=' + e + '._pageName';
i++;
});
}
url_class += '&group_by=items._pageName';
url_class += '&formatversion=1';
url_class = encodeURI(url_class);
var options_class = {
uri: url_class,
headers: {
'User-Agent': 'Request-Promise'
},
json: true
};
rp(options_class)
.then(function (result) {
//console.log(util.inspect(result.cargoquery[0].title.class, false, null))
uniques.push(result.cargoquery);
requestCount++;
})
.catch(function (err) {
requestCount++;
console.log(err)
});
}
waitUntil()
.interval(500)
.times(Infinity)
.condition(function() {
return (classCount == requestCount ? true : false);
})
.done(function(result) {
uniques = prepareItemObject(uniques);
uniques = get_item_mods(uniques);
uniques = get_item_stats(uniques);
uniques = mergeVariants(uniques);
uniques.sort(compareItemNames);
write_data_to_file('uniques', uniques);
});
}
function compareItemNames(a,b) {
if (a.name < b.name)
return -1;
if (a.name > b.name)
return 1;
return 0;
}
function prepareItemObject(result) {
var uniques = [];
result.forEach(function(e) {
e.forEach(function(el) {
var tmp = {}
tmp.properties = {};
for (var key in el.title) {
if (key == "name") {
tmp.name = el.title.name;
} else if (key == "base item") {
tmp["base"] = el.title["base item"];
} else if (key == "class") {
tmp["class"] = el.title["class"];
} else {
tmp.properties[key] = el.title[key];
}
}
uniques.push(tmp);
});
});
return uniques
}
function write_data_to_file(file, data) {
var file_name = 'output/' + file + '.json';
console.log(file_name);
try {
fs.unlinkSync(file_name);
} catch (err) {}
var out = {};
out[file] = data;
jsonfile.writeFile(file_name, out, function(err) {
if(err){
console.error(err)
}
else {
console.log('file ' + file_name + ' saved.');
}
})
}
function add_mods_to_item(mods_existing, mods_new) {
mods_new.forEach(function(e) {
var mod_exists = false;
mods_existing.forEach(function(n) {
if (e.name == n.name) {
mod_exists = true;
}
});
if (!mod_exists) {
mods_existing.push(e);
}
});
return mods_existing
}
function mergeVariants(uniques) {
//console.log()
//console.log("------------------------------")
var mergedUniques = [];
var found;
var mod_found;
uniques.forEach(function(e) {
found = false;
mergedUniques.forEach(function(u) {
if (e.name == u.name) {
found = true;
u.hasVariant = true;
e.mods.forEach(function(em) {
mod_found = false;
u.mods.forEach(function(um) {
if (em.name_orig == um.name_orig) {
mod_found = true;
}
});
if (!mod_found) {
u.mods.push(em);
}
});
}
});
if (!found) {
mergedUniques.push(e);
}
});
return mergedUniques
}
function get_item_stats(uniques) {
uniques.forEach(function(e) {
var stats = [];
var tmp = {};
var value;
/* defense */
// evasion
value = e.properties["evasion range maximum"];
if (typeof value !== "undefined" && value) {
tmp = {};
tmp.name = "Evasion Rating";
tmp.ranges = [[parseFloat(e.properties["evasion range minimum"]), parseFloat(e.properties["evasion range maximum"])]];
if (tmp.ranges[0][0] != tmp.ranges[0][1]) {
stats.push(tmp);
}
}
// energy shield
value = e.properties["energy shield range maximum"];
if (typeof value !== "undefined" && value) {
tmp = {};
tmp.name = "Energy Shield";
tmp.ranges = [ [parseFloat(e.properties["energy shield range minimum"]), parseFloat(e.properties["energy shield range maximum"])] ];
if (tmp.ranges[0][0] != tmp.ranges[0][1]) {
stats.push(tmp);
}
}
// armour
value = e.properties["armour range maximum"];
if (typeof value !== "undefined" && value) {
tmp = {};
tmp.name = "Armour";
tmp.ranges = [ [parseFloat(e.properties["armour range minimum"]), parseFloat(e.properties["armour range maximum"])] ];
if (tmp.ranges[0][0] != tmp.ranges[0][1]) {
stats.push(tmp);
}
}
/* offense */
// APS
minAPS = typeof e.properties["attack speed range minimum"] !== "undefined" ? e.properties["attack speed range minimum"] : 0;
maxAPS = typeof e.properties["attack speed range maximum"] !== "undefined" ? e.properties["attack speed range maximum"] : 0;
baseAPS = typeof e.properties["attack speed"] !== "undefined" ? e.properties["attack speed"] : 0;
//if (minAPS > 0 && maxAPS > 0 && (minAPS != baseAPS && maxAPS != baseAPS)) {
if (minAPS > 0 && maxAPS > 0 && (minAPS != maxAPS)) {
tmp = {};
tmp.name = "APS";
tmp.ranges = [ [ parseFloat(minAPS), parseFloat(maxAPS) ] ];
stats.push(tmp);
} else {
value = e.properties["attack speed range text"];
if (typeof value !== "undefined" && value.length) {
var match = (e.properties["attack speed range text"]).match(/([\d.]+) ?to ?([\d.]+)/);
tmp = {};
tmp.name = "APS";
if (match) {
tmp.ranges = [ [ parseFloat(match[1]), parseFloat(match[2]) ] ];
stats.push(tmp);
}
}
}
// physical damage ranges
value = e.properties["physical damage max range maximum"];
if (typeof value !== "undefined" && value) {
tmp = {};
tmp.name = "Damage";
tmp.ranges = [
[parseFloat(e.properties["physical damage min range minimum"]), parseFloat(e.properties["physical damage min range maximum"])],
[parseFloat(e.properties["physical damage max range minimum"]), parseFloat(e.properties["physical damage max range maximum"])]
];
if (tmp.ranges[0][0] != tmp.ranges[0][1] && tmp.ranges[1][0] != tmp.ranges[1][1]) {
stats.push(tmp);
}
}
// DPS
value = e.properties["dps range maximum"];
if (typeof value !== "undefined" && value) {
tmp = {};
tmp.name = "DPS";
tmp.ranges = [ [parseFloat(e.properties["dps range minimum"]), parseFloat(e.properties["dps range maximum"])] ];
if (tmp.ranges[0][0] != tmp.ranges[0][1]) {
stats.push(tmp);
}
}
// physical dps
value = e.properties["physical dps range maximum"];
if (typeof value !== "undefined" && value) {
tmp = {};
tmp.name = "Physical Dps";
tmp.ranges = [ [parseFloat(e.properties["physical dps range minimum"]), parseFloat(e.properties["physical dps range maximum"])] ];
if (tmp.ranges[0][0] != tmp.ranges[0][1]) {
stats.push(tmp);
}
}
// elemental dps
value = e.properties["elemental dps range maximum"];
if (typeof value !== "undefined" && value) {
tmp = {};
tmp.name = "Elemental Dps";
tmp.ranges = [ [parseFloat(e.properties["elemental dps range minimum"]), parseFloat(e.properties["elemental dps range maximum"])] ];
if (tmp.ranges[0][0] != tmp.ranges[0][1]) {
stats.push(tmp);
}
}
e.stats = stats;
delete e.properties;
});
return uniques
}
function get_item_mods(uniques) {
uniques.forEach(function(e) {
var mods = [];
var imp = [];
var implicit = {};
// split implicit, parse lines to remove hidden mods and join it again
var t_imp = remove_wiki_formats(e.properties["implicit stat text"]);
if (typeof t_imp !== "undefined") {
try {
t_imp = t_imp.split("<br>");
} catch (err) {
console.log(err)
}
t_imp.forEach(function(element, index) {
var tmp = cleanModString(element);
if (typeof tmp !== "undefined" && tmp.length) {
var t_index = mod_to_object(tmp);
if (t_index) {
imp.push(t_index);
}
}
});
implicit = imp.length ? imp : [];
e.implicit = implicit;
}
// remove table formattings in case the mod description is a table (list), example: Watcher's Eye
var regex_find_mod_table = /table.*?class.*?=/;
var regex_table_formatting_beginning_remove = /(.*?)<table.*?;td(>)?/;
var regex_table_formatting_end_remove = /(.*)(<\/td.*?)$/;
var match = e.properties["explicit stat text"].match(regex_find_mod_table);
if (match) {
e.hasVariant = true; // having a mod table means having variants
e.properties["explicit stat text"] = e.properties["explicit stat text"].replace(regex_table_formatting_beginning_remove, '$1');
e.properties["explicit stat text"] = e.properties["explicit stat text"].replace(regex_table_formatting_end_remove, '$1');
}
// split explicit mod block to single mods
var t_mods = remove_wiki_formats(e.properties["explicit stat text"]);
if (typeof t_mods !== "undefined") {
t_mods = t_mods.split(/<.*?>/).clean("");
t_mods.forEach(function(element, index) {
var tmp = cleanModString(element);
if (typeof tmp !== "undefined" && tmp.length) {
var t_mod = mod_to_object(tmp);
if (t_mod) {
mods.push(t_mod);
}
}
});
e.mods = mods;
}
});
return uniques
}
function mod_to_object(string) {
var mod = {};
mod.name_orig = string;
var match = string.match(regex_double_range);
if (match) {
mod.isVariable = true;
// double range
if (match[1] && match[2] && match[3] && match[4]) {
// (10-20) to (30-40)
mod.ranges = [];
mod.ranges.push( [parseFloat(match[1]), parseFloat(match[2])] );
mod.ranges.push( [parseFloat(match[3]), parseFloat(match[4])] );
//mod.name_alt = string.replace(regex_double_range_replace, '#');
mod.name = string.replace(regex_double_range_replace, '# to #');
} else if (match[1] && match[2] && match[3]) {
// (10-20) to 35
mod.ranges = [];
mod.ranges.push( [parseFloat(match[1]), parseFloat(match[2])] );
mod.ranges.push( [parseFloat(match[3])] );
//mod.name_alt = string.replace(regex_double_range_replace, '#');
mod.name = string.replace(regex_double_range_replace, '# to #');
} else if (match[1] && match[3] && match[4]) {
// 15 to (30-40)
mod.ranges = [];
// 1 to (x -x) lightning damage
if (parseFloat(match[1] != 1)) {
mod.ranges.push( [parseFloat(match[1])] );
//mod.name_alt = string.replace(regex_double_range_replace, '#');
mod.name = string.replace(regex_double_range_replace, '# to #');
} else {
//mod.name_alt = string.replace(regex_double_range_replace, '1 to #');
mod.name = string.replace(regex_double_range_replace, '# to #');
}
mod.ranges.push( [parseFloat(match[3]), parseFloat(match[4])] );
} else if (match[1] && match[3]) {
// 15 to 35
mod.values = [];
mod.values.push( parseFloat(match[1]), parseFloat(match[3]) );
mod.isVariable = false;
//mod.name_alt = string.replace(regex_double_range_replace, '#');
mod.name = string.replace(regex_double_range_replace, '# to #');
}
} else if (match = string.match(regex_single_range)) {
// single range
mod.ranges = [];
//mod.name_alt = string.replace(regex_single_range_replace, '#');
mod.name = string.replace(regex_single_range_replace, '#');
mod.ranges.push( [parseFloat(match[2]), parseFloat(match[3])] );
mod.isVariable = true;
} else {
// single value, no range
var regex_single_value = /([\d\.]+)/g;
//mod.name_alt= string.replace(regex_single_value, '#');
mod.name = string.replace(regex_single_value, '#');
mod.values = [];
if (match = string.match(regex_single_value)) {
for (var i = 0; i < match.length; i++) {
mod.values.push( parseFloat(match[i]) );
}
}
mod.isVariable = false;
}
//mod.name_alt = remove_wiki_formats(mod.name_alt);
mod.name = remove_wiki_formats(mod.name);
mod.name_orig = remove_wiki_formats(mod.name_orig);
/*
console.log("name_orig :", mod.name_orig);
console.log("name_alt :", mod.name_alt);
console.log("name :", mod.name);
console.log("ranges :", mod.ranges);
console.log("isVariable :", mod.isVariable);
console.log("--------------------------------------------------------");
*/
return mod
}
function cleanModString(string) {
string = remove_hidden_mods(string);
return string;
}
function remove_wiki_formats(text) {
if (typeof text === "undefined") {
return
}
while (text.match(regex_wikilinks)) {
text = text.replace(regex_wikilinks, '$1$2');
}
text = text.replace('<em class="tc -corrupted">Corrupted</em>', '');
text = text.replace('<em class="tc -corrupted">Corrupted</em>', '');
text = text.replace('<br/>', '');
text = text.replace('<', '<').replace('>', '>');
text = text.replace('&#60;', '<').replace('&#62;', '>');
text = text.replace(/^&##;/, '<');
text = text.replace(/&##;$/, '>');
text = text.replace(/(<.*)&##;/, '$1>');
text = text.replace('<br />', '`n');
return text;
}
function remove_hidden_mods(text) {
if (typeof text === "undefined") {
return
}
return text.replace(regex_hiddenmods, '');
}
function item_exists(name, items) {
var i = 0;
items.forEach(function(element, index) {
if (element.name == name) {
i = index;
}
});
return i
}
Array.prototype.clean = function(deleteValue) {
for (var i = 0; i < this.length; i++) {
if (this[i] == deleteValue) {
this.splice(i, 1);
i--;
}
}
return this;
};