forked from alex7kom/node-steam-tradeoffers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
443 lines (386 loc) · 12.9 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
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
module.exports = SteamTradeOffers;
var request = require('request');
var cheerio = require('cheerio');
var Long = require('long');
var url = require('url');
var querystring = require('querystring');
require('util').inherits(SteamTradeOffers, require('events').EventEmitter);
function SteamTradeOffers() {
require('events').EventEmitter.call(this);
this._j = request.jar();
this._request = request.defaults({ jar: this._j });
}
SteamTradeOffers.prototype.setup = function(options, callback) {
this.sessionID = options.sessionID;
options.webCookie.forEach(function(name) {
setCookie.bind(this)(name);
}.bind(this));
if (options.PIN) {
return parentalUnlock.bind(this)(options.PIN, function(error) {
if (error) {
if (typeof callback == 'function') {
return callback(error);
} else {
throw error;
}
}
getAPIKey.bind(this)(callback);
}.bind(this));
}
getAPIKey.bind(this)(callback);
};
function parentalUnlock(PIN, callback) {
this._request.post({
uri: 'https://steamcommunity.com/parental/ajaxunlock',
json: true,
headers: {
referer: 'https://steamcommunity.com/'
},
form: {
pin: PIN
}
}, function(error, response, body) {
if (error || response.statusCode != 200) {
this.emit('debug', 'family view: ' + (error || response.statusCode));
return callback(error || new Error(response.statusCode));
}
if (!body || typeof body.success != 'boolean') {
this.emit('debug', 'family view: invalid response');
return callback(new Error('Invalid Response'));
}
if (!body.success) {
this.emit('debug', 'family view: incorrect PIN code');
return callback(new Error('Incorrect PIN'));
}
callback();
}.bind(this));
}
function getAPIKey(callback) {
if (this.APIKey) {
if (typeof callback == 'function') {
callback();
}
return;
}
this._request.get({
uri: 'https://steamcommunity.com/dev/apikey'
}, function(error, response, body) {
if (error || response.statusCode != 200) {
this.emit('debug', 'retrieving apikey: ' + (error || response.statusCode));
if (typeof callback == 'function') {
callback(error || new Error(response.statusCode));
}
return;
}
var $ = cheerio.load(body);
if ($('#mainContents h2').html() == 'Access Denied') {
this.emit('debug', 'retrieving apikey: access denied (probably limited account)');
var accessError = new Error('Access Denied');
if (typeof callback == 'function') {
return callback(accessError);
} else {
throw accessError;
}
}
if ($('#bodyContents_ex h2').html() == 'Your Steam Web API Key') {
var key = $('#bodyContents_ex p').html().split(' ')[1];
this.APIKey = key;
if (typeof callback == 'function') {
callback();
}
return;
}
this._request.post({
uri: 'https://steamcommunity.com/dev/registerkey',
form: {
domain: 'localhost',
agreeToTerms: 1
}
}, function(error, response, body) {
getAPIKey.bind(this)(callback);
}.bind(this));
}.bind(this));
}
SteamTradeOffers.prototype.getOfferToken = function(callback) {
this._request.get({
uri: 'https://steamcommunity.com/id/me/tradeoffers/privacy'
}, function(error, response, body) {
if (error || response.statusCode != 200) {
this.emit('debug', 'retrieving offer token: ' + (error || response.statusCode));
return callback(error || new Error(response.statusCode));
}
if (!body) {
this.emit('debug', 'retrieving offer token: invalid response');
return callback(new Error('Invalid Response'));
}
var $ = cheerio.load(body);
var offerUrl = $('input#trade_offer_access_url').attr("value");
var offerToken = url.parse(offerUrl, true).query.token;
callback(null, offerToken);
}.bind(this));
};
function setCookie(cookie) {
this._j.setCookie(request.cookie(cookie), 'https://steamcommunity.com');
}
SteamTradeOffers.prototype._loadInventory = function(inventory, uri, options, contextid, start, callback) {
options.uri = uri;
if (start) {
options.uri = options.uri + '&' + querystring.stringify({ 'start': start });
}
this._request.get(options, function(error, response, body) {
if (error || response.statusCode != 200) {
this.emit('debug', 'loading inventory: ' + (error || response.statusCode != 200));
return callback(error || new Error(response.statusCode));
}
if (!body || !body.rgInventory || !body.rgDescriptions || !body.rgCurrency) {
this.emit('debug', 'loading inventory: invalid response');
return callback(new Error('Invalid Response'));
}
inventory = inventory.concat(mergeWithDescriptions(body.rgInventory, body.rgDescriptions, contextid)
.concat(mergeWithDescriptions(body.rgCurrency, body.rgDescriptions, contextid)));
if (body.more) {
this._loadInventory(inventory, uri, options, contextid, body.more_start, callback);
} else {
callback(null, inventory);
}
}.bind(this));
};
SteamTradeOffers.prototype.loadMyInventory = function(options, callback) {
var query = {};
if (options.language) {
query.l = options.language;
}
if (options.tradableOnly !== false) {
query.trading = 1;
}
var uri = 'https://steamcommunity.com/my/inventory/json/' + options.appId + '/' + options.contextId + '/?' + querystring.stringify(query);
this._loadInventory([], uri, { json: true }, options.contextId, null, callback);
};
SteamTradeOffers.prototype.loadPartnerInventory = function(options, callback) {
var form = {
sessionid: this.sessionID,
partner: options.partnerSteamId,
appid: options.appId,
contextid: options.contextId
};
if (options.language) {
form.l = options.language;
}
var offer = 'new';
if (options.tradeOfferId) {
offer = options.tradeOfferId;
}
var uri = 'https://steamcommunity.com/tradeoffer/' + offer + '/partnerinventory/?' + querystring.stringify(form);
this._loadInventory([], uri, {
json: true,
headers: {
referer: 'https://steamcommunity.com/tradeoffer/' + offer + '/?partner=' + toAccountId(options.partnerSteamId)
}
}, options.contextId, null, callback);
};
function mergeWithDescriptions(items, descriptions, contextid) {
return Object.keys(items).map(function(id) {
var item = items[id];
var description = descriptions[item.classid + '_' + (item.instanceid || '0')];
for (var key in description) {
item[key] = description[key];
}
// add contextid because Steam is retarded
item.contextid = contextid;
return item;
});
}
function doAPICall(options) {
var params = {
uri: 'https://api.steampowered.com/IEconService/' + options.method + '/?key=' + this.APIKey + ((options.post) ? '' : '&' + querystring.stringify(options.params)),
json: true,
method: options.post ? 'POST' : 'GET'
};
if (options.post) {
params.form = options.params;
}
request(params, function(error, response, body) {
if (error || response.statusCode != 200) {
this.emit('debug', 'doing API call ' + options.method + ': ' + (error || response.statusCode));
if (typeof options.callback == 'function') {
options.callback(error || new Error(response.statusCode));
}
return;
}
if (!body || typeof body != 'object') {
this.emit('debug', 'doing API call ' + options.method + ': invalid response');
if (typeof options.callback == 'function') {
options.callback(new Error('Invalid Response'));
}
return;
}
if (typeof options.callback == 'function') {
options.callback(null, body);
}
}.bind(this));
}
SteamTradeOffers.prototype.getOffers = function(options, callback) {
doAPICall.bind(this)({
method: 'GetTradeOffers/v1',
params: options,
callback: function(error, res) {
if (error) {
return callback(error);
}
if (res.response.trade_offers_received !== undefined) {
res.response.trade_offers_received = res.response.trade_offers_received.map(function(offer) {
offer.steamid_other = toSteamId(offer.accountid_other);
return offer;
});
}
if (res.response.trade_offers_sent !== undefined) {
res.response.trade_offers_sent = res.response.trade_offers_sent.map(function(offer) {
offer.steamid_other = toSteamId(offer.accountid_other);
return offer;
});
}
callback(null, res);
}
});
};
SteamTradeOffers.prototype.getOffer = function(options, callback) {
doAPICall.bind(this)({
method: 'GetTradeOffer/v1',
params: options,
callback: function(error, res) {
if (error) {
return callback(error);
}
if (res.response.offer !== undefined) {
res.response.offer.steamid_other = toSteamId(res.response.offer.accountid_other);
}
callback(null, res);
}
});
};
SteamTradeOffers.prototype.getSummary = function(options, callback) {
doAPICall.bind(this)({
method: 'GetTradeOffersSummary/v1',
params: options,
callback: callback
});
};
SteamTradeOffers.prototype.declineOffer = function(options, callback) {
doAPICall.bind(this)({method: 'DeclineTradeOffer/v1', params: {tradeofferid: options.tradeOfferId}, post: true, callback: callback});
};
SteamTradeOffers.prototype.cancelOffer = function(options, callback) {
doAPICall.bind(this)({method: 'CancelTradeOffer/v1', params: {tradeofferid: options.tradeOfferId}, post: true, callback: callback});
};
SteamTradeOffers.prototype.acceptOffer = function(options, callback) {
if (options.tradeOfferId === undefined) {
if (typeof callback == 'function') {
callback(new Error('No options'));
}
return;
}
this._request.post({
uri: 'https://steamcommunity.com/tradeoffer/' + options.tradeOfferId + '/accept',
headers: {
referer: 'https://steamcommunity.com/tradeoffer/' + options.tradeOfferId + '/'
},
json: true,
form: {
sessionid: this.sessionID,
serverid: 1,
tradeofferid: options.tradeOfferId
}
}, function(error, response, body) {
if (error) {
this.emit('debug', 'accepting offer: ' + error);
if (typeof callback == 'function') {
callback(error);
}
return;
}
if (body && body.strError) {
this.emit('debug', 'accepting offer: ' + body.strError);
if (typeof callback == 'function') {
callback(new Error(body.strError));
}
return;
}
if (response.statusCode != 200) {
this.emit('debug', 'accepting offer: ' + response.statusCode);
if (typeof callback == 'function') {
callback(new Error(response.statusCode));
}
return;
}
if (typeof callback == 'function') {
callback(null, body);
}
}.bind(this));
};
function toSteamId(accountId) {
return new Long(parseInt(accountId, 10), 0x1100001).toString();
}
function toAccountId(steamId) {
return Long.fromString(steamId).toInt().toString();
}
SteamTradeOffers.prototype.makeOffer = function(options, callback) {
var tradeoffer = {
newversion: true,
version: 2,
me: { assets: options.itemsFromMe, currency: [], ready: false },
them: { assets: options.itemsFromThem, currency: [], ready: false }
};
var formFields = {
serverid: 1,
sessionid: this.sessionID,
partner: options.partnerSteamId || toSteamId(options.partnerAccountId),
tradeoffermessage: options.message || '',
json_tradeoffer: JSON.stringify(tradeoffer)
};
var query = {
partner: options.partnerAccountId || toAccountId(options.partnerSteamId)
};
if (options.accessToken !== undefined) {
formFields.trade_offer_create_params = JSON.stringify({ trade_offer_access_token: options.accessToken });
query.token = options.accessToken;
}
var referer;
if (options.counteredTradeOffer !== undefined) {
formFields.tradeofferid_countered = options.counteredTradeOffer;
referer = 'https://steamcommunity.com/tradeoffer/' + options.counteredTradeOffer + '/';
} else {
referer = 'https://steamcommunity.com/tradeoffer/new/?' + querystring.stringify(query);
}
this._request.post({
uri: 'https://steamcommunity.com/tradeoffer/new/send',
headers: {
referer: referer
},
json: true,
form: formFields
}, function(error, response, body) {
if (error) {
this.emit('debug', 'making an offer: ' + error);
if (typeof callback == 'function') {
callback(error);
}
return;
}
if (body && body.strError) {
this.emit('debug', 'making an offer: ' + body.strError);
if (typeof callback == 'function') {
callback(new Error(body.strError));
}
return;
}
if (response.statusCode != 200) {
this.emit('debug', 'making an offer: ' + response.statusCode);
if (typeof callback == 'function') {
callback(new Error(response.statusCode));
}
return;
}
if (typeof callback == 'function') {
callback(null, body);
}
}.bind(this));
};