-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
258 lines (230 loc) · 9.03 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
/**
* @fileOverview Weasel - a node.js wrapper module for the <a href="https://www.warcraftlogs.com/v1/docs">Warcraft Logs API</a>
* @author pintapoff@GitHub
* @version 1.0.0
* @license MIT
* @example
* //install with "npm install weasel.js"
* const api = require('weasel.js');
*
* //Set the public WCL api-key that you get from https://www.warcraftlogs.com/accounts/changeuser
* api.setApiKey('abcd123abcd123abcd123abcd123abcd123');
*
* //Optional parameters for the api call.
* var params = {};
*
* //Call the function to list guild reports, can be filtered on start time and end time as a UNIX timestamp with the optional parameters @params.
* api.getReportsGuild('carpe cerevisi', 'moonglade', 'eu', params, function(err, data) {
*
* if (err) {
* //We caught an error, log the error object to the console and exit.
* console.log(err);
* return;
* }
* //Success, log the whole data object to the console.
* console.log(data);
*});
*/
'use strict';
var https = require("https");
var exports = module.exports = {},
apiKey = '';
/**
* Serialize a parameters-object into a querystring format.
*
* @param {Object} obj
* @return {String} querystring
* @private
*/
function serializeParamString(obj) {
var str = [];
for (var p in obj)
if (obj.hasOwnProperty(p)) {
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
}
return str.join("&");
}
/**
* Performs the actual data request from the WCL API service.
*
* @param {string} path - The api path
* @param {object} params - Set of optional parameters
* @param {apiCallback} callback - A callback to run
* @private
*/
function getData(path, params, callback) {
var qs = !params ? {} : params;
qs.api_key = apiKey;
qs = serializeParamString(qs);
var options = {
"method": "GET",
"hostname": "www.warcraftlogs.com",
"port": 443,
"path": "/v1" + encodeURI(path) + "?" + qs,
"headers": {
"cache-control": "no-cache"
}
};
var req = https.request(options, function(res) {
var parts = [];
req.on('error', function(err) {
callback(err);
});
res.on("data", function(part) {
parts.push(part);
});
res.on("end", function() {
var data = Buffer.concat(parts);
data = JSON.parse(data);
if (res.statusCode < 200 || res.statusCode >= 300) {
callback(new Error(res.statusCode + ' - ' + data.error));
}
else {
callback(null, data);
}
});
});
req.end();
}
/**
* A callback to run with the response and/or error from the WCL API call.
*
* @callback apiCallback
* @param {Error} error - I case of an error it contains a new Error with the error message
* @param {object} data - The data(JSON) return from the API
* @private
*/
/**
* Sets your Warcraft Logs API-key which is needed to use the WCL API calls.
*
* @param {string} key - your api-key from https://www.warcraftlogs.com/accounts/changeuser
* @public
*/
exports.setApiKey = function(key) {
if (key && key.trim() != "") {
apiKey = key;
}
};
/**
* Gets a list of available zones used throughout the API.
* Check out {@link https://www.warcraftlogs.com/v1/docs#!/Zones/zones_get| this} section of the API for full coverage of what is returned.
*
* @param {apiCallback} callback - A callback to run
* @public
*/
exports.getZones = function(callback) {
getData('/zones', null, callback);
};
/**
* Gets a list of available classes used throughout the API.
* Check out {@link https://www.warcraftlogs.com/v1/docs#!/Classes/classes_get| this} section of the API for full coverage of what is returned.
*
* @param {apiCallback} callback - A callback to run
* @public
*/
exports.getClasses = function(callback) {
getData('/classes', null, callback);
};
/**
* Gets data concerning rankings of a specific encounter.
* Check out {@link https://www.warcraftlogs.com/v1/docs#!/Rankings/rankings_encounter_encounterID_get| this} section of the API for full coverage of what is returned and what parameters are available.
*
* @param {int} encounterID - an integer representing an encounter
* @param {object} params - optional parameters
* @param {apiCallback} callback - A callback to run
* @public
*/
exports.getRankingsEncounter = function(encounterID, params, callback) {
getData('/rankings/encounter/' + encounterID.toString(), params, callback);
};
/**
* Gets ranked fights for a specific character.
* Check out {@link https://www.warcraftlogs.com/v1/docs#!/Rankings/rankings_character_characterName_serverName_serverRegion_get| this} section of the API for full coverage of what is returned and what parameters are available.
*
* @param {string} characterName - the character name
* @param {string} serverName - the server name
* @param {string} serverRegion - the server region
* @param {object} params - optional parameters
* @param {apiCallback} callback - A callback to run
* @public
*/
exports.getRankingsCharacter = function(characterName, serverName, serverRegion, params, callback) {
getData('/rankings/character/' + characterName + '/' + serverName + '/' + serverRegion, params, callback);
};
/**
* Gets parses for a specific character.
* Check out {@link https://www.warcraftlogs.com/v1/docs#!/Parses/parses_character_characterName_serverName_serverRegion_get| this} section of the API for full coverage of what is returned and what parameters are available.
*
* @param {string} characterName - the character name
* @param {string} serverName - the server name
* @param {string} serverRegion - the server region
* @param {object} params - optional parameters
* @param {apiCallback} callback - A callback to run
* @public
*/
exports.getParsesCharacter = function(characterName, serverName, serverRegion, params, callback) {
getData('/parses/character/' + characterName + '/' + serverName + '/' + serverRegion, params, callback);
};
/**
* Gets reports for a specific guild.
* Check out {@link https://www.warcraftlogs.com/v1/docs#!/Reports/reports_guild_guildName_serverName_serverRegion_get| this} section of the API for full coverage of what is returned and what parameters are available.
*
* @param {string} guildName - the guild name
* @param {string} guildServer - the server name
* @param {string} guildRegion - the guilds region
* @param {object} params - optional parameters
* @param {apiCallback} callback - A callback to run
* @public
*/
exports.getReportsGuild = function(guildName, guildServer, guildRegion, params, callback) {
getData('/reports/guild/' + guildName + '/' + guildServer + '/' + guildRegion, params, callback);
};
/**
* Gets reports for a specific user.
* Check out {@link https://www.warcraftlogs.com/v1/docs#!/Reports/reports_guild_guildName_serverName_serverRegion_get| this} section of the API for full coverage of what is returned and what parameters are available.
*
* @param {string} userName - the username
* @param {object} params - optional parameters
* @param {apiCallback} callback - A callback to run
* @public
*/
exports.getReportsUser = function(userName, params, callback) {
getData('/reports/user' + userName, params, callback);
};
/**
* Gets fights and the participants for a specific report.
* Check out {@link https://www.warcraftlogs.com/v1/docs#!/Report/report_fights_code_get| this} section of the API for full coverage of what is returned and what parameters are available.
*
* @param {string} code - the report code
* @param {object} params - optional parameters
* @param {apiCallback} callback - A callback to run
* @public
*/
exports.getReportFights = function(code, params, callback) {
getData('/report/fights/' + code, params, callback);
};
/**
* Gets events such as damage, healing, cast, buff and debuff events for a specific report.
* Check out {@link https://www.warcraftlogs.com/v1/docs#!/Report/report_events_code_get| this} section of the API for full coverage of what is returned and what parameters are available.
*
* @param {string} code - the report code
* @param {object} params - optional parameters
* @param {apiCallback} callback - A callback to run
* @public
*/
exports.getReportEvents = function(code, params, callback) {
getData('/report/events/' + code, params, callback);
};
/**
* Gets a table of entries for a specific report. More risk of being changed than the other calls because it follows the changes on the table panes over at the site. Read the API docs.
* Check out {@link https://www.warcraftlogs.com/v1/docs#!/Report/report_tables_view_code_get| this} section of the API for full coverage of what is returned and what parameters are available.
*
* @param {string} view - The type of data requested.
* @param {string} code - the report code
* @param {object} params - optional parameters
* @param {apiCallback} callback - A callback to run
* @public
*/
exports.getReportTables = function(view, code, params, callback) {
getData('/report/tables/' + view + '/' + code, params, callback);
};