-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
137 lines (124 loc) · 6.81 KB
/
client.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
var cheerio = require('cheerio');
var moment = require('moment');
var request = require('request');
//var async = require('async');
var D3Leaderboards = function() {
var self = this;
this.defaultTop = 100;
this.requestOpts = {
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36'
}
};
this.baseURL = 'http://us.battle.net/d3/en/rankings/';
this.seasonURL = {
season: 'season/%era%/rift',
nonseason: 'era/%era%/rift',
seasonHardcore: 'season/%era%/rift-hardcore',
nonseasonHardcore: 'era/%era%/rift-hardcore',
};
this.eras = {1:'1', 2:'2', 3:'3', 4:'4', 5:'5'};
this.classes = {
1: 'barbarian',
2: 'monk',
3: 'dh',
4: 'wd',
5: 'wizard',
6: 'crusader'
};
this.teams = {
1: '%class%',
2: 'team-2',
3: 'team-3',
4: 'team-4'
}
this.setURL = function(team, seasonal, hardcore, era, clas) {
var url = this.baseURL;
if (seasonal && hardcore) url += this.seasonURL.seasonHardcore;
if (seasonal && !hardcore) url += this.seasonURL.season;
if (!seasonal && hardcore) url += this.seasonURL.nonseasonHardcore;
if (!seasonal && !hardcore) url += this.seasonURL.nonseason;
url += '-' + this.teams[team];
url = url.replace('%era%', this.eras[era]);
this.requestOpts.url = url.replace('%class%', this.classes[clas]);
console.log('requesting '+this.requestOpts.url);
}
this.parseHTML = function(html, cb) {
$ = cheerio.load(html);
var data = [];
$('#ladders-table tbody tr').map(function(i, link) {
var rank = parseInt(self.prettify($(link).find('.cell-Rank').attr('data-raw')));
if (typeof rank === 'undefined' || isNaN(rank)) return;
var j = {};
j.rank = rank;
j.battleTag = self.prettify($(link).find('.cell-BattleTag a').attr('href'));
j.characterGender = $(link).find('.cell-BattleTag img').attr('src');
j.characterGender = typeof j.characterGender === 'undefined' ? j.characterGender : j.characterGender.indexOf('female') > -1 ? 'f' : 'm'
j.riftLevel = parseInt(self.prettify($(link).find('.cell-RiftLevel').text()));
j.riftTime = parseInt(moment(self.prettify($(link).find('.cell-RiftTime').text()), "mm ss.SSS").format("x"));
j.completedTime = parseInt(moment(self.prettify($(link).find('.cell-CompletedTime').text()), "MMMM D, YYYY h:m A").format("X"));
data.push(j);
});
cb(data);
}
this.getPage = function(team, seasonal, hardcore, era, clas, cb) {
var self = this;
this.setURL(team, seasonal, hardcore, era, clas);
request(this.requestOpts, function(err, resp, html) {
if (err) {
console.error(err);
self.getPage(team, seasonal, hardcore, era, clas, cb);
} else {
self.parseHTML(html, function(json) {
cb(err, json);
});
}
});
};
this.prettify = function(str) {if (typeof str === 'undefined') {return str} else {str = str.replace('/d3/en/profile/', ''); return str.replace(/^\s+|\s+$|^\/|\/$/g, '');}}
this.SeasonalBarbarian = function(era, cb) {self.getPage(1, true, false, era, 1, cb);}
this.SeasonalMonk = function(era, cb) {self.getPage(1, true, false, era, 2, cb);}
this.SeasonalDemonHunter = function(era, cb) {self.getPage(1, true, false, era, 3, cb);}
this.SeasonalWitchDoctor = function(era, cb) {self.getPage(1, true, false, era, 4, cb);}
this.SeasonalWizard = function(era, cb) {self.getPage(1, true, false, era, 5, cb);}
this.SeasonalCrusader = function(era, cb) {self.getPage(1, true, false, era, 6, cb);}
this.SeasonalTeam2s = function(era, cb) {self.getPage(2, true, false, era, undefined, cb);}
this.SeasonalTeam3s = function(era, cb) {self.getPage(3, true, false, era, undefined, cb);}
this.SeasonalTeam4s = function(era, cb) {self.getPage(4, true, false, era, undefined, cb);}
this.SeasonalClass = function(era, clas, cb) {self.getPage(1, true, false, era, clas, cb);}
this.SeasonalTeam = function(era, team, cb) {self.getPage(team, true, false, era, undefined, cb);}
this.SeasonalHardcoreBarbarian = function(era, cb) {self.getPage(1, true, true, era, 1, cb);}
this.SeasonalHardcoreMonk = function(era, cb) {self.getPage(1, true, true, era, 2, cb);}
this.SeasonalHardcoreDemonHunter = function(era, cb) {self.getPage(1, true, true, era, 3, cb);}
this.SeasonalHardcoreWitchDoctor = function(era, cb) {self.getPage(1, true, true, era, 4, cb);}
this.SeasonalHardcoreWizard = function(era, cb) {self.getPage(1, true, true, era, 5, cb);}
this.SeasonalHardcoreCrusader = function(era, cb) {self.getPage(1, true, true, era, 6, cb);}
this.SeasonalHardcoreTeam2s = function(era, cb) {self.getPage(2, true, true, era, undefined, cb);}
this.SeasonalHardcoreTeam3s = function(era, cb) {self.getPage(3, true, true, era, undefined, cb);}
this.SeasonalHardcoreTeam4s = function(era, cb) {self.getPage(4, true, true, era, undefined, cb);}
this.SeasonalHardcoreClass = function(era, clas, cb) {self.getPage(1, true, true, era, clas, cb);}
this.SeasonalHardcoreTeam = function(era, team, cb) {self.getPage(team, true, true, era, undefined, cb);}
this.Barbarian = function(era, cb) {self.getPage(1, false, false, era, 1, cb);}
this.Monk = function(era, cb) {self.getPage(1, false, false, era, 2, cb);}
this.DemonHunter = function(era, cb) {self.getPage(1, false, false, era, 3, cb);}
this.WitchDoctor = function(era, cb) {self.getPage(1, false, false, era, 4, cb);}
this.Wizard = function(cb) {self.getPage(1, false, false, era, 5, cb);}
this.Crusader = function(era, cb) {self.getPage(1, false, false, era, 6, cb);}
this.Team2s = function(era, cb) {self.getPage(2, false, false, era, undefined, cb);}
this.Team3s = function(era, cb) {self.getPage(3, false, false, era, undefined, cb);}
this.Team4s = function(era, cb) {self.getPage(4, false, false, era, undefined, cb);}
this.Class = function(era, clas, cb) {self.getPage(1, false, false, era, clas, cb);}
this.Team = function(era, team, cb) {self.getPage(team, false, false, era, undefined, cb);}
this.HardcoreBarbarian = function(era, cb) {self.getPage(1, false, true, era, 1, cb);}
this.HardcoreMonk = function(era, cb) {self.getPage(1, false, true, era, 2, cb);}
this.HardcoreDemonHunter = function(era, cb) {self.getPage(1, false, true, era, 3, cb);}
this.HardcoreWitchDoctor = function(era, cb) {self.getPage(1, false, true, era, 4, cb);}
this.HardcoreWizard = function(era, cb) {self.getPage(1, false, true, era, 5, cb);}
this.HardcoreCrusader = function(era, cb) {self.getPage(1, false, true, era, 6, cb);}
this.HardcoreTeam2s = function(era, cb) {self.getPage(2, false, true, era, undefined, cb);}
this.HardcoreTeam3s = function(era, cb) {self.getPage(3, false, true, era, undefined, cb);}
this.HardcoreTeam4s = function(era, cb) {self.getPage(4, false, true, era, undefined, cb);}
this.HardcoreClass = function(era, clas, cb) {self.getPage(1, false, true, era, clas, cb);}
this.HardcoreTeam = function(era, team, cb) {self.getPage(team, false, true, era, undefined, cb);}
};
module.exports = new D3Leaderboards();