Repository navigation
Expand file tree
/
Copy pathsteamlytics.js
More file actions
382 lines (311 loc) · 12.5 KB
/
Copy pathsteamlytics.js
File metadata and controls
382 lines (311 loc) · 12.5 KB
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
"use strict";
var request = require('ajax-request');
var Promise = require('bluebird');
function getJSON(url, callback) {
request(url, function(err, resp, data) {
if(!resp.headers)
return callback("invalid_response");
if(resp.headers.location)
return getJSON(resp.headers.location, callback);
var parsed_data;
try {
parsed_data = JSON.parse(data);
} catch(e) {
parsed_data = {success: false, message: "There was an error parsing the response from steamlytics, their servers may be down.", exception: e};
}
return callback(parsed_data);
});
}
/**
* Constructs a new Steamlytics object used to interact with the Steamlytics API. A test call is made when the object is constructed.
* @param {string} apiKey The API key used in order to make calls to the Steamlytics API.
* @param {function(Steamlytics, {api_plan:number, subscription_ends_at:number, calls_this_minute:number, calls_today:number}, SteamlyticsError)} readyCallback Callback is run when test call is complete and api is ready to be used, the argument is the api object.
*/
function Steamlytics(apiKey, readyCallback) {
this.apiKey = apiKey;
this.apiLevel = 0;
this.ready = false;
var csgo = {};
/**
* Gets the account information associated with this API key.
* @param {function(SteamlyticsError, Object)} callback The function to be called when the api call is complete.
*/
csgo.account = (callback) => {
return new Promise((resolve, reject) => {
getJSON(`http://api.steamlytics.xyz/v1/account?key=${this.apiKey}`, (data) => {
if(data.success) {
resolve(data);
return callback(null, data);
}
var error = new SteamlyticsError(data.message);
reject(error);
return callback(error, {});
});
});
}
/**
* Gets the pro-plan enabled pricelist (V2). If the account tied to the API key does not have pro-plan or higher, this will return an empty array.
*
* @param {string} [currency="$"] The currency code used for the api request. The default is 2001 (USD or $). View the list of valid currencies at http://steam.steamlytics.xyz/currencies#currencies
* @param {function(SteamlyticsError, Array<Object>)} callback The function to be called when the api call is complete.
*/
csgo.pricelist = (currency, callback) => {
return new Promise((resolve, reject) => {
var isCallbackFirst = typeof currency == 'function' || currency == undefined;
callback = checkCallback(isCallbackFirst ? currency : callback);
var error;
if(this.apiLevel < 2) {
error = new SteamlyticsError("Pro Plan Required");
reject(error);
return callback(error, []);
}
var currencyURL = isCallbackFirst ? `` : `¤cy=${currency}`;
getJSON(`http://api.csgo.steamlytics.xyz/v2/pricelist?key=${this.apiKey}${currencyURL}`, (data) => {
if(data.success) {
resolve(data.items);
return callback(null, data.items);
}
error = new SteamlyticsError(data.message);
reject(error);
return callback(error, []);
});
});
}
/**
* Gets the price of a single item.
*
* @param {string} market_hash_name The market_hash_name of the item.
* @param {object} [options] the additional parameters for the api call. See the API docs at http://csgo.steamlytics.xyz/api for more details.
* @param {function(SteamlyticsError, Object)} callback the function to be called when the api call is complete. See the API docs at http://csgo.steamlytics.xyz/api for more details.
*/
csgo.prices = (market_hash_name, options, callback) => {
return new Promise((resolve, reject) => {
var options_str = "";
// cuz fuck `if` statements
var callbackFirst = typeof options == 'function' || options == undefined;
callback = callbackFirst ? options : callback;
callback = checkCallback(callback);
options = callbackFirst ? {} : options;
if(options.from)
options_str += `&from=${options.from}`;
if(options.to)
options_str += `&to=${options.to}`;
if(options.on) {
if(options.from || options.to)
throw new SteamlyticsError('May not use "from" or "to" in conjunction with "on".');
options_str += `&on=${options.on}`;
}
if(options.currency && this.apiLevel >= 2)
options_str += `¤cy=${options.currency}`;
getJSON(`http://api.csgo.steamlytics.xyz/v1/prices/${market_hash_name}?key=${this.apiKey}${options_str}`, (data) => {
if(data.success) {
resolve(data);
return callback(null, data);
}
var error = new SteamlyticsError(data.message);
reject(error);
callback(error, {});
});
})
}
/**
* Gets a list of all items currently tracked in the Steamlytics database.
*
* @param {function(SteamlyticsError, {num_items:number, items:Object[]})} callback the function to be called when the api call is complete. See the api docs at http://csgo.steamlytics.xyz/api for more details.
*/
csgo.items = (callback) => {
callback = checkCallback(callback);
return new Promise((resolve, reject) => {
getJSON(`http://api.csgo.steamlytics.xyz/v1/items?key=${this.apiKey}`, (data) => {
if(data.success) {
resolve({num_items: data.num_items, items:data.items});
return callback(null, {num_items: data.num_items, items:data.items});
}
var error = new SteamlyticsError(data.message);
reject(error);
return callback(error, {num_items: 0, items: []});
});
});
}
/**
* Gets the current list of popular items
*
* @param {number} [limit] (optional) The limit for how many results to return
* @param {function(SteamlyticsError, {rank: number, market_hash_name: string, volume: number}[])} callback The callback for when the API call is complete. See the api docs at http://csgo.steamlytics.xyz/api for more details.
*/
csgo.items.popular = (limit, callback) => {
return new Promise((resolve, reject) => {
var callbackFirst = typeof limit == 'function' || limit == undefined;
callback = checkCallback(callbackFirst ? limit : callback);
var limit_str = callbackFirst ? `` : `&limit=${limit}`;
getJSON(`http://api.csgo.steamlytics.xyz/v1/items/popular?key=${this.apiKey}${limit_str}`, (data) => {
if(data.success) {
resolve(data.items);
return callback(null, data.items);
}
error = new SteamlyticsError(data.message);
reject(error);
return callback(error, []);
});
});
}
this.csgo = csgo;
var steam = {};
/**
* Gets the list of currencies Steamlytics has encountered on the community market
*
* @param {function(SteamlyticsError, Object)} callback The callback for when the API call is complete. The returned object is the table of currencies. See the docs at http://steam.steamlytics.xyz/api for details.
*/
steam.currencies = (callback) => {
getJSON(`http://api.steam.steamlytics.xyz/v1/currencies/?key=${this.apiKey}`, (data) => {
if(data.success)
return callback(null, data.currencies);
return callback(new SteamlyticsError(data.message), {});
})
}
/**
* Returns a table of exchange rates for the current day, based on the base currency ID. The base currency will always be shown in the table with an exchange rate of 1.
*
* @param {Object} options The options for the call, see the docs at http://steam.steamlytics.xyz/api for details.
* @param {function(SteamlyticsError, Object)} callback The callback for when the API call is complete. The returned object is the table of exchange rates relative to the base.
*/
steam.currencies.latest = (options, callback) => {
return new Promise((resolve, reject) => {
var callbackFirst = typeof options == 'function' || options == undefined;
callback = checkCallback(callbackFirst ? options : callback);
options = callbackFirst ? {} : options;
var options_str = ``;
if(options.base)
options_str += `&base=${options_base}`;
var error;
if(options.currencies) {
if(!options.currencies instanceof Array) {
error = new SteamlyticsError("options.currencies must be an Array");
reject(error);
return callback(error, {});
}
var currenciesStr = "";
options.currencies.forEach((element, index) => {
if(index != 0)
currenciesStr += ",";
currenciesStr += element;
});
options_str += `¤cies=${currenciesStr}`;
}
getJSON(`http://api.steam.steamlytics.xyz/v1/currencies/latest/?key=${this.apiKey}${options_str}`, (data) => {
if(data.success) {
resolve(data.rates);
return callback(null, data.rates);
}
error = new SteamlyticsError(data.message);
reject(error);
return callback(error, {});
});
});
}
/**
* Returns a table of exchange rates for the given date (YYYY-MM-DD), based on the base currency ID. The base currency will always be shown in the table with an exchange rate of 1.
*
* This API call is pro plan and up only.
*
* @param {Object} options The options for the call, see the docs at http://steam.steamlytics.xyz/api for details.
* @param {function(SteamlyticsError, Object)} callback The callback for when the API call is complete. The returned object is the table of exchange rates relative to the base.
*/
steam.currencies.historical = (date, options, callback) => {
return new Promise((resolve, reject) => {
var error;
if(this.apiLevel < 2) {
error = new SteamlyticsError("Pro Plan Required");
reject(error);
return callback(error, {});
}
if(!date.match(/\d{4}-(01|02|03|04|05|06|07|08|09|10|11|12)-(01|02|03|04|05|06|07|08|09|10|11|12|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31)/)) {
error = new SteamlyticsError("Malformed date, must be in format YYYY-MM-DD");
reject(error);
return callback(error, {});
}
var callbackFirst = typeof options == 'function' || options == undefined;
callback = checkCallback(callbackFirst ? options : callback);
options = callbackFirst ? {} : options;
var options_str = ``;
if(options.base)
options_str += `&base=${options_base}`;
if(options.currencies) {
if(!options.currencies instanceof Array) {
error = new SteamlyticsError("options.currencies must be an Array");
reject(error);
return callback(error, {});
}
var currenciesStr = "";
options.currencies.forEach((element, index) => {
if(index != 0)
currenciesStr += ",";
currenciesStr += element;
});
options_str += `¤cies=${currenciesStr}`;
}
getJSON(`http://api.steam.steamlytics.xyz/v1/currencies/historical/${date}?key=${this.apiKey}${options_str}`, (data) => {
if(data.success) {
resolve(data.rates);
return callback(null, data.rates);
}
error = new SteamlyticsError(data.message);
reject(error);
return callback(error, {});
});
});
}
/**
* Converts an amount from one currency to another. The response will also give you the exchange rate from the first currency to the second currency.
*
* @param {number} amount The amount of currency to convert
* @param {number|string} from_id The id to convert from
* @param {number|string} to_id The id to convert to
* @param {function(SteamlyticsError, {amount:number, rate:number})} callback The callback response from this api call. Includes the amount and the rate.
*/
steam.currencies.convert = (amount, from_id, to_id, callback) => {
return new Promise((resolve, reject) => {
callback = checkCallback(callback);
if(this.apiLevel < 3) {
reject(new SteamlyticsError("Enterprise Plan Required"));
return callback(new SteamlyticsError("Enterprise Plan Required"), {});
}
getJSON(`http://api.steam.steamlytics.xyz/v1/currencies/convert/${amount}/${from_id}/${to_id}?key=${this.apiKey}`, (data) => {
if(data.success) {
resolve({rate: data.rante, amount: data.amount});
return callback(null, {rate: data.rate, amount: data.amount});
}
reject(new SteamlyticsError(data.message));
return callback(new SteamlyticsError(data.message), {});
})
});
}
this.steam = steam;
csgo.account((err, data) => {
if(err) {
readyCallback(null, null, err);
return;
}
this.apiLevel = data.api_plan;
this.ready = true;
return readyCallback(this, data);
});
}
/**
* Used to make sure the callback isn't undefined, returns the callback if it isn't or an empty function if it is.
* @param {function} callback callback to checkCallback
*/
function checkCallback(callback) {
if(typeof callback == 'function')
return callback;
return () => {};
}
class SteamlyticsError {
constructor(message) {
Error.captureStackTrace(this, this.constructor);
this.name = this.constructor.name;
this.message = message;
}
}
exports.API = Steamlytics;
exports.SteamlyticsError = SteamlyticsError;