-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNorthrend.js
500 lines (400 loc) · 22.1 KB
/
Northrend.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
const request = require('request')
const cheerio = require('cheerio')
const o2csv = require('objects-to-csv')
// FIRST ID OF NORTHREND: 79825180
// LAST ID OF NORTHREND: 79902172
// IDs for testing: Solo: 79855545 Tournament 1v1: 79662654 AT 2v2: 79659425 1min game: 79673355
var gamesParsed = 0;
var gamesNotFound = 0;
var gamesNotSolo = 0;
var badGame = 0;
var timeOuts = 0;
var baseUrl = 'http://classic.battle.net/war3/ladder/w3xp-game-detail.aspx?Gateway=Northrend&GameID='
id = 79825180
var url = baseUrl + id
getHttp()
async function getHttp() {
if (id === 79902172) {
printResults();
return;
}
url = baseUrl + id
await request({
url: url,
json: false
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(body)
var numberOfPlayers = $('.rankingRowLeft').length / 3;
var pagetitle = $('title').html();
if (pagetitle == 'Frozen Throne Game Detail') {
//console.log('Game page found, parsing game ID: ' + id + ' / Gametype is: ' + $(".playerStatsDataLeft").eq(2).text())
} else if (pagetitle == 'Warcraft III Ladder - Error') {
console.log('No game with id: ' + id + ' found.')
gamesNotFound++
if (gamesNotFound > 10) {
console.log('10 games were not found, cancelling scrape.')
printResults()
return;
}
} else {
console.log('unknown error, page title found to be: ' + pagetitle)
}
// If the game is a 1v1 game (either type: 'Solo' or 'Tournament') we can proceed
if (numberOfPlayers == 2) { // if this is a "Solo" game
// scrape the game length
var gameLength = $(".playerStatsDataLeft").eq(3).text()
// regex for any "digit"
var r = /\d+/;
// .match(/\d+/) returns an array where the first element is the number that was found
// check if game is 3 minutes or longer, and therefore probably not a joinbug or a lossbot, also included a 90min limit
if (gameLength.match(r)[0] >= 3 && gameLength.match(r)[0] <= 90) {
// get player names
p1name = $(".rankingRowLeft").eq(0).text()
p2name = $(".rankingRowLeft").eq(3).text()
// get player races
matchup = formatRaces();
// flag win and lose
var p1win;
var p2win;
if ($(".rankingRowLeft").eq(2).text() == 'Win') {
// p1 wins
p1win = true;
p2win = false;
} else if ($(".rankingRowLeft").eq(5).text() == 'Win') {
// p2 wins
p2win = true;
p1win = false;
}
// save the game date
var date = regexDate($(".playerStatsDataLeft").eq(0).text())
// save the game time
var time = regexTime($(".playerStatsDataLeft").eq(0).text())
// save the game length
var length = gameLength.match(r)[0]
// save the map
var map = $(".playerStatsDataLeft").eq(1).text();
// get the player colours (needed for scores)
var colours = getColours();
// get overview score HTML references
p1scoreClass = String('.gameDetail' + colours[0]);
p2scoreClass = String('.gameDetail' + colours[1]);
// get overview scores
var overviewScores = getOverviewScores(p1scoreClass,p2scoreClass);
// get unit scores
var unitScores = getUnitScores(p1scoreClass,p2scoreClass)
// get heroes & hero scores
var heroes = getHeroes(p1scoreClass,p2scoreClass)
var heroScores = getHeroScores(p1scoreClass,p2scoreClass)
// get resource scores
var resourceScores = getResourceScores(p1scoreClass, p2scoreClass)
createScrapedData();
getHttp();
// ------------------------------------------------------------------------------------------- //
// CONSOLE LOG TESTS
//console.log(scrapedData)
// ------------------------------------------------------------------------------------------- //
// FUNCTIONS
function createScrapedData() {
scrapedData = {};
scrapedData.gameID = id-1;
scrapedData.gameLength = length;
scrapedData.map = map;
scrapedData.matchup = Object.values(matchup[4]).toString().replace(/[.,\/",()]/g,"");
scrapedData.winningPlayer = getWinner();
scrapedData.date = date;
scrapedData.time = time;
scrapedData.gameType = $(".playerStatsDataLeft").eq(2).text(); // should only return 'Solo' or 'Tournament'
// PLAYER 1
scrapedData.p1name = p1name;
scrapedData.p1colour = colours[0];
scrapedData.p1race = $(".rankingRowLeft").eq(1).text()
scrapedData.p1numberOfHeroes = countHeroes(heroes[0])
// P1 Overview Scores
scrapedData.p1overviewUnitScore = getValue(Object.values(overviewScores[0][0]))
scrapedData.p1overviewHeroScore = getValue(Object.values(overviewScores[0][1]))
scrapedData.p1overviewResourceScore = getValue(Object.values(overviewScores[0][2]))
scrapedData.p1overviewTotalScore = getValue(Object.values(overviewScores[0][3]))
// P1 Unit scores
scrapedData.p1unitsProduced = getValue(Object.values(unitScores[0][0]))
scrapedData.p1unitsKilled = getValue(Object.values(unitScores[0][1]))
scrapedData.p1buildingsProduced = getValue(Object.values(unitScores[0][2]))
scrapedData.p1buildingsRazed = getValue(Object.values(unitScores[0][3]))
scrapedData.p1largestArmy = getValue(Object.values(unitScores[0][4]))
// P1 Heroes
setP1HeroesToNull(scrapedData)
// checks how many heroes were scraped, then loops that many times to add the hero data
for (i=0;i<scrapedData.p1numberOfHeroes;i++) {
if (i=1) {
scrapedData.p1FirstHeroName = heroes[0][0]
scrapedData.p1FirstHeroLevel = heroes[0][1]
} if (i=2) {
scrapedData.p1SecondHeroName = heroes[0][2]
scrapedData.p1SecondHeroLevel = heroes[0][3]
} if (i=3) {
scrapedData.p1ThirdHeroName = heroes[0][4]
scrapedData.p1ThirdHeroLevel = heroes[0][5]
}
}
// P1 Hero Scores
scrapedData.p1heroesKilled = getValue(Object.values(heroScores[0][0]))
scrapedData.p1itemsObtained = getValue(Object.values(heroScores[0][1]))
scrapedData.p1mercenariesHired = getValue(Object.values(heroScores[0][2]))
scrapedData.p1experienceGained = getValue(Object.values(heroScores[0][3]))
// P1 Resource scores
scrapedData.p1goldMined = getValue(Object.values(resourceScores[0][0]))
scrapedData.p1lumberHarvested = getValue(Object.values(resourceScores[0][1]))
scrapedData.p1resourcesTraded = getValue(Object.values(resourceScores[0][2]))
scrapedData.p1techPercentage = getValue(Object.values(resourceScores[0][3].replace(/\ /,"")))
scrapedData.p1goldLostToUpkeep = getValue(Object.values(resourceScores[0][4]))
// PLAYER 2
scrapedData.p2name = p2name;
scrapedData.p2colour = colours[1];
scrapedData.p2race = $(".rankingRowLeft").eq(4).text()
scrapedData.p2numberOfHeroes = countHeroes(heroes[1])
// P2 Overview scores
scrapedData.p2overviewUnitScore = getValue(Object.values(overviewScores[1][0]))
scrapedData.p2overviewHeroScore = getValue(Object.values(overviewScores[1][1]))
scrapedData.p2overviewResourceScore = getValue(Object.values(overviewScores[1][2]))
scrapedData.p2overviewTotalScore = getValue(Object.values(overviewScores[1][3]))
// P2 Unit scores
scrapedData.p2unitsProduced = getValue(Object.values(unitScores[1][0]))
scrapedData.p2unitsKilled = getValue(Object.values(unitScores[1][1]))
scrapedData.p2buildingsProduced = getValue(Object.values(unitScores[1][2]))
scrapedData.p2buildingsRazed = getValue(Object.values(unitScores[1][3]))
scrapedData.p2largestArmy = getValue(Object.values(unitScores[1][4]))
// P2 Heroes
setP2HeroesToNull(scrapedData)
for (i=0;i<scrapedData.p2numberOfHeroes;i++) {
if (i=1) {
scrapedData.p2FirstHeroName = heroes[1][0]
scrapedData.p2FirstHeroLevel = heroes[1][1]
} if (i=2) {
scrapedData.p2SecondHeroName = heroes[1][2]
scrapedData.p2SecondHeroLevel = heroes[1][3]
} if (i=3) {
scrapedData.p2ThirdHeroName = heroes[1][4]
scrapedData.p2ThirdHeroLevel = heroes[1][5]
}
}
// P2 Hero Scores
scrapedData.p2heroesKilled = getValue(Object.values(heroScores[0][0]))
scrapedData.p2itemsObtained = getValue(Object.values(heroScores[0][1]))
scrapedData.p2mercenariesHired = getValue(Object.values(heroScores[0][2]))
scrapedData.p2experienceGained = getValue(Object.values(heroScores[0][3]))
// P2 Resource scores
scrapedData.p2goldMined = getValue(Object.values(resourceScores[1][0]))
scrapedData.p2lumberHarvested = getValue(Object.values(resourceScores[1][1]))
scrapedData.p2resourcesTraded = getValue(Object.values(resourceScores[1][2]))
scrapedData.p2techPercentage = getValue(Object.values(resourceScores[1][3].replace(/\ /,"")))
scrapedData.p2goldLostToUpkeep = getValue(Object.values(resourceScores[1][4]))
outputData = [scrapedData];
const csvOutput = new o2csv(outputData);
csvOutput.toDisk('./northrend.csv', {append: true});
gamesParsed++
console.log('GameID: ' + id + ' - Successfully parsed and saved, ' + gamesParsed + ' games scraped so far')
}
function setP1HeroesToNull(d) {
d.p1FirstHeroName = null
d.p1FirstHeroLevel = null
d.p1SecondHeroName = null
d.p1SecondHeroLevel = null
d.p1ThirdHeroName = null
d.p1ThirdHeroLevel = null
}
function setP2HeroesToNull(d) {
d.p2FirstHeroName = null
d.p2FirstHeroLevel = null
d.p2SecondHeroName = null
d.p2SecondHeroLevel = null
d.p2ThirdHeroName = null
d.p2ThirdHeroLevel = null
}
function countHeroes(heroesArray) {
return Object.keys(heroesArray).length / 2;
}
// removes , and " from strings that are like: '"35,010"' which is how the scores are brought in from cheerio
function getValue(units) {
var tempString = units.toString().replace(/[.,\/",()]/g,"");
return tempString;
}
// players are either player '1' or player '2' in solos
function getWinner() {
if (p1win) {return 1}
else {return 2}
}
function getHeroes(p1scoreClass,p2scoreClass) {
var r = /alt="(.*)" | (\d)/g
p1heroes = []
p2heroes = []
var i = 1;
var j = 1;
while (regexMatch = r.exec($(p1scoreClass).eq(2).html())) {
if (i%2 == 1) {
p1heroes.push(regexMatch[1])
} else {
p1heroes.push(regexMatch[2])
}
i++
}
while (regexMatch = r.exec($(p2scoreClass).eq(2).html())) {
if (j%2 == 1) {
p2heroes.push(regexMatch[1])
} else {
p2heroes.push(regexMatch[2])
}
j++
}
return [p1heroes,p2heroes]
}
function getUnitScores(p1scoreClass,p2scoreClass) {
var unitScores = []
var p1 = []
var p2 = []
for (i=0;i<5;i++) {
p1[i] = $(p1scoreClass).eq(1).children().eq(i+1).text();
p2[i] = $(p2scoreClass).eq(1).children().eq(i+1).text();
}
unitScores = [p1,p2]
return unitScores
}
function getHeroScores(p1scoreClass,p2scoreClass) {
var heroScores = []
var p1 = []
var p2 = []
for (i=0;i<4;i++) {
p1[i] = $(p1scoreClass).eq(2).children().eq(i+2).text();
p2[i] = $(p2scoreClass).eq(2).children().eq(i+2).text();
}
heroScores = [p1,p2]
return heroScores
}
function getResourceScores(p1scoreClass,p2scoreClass) {
var resourceScores = []
var p1 = []
var p2 = []
for (i=0;i<5;i++) {
p1[i] = $(p1scoreClass).eq(3).children().eq(i+1).text();
p2[i] = $(p2scoreClass).eq(3).children().eq(i+1).text();
}
resourceScores = [p1,p2]
return resourceScores
}
function getOverviewScores(p1scoreClass,p2scoreClass) {
var player1Scores = []
var player2Scores = []
player1Scores[0] = $(p1scoreClass).children().eq(1).text();
player1Scores[1] = $(p1scoreClass).children().eq(2).text();
player1Scores[2] = $(p1scoreClass).children().eq(3).text();
player1Scores[3] = $(p1scoreClass).children().eq(4).text();
player2Scores[0] = $(p2scoreClass).children().eq(1).text();
player2Scores[1] = $(p2scoreClass).children().eq(2).text();
player2Scores[2] = $(p2scoreClass).children().eq(3).text();
player2Scores[3] = $(p2scoreClass).children().eq(4).text();
return [player1Scores,player2Scores]
}
function regexDate(string) {
var date_regex = /\d{1,2}\/\d{1,2}\/\d{4}/;
var a = string.match(date_regex);
return a[0]
}
function regexTime(string) {
var time_regex = /\d{1,2}\:\d{1,2}\:\d{1,2}\ [A-Z]{2}/;
var a = string.match(time_regex);
return a[0]
}
function formatRaces() {
var r1;
var r2;
var p1rand = false;
var p2rand = false;
switch ($(".rankingRowLeft").eq(1).text()) {
case 'Orc':
r1 = 'O'
break;
case 'Human':
r1 = 'H'
break;
case 'Night Elf':
r1 = 'N'
break;
case 'Undead':
r1 = 'U'
break;
case 'Random (Orc)':
r1 = 'O'
p1rand = true;
break;
case 'Random (Human)':
r1 = 'H'
p1rand = true;
break;
case 'Random (Night Elf)':
r1 = 'N'
p1rand = true;
break;
case 'Random (Undead)':
r1 = 'U'
p1rand = true;
break;
}
switch ($(".rankingRowLeft").eq(4).text()) {
case 'Orc':
r2 = 'O'
break;
case 'Human':
r2 = 'H'
break;
case 'Night Elf':
r2 = 'N'
break;
case 'Undead':
r2 = 'U'
break;
case 'Random (Orc)':
r2 = 'O'
p2rand = true;
break;
case 'Random (Human)':
r2 = 'H'
p2rand = true;
break;
case 'Random (Night Elf)':
r2 = 'N'
p2rand = true;
break;
case 'Random (Undead)':
r2 = 'U'
p2rand = true;
break;
}
return [r1, p1rand, r2 , p2rand, String(r1 + 'v' + r2)]
}
function getColours() {
var r = /[^.]*/;
p1colour = $('td[class=rankingRow]').html().slice(44).match(r)[0];
p2colour = $('td[class=rankingRow]').parent().next().children().html().slice(44).match(r)[0];
return [p1colour,p2colour]
}
} else {
badGame++
console.log('GameID: ' + id + ' - Game was found, but it is either <3 minutes, or >90 minutes / ' + badGame + ' number of games found in that category')
getHttp()
}
} else {
gamesNotSolo++
console.log('GameID: ' + id + ' - Game was found, but it is not a 1v1 game, game is type: ' + $(".playerStatsDataLeft").eq(2).text() + " / " + gamesNotSolo + " non-solo games found so far")
getHttp();
}
} else {
timeOuts++
console.log('GameID: ' + id + ' - Request did not complete, error: ' + error + ' / Number of timeouts: ' + timeOuts)
printResults()
}
}
)
id++
}
function printResults() {
console.log('Scraping Complete!\n' + gamesParsed + ' games parsed and saved.\n' + gamesNotFound + ' games not found\n' + gamesNotSolo + ' non-1v1 games found\n' + badGame + ' games found that were <3 or >90 mins (not counted)\n' + timeOuts + ' timeouts\n');
}