-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbetterSteamWrapper.js
79 lines (62 loc) · 2.18 KB
/
betterSteamWrapper.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
"use strict";
var url = require('url'),
https = require('https');
// The npm package `steam-api` sucks and doesn't pass exceptions on to your error handler
// in some cases, so you get unhandled exceptions and the program crashes. To hell with that.
var templateUrl = "https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v2/";
class betterSteamWrapper {
constructor (apiKey) {
this.apiKey = apiKey;
}
getPlayerInfo(steamIds, callback) {
if (typeof callback !== "function") { throw "no callback given"; }
var steamUrl = this.getUrl(steamIds);
this.doRequest(steamUrl, callback);
}
getBulkPlayerInfo(steamIds, callback) {
if (Array.isArray(steamIds) == false || typeof callback !== "function") {
console.error("invalid args to getBulkPlayerInfo");
return;
}
// clone array so that our modifications don't affect the caller's copy
steamIds = steamIds.slice(0);
while (steamIds.length > 0) {
var thisBatch = steamIds.splice(0,100);
this.getPlayerInfo(thisBatch, callback);
}
}
doRequest(url, callback) {
var request = https.request(url, (res) => {
var data = "";
res.on("data", (d) => {
data += d; // TODO: do this less shit
});
res.on("end", () => {
if (res.statusCode !== 200) { return callback("Unepected status code from steam API: " + res.statusCode); }
var response;
try {
response = JSON.parse(data);
} catch (e) { return callback(e); }
response.response.players.forEach((p) => {
callback(null, p);
});
});
});
request.on("error", (err) => {
console.error("Failed to complete steam API request", err);
callback(err);
});
request.end();
}
getUrl(steamIdArray) {
if (Array.isArray(steamIdArray) == false || steamIdArray.length >100) {
throw "Steam ID Array must be an array and less than 101 values";
}
var steamUrl = url.parse(templateUrl, true);
steamUrl.query.key = this.apiKey;
steamUrl.query.format = "json";
steamUrl.query.steamids = steamIdArray.join(",");
return url.format(steamUrl);
}
}
module.exports = betterSteamWrapper;