Compare commits

...

25 Commits

Author SHA1 Message Date
Alexander Corn
0069bb36ef 3.19.0-beta2 2016-03-04 19:36:42 -05:00
Alexander Corn
1028c4193f Added HTTP source 2016-03-04 19:35:04 -05:00
Alexander Corn
991b11e0f4 3.19.0-beta1 2016-03-04 18:51:39 -05:00
Alexander Corn
72c01468da Want node v4.0.0 or later 2016-03-04 18:27:50 -05:00
Alexander Corn
eecd682033 Added unified interface for HTTP requests with pre/post hooks 2016-03-04 18:26:47 -05:00
Alexander Corn
574a3127cb 3.18.7 2016-03-04 00:19:31 -05:00
Alexander Corn
55027f7b1f Merge pull request #87 from andrewda/patch-1
Fixed crash when handling an inventory issue
2016-03-04 00:18:17 -05:00
Andrew Dassonville
70ffc5559d Renamed body.error to body.Error 2016-03-03 19:22:05 -08:00
Andrew Dassonville
f4be134f71 Fixed crash when handling an inventory issue
```
/bots/node_modules/steamcommunity/components/users.js:265
                                callback(new Error(body.Error || "Malformed response"));
                                                       ^

TypeError: Cannot read property 'Error' of undefined
    at Request._callback (/bots/node_modules/steamcommunity/components/users.js:265:28)
    at Request.self.callback (/bots/node_modules/steamcommunity/node_modules/request/request.js:199:22)
    at emitTwo (events.js:100:13)
    at Request.emit (events.js:185:7)
    at Request.<anonymous> (/bots/node_modules/steamcommunity/node_modules/request/request.js:1036:10)
    at emitOne (events.js:95:20)
    at Request.emit (events.js:182:7)
    at IncomingMessage.<anonymous> (/bots/node_modules/steamcommunity/node_modules/request/request.js:963:12)
    at emitNone (events.js:85:20)
    at IncomingMessage.emit (events.js:179:7)
```
2016-03-03 13:42:05 -08:00
Alexander Corn
7792969cae Merge pull request #54 from Mikxail/bugfix-market-item-price
bugfix market-item price
2016-02-29 16:44:17 -05:00
Alexander Corn
57a3532860 3.18.6 2016-02-29 16:42:00 -05:00
Alexander Corn
facf335f0e Fixed captcha overriding more important errors (fixes #56) 2016-02-29 16:41:37 -05:00
Alexander Corn
1c3817b797 Fixed captcha URL (fixes #83) 2016-02-29 16:24:29 -05:00
Alexander Corn
74efd50f72 Added more informative error messages for RSA key retrieval failure 2016-02-06 23:48:26 -05:00
Alexander Corn
87e3665b01 Revised JSDoc language (#80) 2016-02-04 21:49:54 -05:00
Alexander Corn
0b94419de4 Merge pull request #76 from Aareksio/patch-1
Fix for issue #75
2016-01-25 11:47:34 -05:00
Arkadiusz Sygulski
defbd347cc Fix for issue #75
In received XML there's `isPrimary="0"`, which mostly likely causes the problem, which results the last group in xml to be set as primary. 
This should fix it.
2016-01-25 11:35:22 +01:00
Alexander Corn
7efc1f2fd0 3.18.5 2016-01-21 01:15:00 -05:00
Alexander Corn
05c2da89da Fixed crash when unable to check confirmations 2016-01-21 01:14:52 -05:00
Alexander Corn
b83c40419c 3.18.4 2016-01-20 23:05:07 -05:00
Alexander Corn
fb0097499d Added some more debug output to confirmation checker 2016-01-20 23:04:54 -05:00
Alexander Corn
c2983e3bcd RIP market and trade confirmation opt-out 2016-01-20 13:47:14 -05:00
mikxail
26f27e2698 remove unused price calculating block
and fix lowestPrice value
2016-01-05 21:58:49 +03:00
Mikhail Konovalov
eae78aab93 remove options argument for updatePrice method 2015-12-28 15:58:31 +03:00
Mikhail Konovalov
5c2894a7d4 bugfix market-item price
and add currency option for .getMarketItem(appid, hashName, currency, cb)
2015-12-27 16:51:11 +03:00
16 changed files with 353 additions and 217 deletions

View File

@@ -1,9 +1,13 @@
var SteamCommunity = require('../index.js');
var Cheerio = require('cheerio');
SteamCommunity.prototype.getMarketItem = function(appid, hashName, callback) {
SteamCommunity.prototype.getMarketItem = function(appid, hashName, currency, callback) {
if (typeof currency == "function") {
callback = currency;
currency = 1;
}
var self = this;
this.request("https://steamcommunity.com/market/listings/" + appid + "/" + encodeURIComponent(hashName), function(err, response, body) {
this.httpRequest("https://steamcommunity.com/market/listings/" + appid + "/" + encodeURIComponent(hashName), function(err, response, body) {
if(self._checkHttpError(err, response, callback)) {
return;
}
@@ -13,23 +17,21 @@ SteamCommunity.prototype.getMarketItem = function(appid, hashName, callback) {
callback(new Error("There are no listings for this item."));
return;
}
var item = new CMarketItem(self, body, $);
if(item.commodity) {
item.updatePrice(function(err) {
if(err) {
callback(err);
} else {
callback(null, item);
}
});
} else {
callback(null, item);
}
});
var item = new CMarketItem(appid, hashName, self, body, $);
item.updatePrice(currency, function(err) {
if(err) {
callback(err);
} else {
callback(null, item);
}
});
}, "steamcommunity");
};
function CMarketItem(community, body, $) {
function CMarketItem(appid, hashName, community, body, $) {
this._appid = appid;
this._hashName = hashName;
this._community = community;
this._$ = $;
@@ -38,6 +40,12 @@ function CMarketItem(community, body, $) {
if(match) {
this._country = match[1];
}
this._language = "english";
match = body.match(/var g_strLanguage = "([^"]+)";/);
if(match) {
this._language = match[1];
}
this.commodity = false;
var match = body.match(/Market_LoadOrderSpread\(\s*(\d+)\s*\);/);
@@ -62,35 +70,42 @@ function CMarketItem(community, body, $) {
// ignore
}
}
this.quantity = 0;
this.lowestPrice = 0;
if(!this.commodity) {
var total = $('#searchResults_total');
if(total) {
this.quantity = parseInt(total.text().replace(/[^\d]/g, '').trim(), 10);
}
var lowest = $('.market_listing_price.market_listing_price_with_fee');
if(lowest[0]) {
this.lowestPrice = parseInt($(lowest[0]).text().replace(/[^\d]/g, '').trim(), 10);
this.firstAsset = null;
this.assets = null;
match = body.match(/var g_rgAssets = (.*);/);
if (match) {
try {
this.assets = JSON.parse(match[1]);
this.assets = this.assets['730']['2'];
this.firstAsset = this.assets[Object.keys(this.assets)[0]];
} catch (e) {
// ignore
}
}
this.quantity = 0;
this.lowestPrice = 0;
// TODO: Buying listings and placing buy orders
}
CMarketItem.prototype.updatePrice = function(callback) {
CMarketItem.prototype.updatePrice = function (currency, callback) {
if (this.commodity) {
this.updatePriceForCommodity(currency, callback);
} else {
this.updatePriceForNonCommodity(currency, callback);
}
};
CMarketItem.prototype.updatePriceForCommodity = function(currency, callback) {
if(!this.commodity) {
throw new Error("Cannot update price for non-commodity item");
}
// TODO: Currency option maybe?
var self = this;
this._community.request({
"uri": "https://steamcommunity.com/market/itemordershistogram?country=US&language=english&currency=1&item_nameid=" + this.commodityID,
"json": true,
this._community.httpRequest({
"uri": "https://steamcommunity.com/market/itemordershistogram?country=US&language=english&currency=" + currency + "&item_nameid=" + this.commodityID,
"json": true
}, function(err, response, body) {
if(self._community._checkHttpError(err, response, callback)) {
return;
@@ -122,5 +137,49 @@ CMarketItem.prototype.updatePrice = function(callback) {
if(callback) {
callback(null);
}
});
}, "steamcommunity");
};
CMarketItem.prototype.updatePriceForNonCommodity = function (currency, callback) {
if(this.commodity) {
throw new Error("Cannot update price for commodity item");
}
var self = this;
this._community.httpRequest({
"uri": "https://steamcommunity.com/market/listings/" +
this._appid + "/" +
encodeURIComponent(this._hashName) +
"/render/?query=&start=0&count=10&country=US&language=english&currency=" + currency,
"json": true
}, function(err, response, body) {
if (self._community._checkHttpError(err, response, callback)) {
return;
}
if (body.success != 1) {
callback && callback(new Error("Error " + body.success));
return;
}
var match = body.total_count;
if (match) {
self.quantity = parseInt(match, 10);
}
var lowestPrice;
var $ = Cheerio.load(body.results_html);
match = $(".market_listing_price.market_listing_price_with_fee");
if (match) {
for (var i = 0; i < match.length; i++) {
lowestPrice = parseFloat($(match[i]).text().replace(",", ".").replace(/[^\d.]/g, ''));
if (!isNaN(lowestPrice)) {
self.lowestPrice = lowestPrice;
break;
}
}
}
callback && callback(null);
}, "steamcommunity");
};

View File

@@ -27,7 +27,7 @@ SteamCommunity.prototype.marketSearch = function(options, callback) {
qs.count = 100;
qs.sort_column = 'price';
qs.sort_dir = 'asc';
performSearch.call(this, this.request, qs, [], callback);
performSearch.call(this, this.httpRequest, qs, [], callback);
};
function performSearch(request, qs, results, callback) {
@@ -71,7 +71,7 @@ function performSearch(request, qs, results, callback) {
qs.start += body.pagesize;
performSearch.call(self, request, qs, results, callback);
}
});
}, "steamcommunity");
}
function CMarketSearchResult(row) {

View File

@@ -12,7 +12,7 @@ SteamCommunity.prototype.getSteamGroup = function(id, callback) {
}
var self = this;
this.request("https://steamcommunity.com/" + (typeof id === 'string' ? "groups/" + id : "gid/" + id.toString()) + "/memberslistxml/?xml=1", function(err, response, body) {
this.httpRequest("https://steamcommunity.com/" + (typeof id === 'string' ? "groups/" + id : "gid/" + id.toString()) + "/memberslistxml/?xml=1", function(err, response, body) {
if(self._checkHttpError(err, response, callback)) {
return;
}
@@ -29,7 +29,7 @@ SteamCommunity.prototype.getSteamGroup = function(id, callback) {
callback(null, new CSteamGroup(self, result.memberList));
});
});
}, "steamcommunity");
};
function CSteamGroup(community, groupData) {

View File

@@ -12,7 +12,7 @@ SteamCommunity.prototype.getSteamUser = function(id, callback) {
}
var self = this;
this.request("http://steamcommunity.com/" + (typeof id === 'string' ? "id/" + id : "profiles/" + id.toString()) + "/?xml=1", function(err, response, body) {
this.httpRequest("http://steamcommunity.com/" + (typeof id === 'string' ? "id/" + id : "profiles/" + id.toString()) + "/?xml=1", function(err, response, body) {
if(self._checkHttpError(err, response, callback)) {
return;
}
@@ -48,7 +48,7 @@ SteamCommunity.prototype.getSteamUser = function(id, callback) {
callback(null, new CSteamUser(self, result.profile, customurl));
});
});
}, "steamcommunity");
};
function CSteamUser(community, userData, customurl) {
@@ -90,7 +90,7 @@ function CSteamUser(community, userData, customurl) {
var self = this;
if(userData.groups && userData.groups[0] && userData.groups[0].group) {
this.groups = userData.groups[0].group.map(function(group) {
if(group['$'] && group['$'].isPrimary) {
if(group['$'] && group['$'].isPrimary === "1") {
self.primaryGroup = new SteamID(group.groupID64[0]);
}

View File

@@ -48,7 +48,7 @@ SteamCommunity.prototype.chatLogon = function(interval, uiMode) {
return;
}
self.request.post({
self.httpRequestPost({
"uri": "https://api.steampowered.com/ISteamWebUserPresenceOAuth/Logon/v1",
"form": {
"ui_mode": uiMode,
@@ -82,7 +82,7 @@ SteamCommunity.prototype.chatLogon = function(interval, uiMode) {
self.chatState = SteamCommunity.ChatState.LoggedOn;
self.emit('chatLoggedOn');
self._chatPoll();
});
}, "steamcommunity");
});
};
@@ -103,7 +103,7 @@ SteamCommunity.prototype.chatMessage = function(recipient, text, type, callback)
type = type || 'saytext';
var self = this;
this.request.post({
this.httpRequestPost({
"uri": "https://api.steampowered.com/ISteamWebUserPresenceOAuth/Message/v1",
"form": {
"access_token": this._chat.accessToken,
@@ -127,12 +127,12 @@ SteamCommunity.prototype.chatMessage = function(recipient, text, type, callback)
} else {
callback(null);
}
});
}, "steamcommunity");
};
SteamCommunity.prototype.chatLogoff = function() {
var self = this;
this.request.post({
this.httpRequestPost({
"uri": "https://api.steampowered.com/ISteamWebUserPresenceOAuth/Logoff/v1",
"form": {
"access_token": this._chat.accessToken,
@@ -149,14 +149,14 @@ SteamCommunity.prototype.chatLogoff = function() {
delete self.chatFriends;
self.chatState = SteamCommunity.ChatState.Offline;
}
});
}, "steamcommunity");
};
SteamCommunity.prototype._chatPoll = function() {
this.emit('debug', 'Doing chat poll');
var self = this;
this.request.post({
this.httpRequestPost({
"uri": "https://api.steampowered.com/ISteamWebUserPresenceOAuth/Poll/v1",
"form": {
"umqid": self._chat.umqid,
@@ -211,13 +211,13 @@ SteamCommunity.prototype._chatPoll = function() {
self.emit('debug', 'Unhandled chat message type: ' + message.type);
}
});
});
}, "steamcommunity");
};
SteamCommunity.prototype._chatUpdatePersona = function(steamID) {
this.emit('debug', 'Updating persona data for ' + steamID);
var self = this;
this.request({
this.httpRequest({
"uri": "https://steamcommunity.com/chat/friendstate/" + steamID.accountid,
"json": true
}, function(err, response, body) {
@@ -242,5 +242,5 @@ SteamCommunity.prototype._chatUpdatePersona = function(steamID) {
self.emit('chatPersonaState', steamID, persona);
self.chatFriends[steamID.getSteamID64()] = persona;
});
}, "steamcommunity");
};

View File

@@ -148,7 +148,7 @@ function request(community, url, key, time, tag, params, json, callback) {
params.m = "android";
params.tag = tag;
community.request.get({
community.httpRequestGet({
"uri": "https://steamcommunity.com/mobileconf/" + url,
"qs": params,
"json": !!json
@@ -158,14 +158,14 @@ function request(community, url, key, time, tag, params, json, callback) {
}
callback(null, body);
});
}, "steamcommunity");
}
// Confirmation checker
/**
* Start automatically polling our confirmations for new ones. The `confKeyNeeded` event will be emitted when we need a confirmation key, or `newConfirmation` when we get a new confirmation
* @param {int} pollInterval - The interval, in milliseconds, at which we will poll for confirmations. This shouldn't be any less than 10,000 probably.
* @param {int} pollInterval - The interval, in milliseconds, at which we will poll for confirmations. This should probably be at least 10,000 to avoid rate-limits.
* @param {Buffer|string|null} [identitySecret=null] - Your identity_secret. If passed, all confirmations will be automatically accepted and nothing will be emitted.
*/
SteamCommunity.prototype.startConfirmationChecker = function(pollInterval, identitySecret) {
@@ -239,6 +239,7 @@ SteamCommunity.prototype.checkConfirmations = function() {
self.getConfirmations(key.time, key.key, function(err, confirmations) {
if(err) {
self.emit('debug', "Can't check confirmations: " + err.message);
resetTimer();
return;
}
@@ -287,9 +288,14 @@ SteamCommunity.prototype.checkConfirmations = function() {
// Delay them by 1 second per new confirmation that we see, so that keys won't be the same.
setTimeout(function() {
if(self._identitySecret) {
self.emit('debug', 'Accepting confirmation ' + conf.id);
self.emit('debug', 'Accepting confirmation #' + conf.id);
var time = Math.floor(Date.now() / 1000);
conf.respond(time, SteamTotp.getConfirmationKey(self._identitySecret, time, "allow"), true, function() {
conf.respond(time, SteamTotp.getConfirmationKey(self._identitySecret, time, "allow"), true, function(err) {
if (err) {
self.emit('debug', "Can't accept confirmation #" + conf.id + ": " + err.message);
}
// We'll just retry next time we poll
delete self._knownConfirmations[conf.id];
});
} else {

View File

@@ -38,7 +38,7 @@ SteamCommunity.prototype.getGroupMembers = function(gid, callback, members, link
}
var self = this;
this.request(options, function(err, response, body) {
this.httpRequest(options, function(err, response, body) {
if (self._checkHttpError(err, response, callback)) {
return;
}
@@ -60,7 +60,7 @@ SteamCommunity.prototype.getGroupMembers = function(gid, callback, members, link
callback(null, members);
}
});
});
}, "steamcommunity");
};
SteamCommunity.prototype.getGroupMembersEx = function(gid, addresses, callback) {
@@ -73,7 +73,7 @@ SteamCommunity.prototype.joinGroup = function(gid, callback) {
}
var self = this;
this.request.post({
this.httpRequestPost({
"uri": "https://steamcommunity.com/gid/" + gid.getSteamID64(),
"form": {
"action": "join",
@@ -94,7 +94,7 @@ SteamCommunity.prototype.joinGroup = function(gid, callback) {
}
callback(null);
});
}, "steamcommunity");
};
SteamCommunity.prototype.leaveGroup = function(gid, callback) {
@@ -136,7 +136,7 @@ SteamCommunity.prototype.getAllGroupAnnouncements = function(gid, time, callback
}
var self = this;
this.request({
this.httpRequest({
"uri": "https://steamcommunity.com/gid/" + gid.getSteamID64() + "/rss/"
}, function(err, response, body) {
@@ -172,7 +172,7 @@ SteamCommunity.prototype.getAllGroupAnnouncements = function(gid, time, callback
return callback(null, announcements);
});
});
}, "steamcommunity");
}
SteamCommunity.prototype.postGroupAnnouncement = function(gid, headline, content, callback) {
@@ -181,7 +181,7 @@ SteamCommunity.prototype.postGroupAnnouncement = function(gid, headline, content
}
var self = this;
this.request.post({
this.httpRequestPost({
"uri": "https://steamcommunity.com/gid/" + gid.getSteamID64() + "/announcements",
"form": {
"sessionID": this.getSessionID(),
@@ -205,7 +205,7 @@ SteamCommunity.prototype.postGroupAnnouncement = function(gid, headline, content
}
callback(null);
})
}, "steamcommunity");
};
SteamCommunity.prototype.editGroupAnnouncement = function(gid, aid, headline, content, callback) {
@@ -229,7 +229,7 @@ SteamCommunity.prototype.editGroupAnnouncement = function(gid, aid, headline, co
}
}
this.request.post(submitData, function(err, response, body) {
this.httpRequestPost(submitData, function(err, response, body) {
if(!callback) {
return;
}
@@ -243,7 +243,7 @@ SteamCommunity.prototype.editGroupAnnouncement = function(gid, aid, headline, co
}
callback(null);
})
}, "steamcommunity");
};
SteamCommunity.prototype.deleteGroupAnnouncement = function(gid, aid, callback) {
@@ -254,10 +254,10 @@ SteamCommunity.prototype.deleteGroupAnnouncement = function(gid, aid, callback)
var self = this;
var submitData = {
"uri": "https://steamcommunity.com/gid/" + gid.getSteamID64() + "/announcements/delete/" + aid + "?sessionID=" + this.getSessionID(),
}
"uri": "https://steamcommunity.com/gid/" + gid.getSteamID64() + "/announcements/delete/" + aid + "?sessionID=" + this.getSessionID()
};
this.request.get(submitData, function(err, response, body) {
this.httpRequestGet(submitData, function(err, response, body) {
if(!callback) {
return;
}
@@ -271,7 +271,7 @@ SteamCommunity.prototype.deleteGroupAnnouncement = function(gid, aid, callback)
}
callback(null);
})
}, "steamcommunity");
};
SteamCommunity.prototype.scheduleGroupEvent = function(gid, name, type, description, time, server, callback) {
@@ -319,7 +319,7 @@ SteamCommunity.prototype.scheduleGroupEvent = function(gid, name, type, descript
}
var self = this;
this.request.post({
this.httpRequestPost({
"uri": "https://steamcommunity.com/gid/" + gid.toString() + "/eventEdit",
"form": form
}, function(err, response, body) {
@@ -337,7 +337,7 @@ SteamCommunity.prototype.scheduleGroupEvent = function(gid, name, type, descript
}
callback(null);
});
}, "steamcommunity");
};
SteamCommunity.prototype.setGroupPlayerOfTheWeek = function(gid, steamID, callback) {
@@ -350,7 +350,7 @@ SteamCommunity.prototype.setGroupPlayerOfTheWeek = function(gid, steamID, callba
}
var self = this;
this.request.post({
this.httpRequestPost({
"uri": "https://steamcommunity.com/gid/" + gid.getSteamID64() + "/potwEdit",
"form": {
"xml": 1,
@@ -380,7 +380,7 @@ SteamCommunity.prototype.setGroupPlayerOfTheWeek = function(gid, steamID, callba
callback(new Error(results.response.results[0]));
}
});
});
}, "steamcommunity");
};
SteamCommunity.prototype.kickGroupMember = function(gid, steamID, callback) {
@@ -393,7 +393,7 @@ SteamCommunity.prototype.kickGroupMember = function(gid, steamID, callback) {
}
var self = this;
this.request.post({
this.httpRequestPost({
"uri": "https://steamcommunity.com/gid/" + gid.getSteamID64() + "/membersManage",
"form": {
"sessionID": this.getSessionID(),
@@ -416,7 +416,7 @@ SteamCommunity.prototype.kickGroupMember = function(gid, steamID, callback) {
}
callback(null);
});
}, "steamcommunity");
};
SteamCommunity.prototype.getGroupHistory = function(gid, page, callback) {
@@ -430,7 +430,7 @@ SteamCommunity.prototype.getGroupHistory = function(gid, page, callback) {
}
var self = this;
this.request("https://steamcommunity.com/gid/" + gid.getSteamID64() + "/history?p=" + page, function(err, response, body) {
this.httpRequest("https://steamcommunity.com/gid/" + gid.getSteamID64() + "/history?p=" + page, function(err, response, body) {
if(self._checkHttpError(err, response, callback)) {
return;
}
@@ -499,5 +499,5 @@ SteamCommunity.prototype.getGroupHistory = function(gid, page, callback) {
});
callback(null, output);
});
}, "steamcommunity");
};

99
components/http.js Normal file
View File

@@ -0,0 +1,99 @@
var SteamCommunity = require('../index.js');
SteamCommunity.prototype.httpRequest = function(uri, options, callback, source) {
if (typeof uri === 'object') {
source = callback;
callback = options;
options = uri;
uri = options.url || options.uri;
}
options.url = options.uri = uri;
if (this._httpRequestConvenienceMethod) {
options.method = this._httpRequestConvenienceMethod;
delete this._httpRequestConvenienceMethod;
}
var requestID = ++this._httpRequestID;
source = source || "";
if (this.onPreHttpRequest && this.onPreHttpRequest(requestID, source, options, callback) === false) {
return false;
}
var self = this;
this.request(options, function(err, response, body) {
var hasCallback = !!callback;
var httpError = options.checkHttpError !== false && self._checkHttpError(err, response, callback);
var communityError = !options.json && options.checkCommunityError !== false && self._checkCommunityError(body, httpError ? function() {} : callback); // don't fire the callback if hasHttpError did it already
var tradeError = !options.json && options.checkTradeError !== false && self._checkTradeError(body, httpError || communityError ? function() {} : callback); // don't fire the callback if either of the previous already did
self.emit('postHttpRequest', requestID, source, options, httpError || communityError || tradeError || null, response, body, {
"hasCallback": hasCallback,
"httpError": httpError,
"communityError": communityError,
"tradeError": tradeError
});
if (hasCallback && !(httpError || communityError || tradeError)) {
callback.apply(self, arguments);
}
});
return true;
};
SteamCommunity.prototype.httpRequestGet = function() {
this._httpRequestConvenienceMethod = "GET";
return this.httpRequest.apply(this, arguments);
};
SteamCommunity.prototype.httpRequestPost = function() {
this._httpRequestConvenienceMethod = "POST";
return this.httpRequest.apply(this, arguments);
};
SteamCommunity.prototype._checkHttpError = function(err, response, callback) {
if(err) {
callback(err);
return err;
}
if(response.statusCode >= 300 && response.statusCode <= 399 && response.headers.location.indexOf('/login') != -1) {
err = new Error("Not Logged In");
callback(err);
return err;
}
if(response.statusCode >= 400) {
err = new Error("HTTP error " + response.statusCode);
err.code = response.statusCode;
callback(err);
return err;
}
return false;
};
SteamCommunity.prototype._checkCommunityError = function(html, callback) {
if(html.match(/<h1>Sorry!<\/h1>/)) {
var match = html.match(/<h3>(.+)<\/h3>/);
var err = new Error(match ? match[1] : "Unknown error occurred");
callback(err);
return err;
}
return false;
};
SteamCommunity.prototype._checkTradeError = function(html, callback) {
var match = html.match(/<div id="error_msg">\s*([^<]+)\s*<\/div>/);
if (match) {
var err = new Error(match[1].trim());
callback(new Error(err));
return err;
}
return false;
};

View File

@@ -13,7 +13,7 @@ SteamCommunity.prototype.getInventoryHistory = function(options, callback) {
options.page = options.page || 1;
this.request("https://steamcommunity.com/my/inventoryhistory?l=english&p=" + options.page, function(err, response, body) {
this.httpRequest("https://steamcommunity.com/my/inventoryhistory?l=english&p=" + options.page, function(err, response, body) {
if(err) {
callback(err);
return;
@@ -122,7 +122,7 @@ SteamCommunity.prototype.getInventoryHistory = function(options, callback) {
} else {
callback(null, output);
}
});
}, "steamcommunity");
};
function resolveVanityURL(vanityURL, callback) {

View File

@@ -3,7 +3,7 @@ var Cheerio = require('cheerio');
SteamCommunity.prototype.getMarketApps = function(callback) {
var self = this;
this.request('https://steamcommunity.com/market/', function (err, response, body) {
this.httpRequest('https://steamcommunity.com/market/', function (err, response, body) {
if(self._checkHttpError(err, response, callback)) {
return;
}
@@ -21,5 +21,5 @@ SteamCommunity.prototype.getMarketApps = function(callback) {
} else {
callback(new Error("Malformed response"));
}
});
}
}, "steamcommunity");
};

View File

@@ -190,15 +190,6 @@ SteamCommunity.prototype.profileSettings = function(settings, callback) {
case 'inventoryGifts':
values.inventoryGiftPrivacy = settings[i] ? 1 : 0;
break;
case 'emailConfirmation': // deprecated
case 'tradeConfirmation':
values.tradeConfirmationSetting = settings[i] ? 1 : 0;
break;
case 'marketConfirmation':
values.marketConfirmationSetting = settings[i] ? 1 : 0;
break;
}
}
@@ -229,7 +220,7 @@ SteamCommunity.prototype.uploadAvatar = function(image, format, callback) {
if(image instanceof Buffer) {
doUpload(image);
} else if(image.match(/^https?:\/\//)) {
this.request.get({
this.httpRequestGet({
"uri": image,
"encoding": null
}, function(err, response, body) {
@@ -246,7 +237,7 @@ SteamCommunity.prototype.uploadAvatar = function(image, format, callback) {
}
doUpload(body);
})
}, "steamcommunity");
} else {
if(!format) {
format = image.match(/\.([^\.]+)$/);
@@ -309,7 +300,7 @@ SteamCommunity.prototype.uploadAvatar = function(image, format, callback) {
return;
}
self.request.post({
self.httpRequestPost({
"uri": "https://steamcommunity.com/actions/FileUploader",
"formData": {
"MAX_FILE_SIZE": buffer.length,
@@ -363,6 +354,6 @@ SteamCommunity.prototype.uploadAvatar = function(image, format, callback) {
if(callback) {
callback(null, body.images.full);
}
});
}, "steamcommunity");
}
};

View File

@@ -16,7 +16,7 @@ SteamCommunity.prototype.enableTwoFactor = function(callback) {
return;
}
self.request.post({
self.httpRequestPost({
"uri": "https://api.steampowered.com/ITwoFactorService/AddAuthenticator/v1/",
"form": {
"steamid": self.steamID.getSteamID64(),
@@ -45,7 +45,7 @@ SteamCommunity.prototype.enableTwoFactor = function(callback) {
}
callback(null, body.response);
});
}, "steamcommunity");
});
};
@@ -66,7 +66,7 @@ SteamCommunity.prototype.finalizeTwoFactor = function(secret, activationCode, ca
function finalize(token) {
var code = SteamTotp.generateAuthCode(secret, diff);
self.request.post({
self.httpRequestPost({
"uri": "https://api.steampowered.com/ITwoFactorService/FinalizeAddAuthenticator/v1/",
"form": {
"steamid": self.steamID.getSteamID64(),
@@ -104,7 +104,7 @@ SteamCommunity.prototype.finalizeTwoFactor = function(secret, activationCode, ca
} else {
callback(null);
}
});
}, "steamcommunity");
}
};
@@ -117,7 +117,7 @@ SteamCommunity.prototype.disableTwoFactor = function(revocationCode, callback) {
return;
}
self.request.post({
self.httpRequestPost({
"uri": "https://api.steampowered.com/ITwoFactorService/RemoveAuthenticator/v1/",
"form": {
"steamid": self.steamID.getSteamID64(),
@@ -149,6 +149,6 @@ SteamCommunity.prototype.disableTwoFactor = function(revocationCode, callback) {
var error = new Error("Cannot remove authenticator (" + body.response.status + ")");
error.eresult = body.response.status;
callback(error);
});
}, "steamcommunity");
});
};

View File

@@ -8,7 +8,7 @@ SteamCommunity.prototype.addFriend = function(userID, callback) {
}
var self = this;
this.request.post({
this.httpRequestPost({
"uri": "https://steamcommunity.com/actions/AddFriendAjax",
"form": {
"accept_invite": 0,
@@ -30,7 +30,7 @@ SteamCommunity.prototype.addFriend = function(userID, callback) {
} else {
callback(new Error("Unknown error"));
}
});
}, "steamcommunity");
};
SteamCommunity.prototype.acceptFriendRequest = function(userID, callback) {
@@ -39,7 +39,7 @@ SteamCommunity.prototype.acceptFriendRequest = function(userID, callback) {
}
var self = this;
this.request.post({
this.httpRequestPost({
"uri": "https://steamcommunity.com/actions/AddFriendAjax",
"form": {
"accept_invite": 1,
@@ -56,7 +56,7 @@ SteamCommunity.prototype.acceptFriendRequest = function(userID, callback) {
}
callback(null);
});
}, "steamcommunity");
};
SteamCommunity.prototype.removeFriend = function(userID, callback) {
@@ -65,7 +65,7 @@ SteamCommunity.prototype.removeFriend = function(userID, callback) {
}
var self = this;
this.request.post({
this.httpRequestPost({
"uri": "https://steamcommunity.com/actions/RemoveFriendAjax",
"form": {
"sessionID": this.getSessionID(),
@@ -81,7 +81,7 @@ SteamCommunity.prototype.removeFriend = function(userID, callback) {
}
callback(null);
});
}, "steamcommunity");
};
SteamCommunity.prototype.blockCommunication = function(userID, callback) {
@@ -90,7 +90,7 @@ SteamCommunity.prototype.blockCommunication = function(userID, callback) {
}
var self = this;
this.request.post({
this.httpRequestPost({
"uri": "https://steamcommunity.com/actions/BlockUserAjax",
"form": {
"sessionID": this.getSessionID(),
@@ -106,7 +106,7 @@ SteamCommunity.prototype.blockCommunication = function(userID, callback) {
}
callback(null);
});
}, "steamcommunity");
};
SteamCommunity.prototype.unblockCommunication = function(userID, callback) {
@@ -137,7 +137,7 @@ SteamCommunity.prototype.postUserComment = function(userID, message, callback) {
}
var self = this;
this.request.post({
this.httpRequestPost({
"uri": "https://steamcommunity.com/comment/Profile/post/" + userID.toString() + "/-1",
"form": {
"comment": message,
@@ -161,7 +161,7 @@ SteamCommunity.prototype.postUserComment = function(userID, message, callback) {
} else {
callback(new Error("Unknown error"));
}
});
}, "steamcommunity");
};
SteamCommunity.prototype.inviteUserToGroup = function(userID, groupID, callback) {
@@ -170,7 +170,7 @@ SteamCommunity.prototype.inviteUserToGroup = function(userID, groupID, callback)
}
var self = this;
this.request.post({
this.httpRequestPost({
"uri": "https://steamcommunity.com/actions/GroupInvite",
"form": {
"group": groupID.toString(),
@@ -196,7 +196,7 @@ SteamCommunity.prototype.inviteUserToGroup = function(userID, groupID, callback)
} else {
callback(new Error("Unknown error"));
}
});
}, "steamcommunity");
};
SteamCommunity.prototype.getUserInventoryContexts = function(userID, callback) {
@@ -215,7 +215,7 @@ SteamCommunity.prototype.getUserInventoryContexts = function(userID, callback) {
}
var self = this;
this.request("https://steamcommunity.com/profiles/" + userID.getSteamID64() + "/inventory/", function(err, response, body) {
this.httpRequest("https://steamcommunity.com/profiles/" + userID.getSteamID64() + "/inventory/", function(err, response, body) {
if(self._checkHttpError(err, response, callback)) {
return;
}
@@ -235,7 +235,7 @@ SteamCommunity.prototype.getUserInventoryContexts = function(userID, callback) {
}
callback(null, data);
});
}, "steamcommunity");
};
SteamCommunity.prototype.getUserInventory = function(userID, appID, contextID, tradableOnly, callback) {
@@ -249,7 +249,7 @@ SteamCommunity.prototype.getUserInventory = function(userID, appID, contextID, t
get([], []);
function get(inventory, currency, start) {
self.request({
self.httpRequest({
"uri": "https://steamcommunity.com" + endpoint + "/inventory/json/" + appID + "/" + contextID,
"qs": {
"start": start,
@@ -262,7 +262,12 @@ SteamCommunity.prototype.getUserInventory = function(userID, appID, contextID, t
}
if(!body || !body.success || !body.rgInventory || !body.rgDescriptions || !body.rgCurrency) {
callback(new Error(body.Error || "Malformed response"));
if(body) {
callback(new Error(body.Error || "Malformed response"));
} else {
callback(new Error("Malformed response"));
}
return;
}
@@ -293,6 +298,6 @@ SteamCommunity.prototype.getUserInventory = function(userID, appID, contextID, t
} else {
callback(null, inventory, currency);
}
});
}, "steamcommunity");
}
};

View File

@@ -2,7 +2,7 @@ var SteamCommunity = require('../index.js');
SteamCommunity.prototype.getWebApiKey = function(domain, callback) {
var self = this;
this.request({
this.httpRequest({
"uri": "https://steamcommunity.com/dev/apikey",
"followRedirect": false
}, function(err, response, body) {
@@ -20,7 +20,7 @@ SteamCommunity.prototype.getWebApiKey = function(domain, callback) {
callback(null, match[1]);
} else {
// We need to register a new API key
self.request.post('https://steamcommunity.com/dev/registerkey', {
self.httpRequestPost('https://steamcommunity.com/dev/registerkey', {
"form": {
"domain": domain,
"agreeToTerms": "agreed",
@@ -33,9 +33,9 @@ SteamCommunity.prototype.getWebApiKey = function(domain, callback) {
}
self.getWebApiKey(domain, callback);
});
}, "steamcommunity");
}
});
}, "steamcommunity");
};
SteamCommunity.prototype.getWebApiOauthToken = function(callback) {
@@ -46,7 +46,7 @@ SteamCommunity.prototype.getWebApiOauthToken = function(callback) {
}
// Pull an oauth token from the webchat UI
this.request("https://steamcommunity.com/chat", function(err, response, body) {
this.httpRequest("https://steamcommunity.com/chat", function(err, response, body) {
if(self._checkHttpError(err, response, callback)) {
return;
}
@@ -62,5 +62,5 @@ SteamCommunity.prototype.getWebApiOauthToken = function(callback) {
}
callback(null, match[1]);
});
}, "steamcommunity");
};

145
index.js
View File

@@ -16,6 +16,7 @@ function SteamCommunity(options) {
this._jar = Request.jar();
this._captchaGid = -1;
this._httpRequestID = 0;
this.chatState = SteamCommunity.ChatState.Offline;
var defaults = {
@@ -67,56 +68,44 @@ SteamCommunity.prototype.login = function(details, callback) {
this._jar.setCookie(Request.cookie("mobileClientVersion=0 (2.1.3)"), "https://steamcommunity.com");
this._jar.setCookie(Request.cookie("mobileClient=android"), "https://steamcommunity.com");
this.request.post("https://steamcommunity.com/login/getrsakey/", {
"form": {
"username": details.accountName
},
"headers": mobileHeaders
this.httpRequestPost("https://steamcommunity.com/login/getrsakey/", {
"form": {"username": details.accountName},
"headers": mobileHeaders,
"json": true
}, function(err, response, body) {
// Remove the mobile cookies
if(err) {
if (self._checkHttpError(err, response, callback)) {
deleteMobileCookies();
callback(err);
return;
}
var json;
try {
json = JSON.parse(body);
} catch(e) {
deleteMobileCookies();
callback(e);
return;
}
if(!json.publickey_mod || !json.publickey_exp) {
if(!body.publickey_mod || !body.publickey_exp) {
deleteMobileCookies();
callback(new Error("Invalid RSA key received"));
return;
}
var key = new RSA();
key.setPublic(json.publickey_mod, json.publickey_exp);
key.setPublic(body.publickey_mod, body.publickey_exp);
var form = {
"captcha_text": details.captcha || "",
"captchagid": self._captchaGid,
"emailauth": details.authCode || "",
"emailsteamid": "",
"password": hex2b64(key.encrypt(details.password)),
"remember_login": "true",
"rsatimestamp": json.timestamp,
"twofactorcode": details.twoFactorCode || "",
"username": details.accountName,
"oauth_client_id": "DE45CD61",
"oauth_scope": "read_profile write_profile read_client write_client",
"loginfriendlyname": "#login_emailauth_friendlyname_mobile"
};
self.request.post({
self.httpRequestPost({
"uri": "https://steamcommunity.com/login/dologin/",
"json": true,
"form": form,
"form": {
"captcha_text": details.captcha || "",
"captchagid": self._captchaGid,
"emailauth": details.authCode || "",
"emailsteamid": "",
"password": hex2b64(key.encrypt(details.password)),
"remember_login": "true",
"rsatimestamp": body.timestamp,
"twofactorcode": details.twoFactorCode || "",
"username": details.accountName,
"oauth_client_id": "DE45CD61",
"oauth_scope": "read_profile write_profile read_client write_client",
"loginfriendlyname": "#login_emailauth_friendlyname_mobile",
"donotcache": Date.now()
},
"headers": mobileHeaders
}, function(err, response, body) {
deleteMobileCookies();
@@ -124,19 +113,20 @@ SteamCommunity.prototype.login = function(details, callback) {
if(self._checkHttpError(err, response, callback)) {
return;
}
var error;
if(!body.success && body.emailauth_needed) {
// Steam Guard (email)
var error = new Error("SteamGuard");
error = new Error("SteamGuard");
error.emaildomain = body.emaildomain;
callback(error);
} else if(!body.success && body.requires_twofactor) {
// Steam Guard (app)
callback(new Error("SteamGuardMobile"));
} else if(!body.success && body.captcha_needed) {
var error = new Error("CAPTCHA");
error.captchaurl = "https://steamcommunity.com/public/captcha.php?gid=" + body.captcha_gid;
} else if(!body.success && body.captcha_needed && body.message.match(/Please verify your humanity/)) {
error = new Error("CAPTCHA");
error.captchaurl = "https://steamcommunity.com/login/rendercaptcha/?gid=" + body.captcha_gid;
self._captchaGid = body.captcha_gid;
@@ -167,8 +157,8 @@ SteamCommunity.prototype.login = function(details, callback) {
callback(null, sessionID, cookies, steamguard, oAuth.oauth_token);
}
});
});
}, "steamcommunity");
}, "steamcommunity");
function deleteMobileCookies() {
var cookie = Request.cookie('mobileClientVersion=');
@@ -186,7 +176,7 @@ SteamCommunity.prototype.oAuthLogin = function(steamguard, token, callback) {
var steamID = new SteamID(steamguard[0]);
var self = this;
this.request.post({
this.httpRequestPost({
"uri": "https://api.steampowered.com/IMobileAuthService/GetWGToken/v1/",
"form": {
"access_token": token
@@ -211,7 +201,7 @@ SteamCommunity.prototype.oAuthLogin = function(steamguard, token, callback) {
self.setCookies(cookies);
callback(null, self.getSessionID(), cookies);
});
}, "steamcommunity");
};
SteamCommunity.prototype.setCookies = function(cookies) {
@@ -245,7 +235,9 @@ function generateSessionID() {
}
SteamCommunity.prototype.parentalUnlock = function(pin, callback) {
this.request.post("https://steamcommunity.com/parental/ajaxunlock", {
var self = this;
this.httpRequestPost("https://steamcommunity.com/parental/ajaxunlock", {
"json": true,
"form": {
"pin": pin
@@ -268,12 +260,12 @@ SteamCommunity.prototype.parentalUnlock = function(pin, callback) {
}
callback();
}.bind(this));
}.bind(this), "steamcommunity");
};
SteamCommunity.prototype.getNotifications = function(callback) {
var self = this;
this.request.get("https://steamcommunity.com/actions/RefreshNotificationArea", function(err, response, body) {
this.httpRequestGet("https://steamcommunity.com/actions/RefreshNotificationArea", function(err, response, body) {
if(self._checkHttpError(err, response, callback)) {
return;
}
@@ -304,12 +296,12 @@ SteamCommunity.prototype.getNotifications = function(callback) {
}
callback(null, notifications);
});
}, "steamcommunity");
};
SteamCommunity.prototype.resetItemNotifications = function(callback) {
var self = this;
this.request.get("https://steamcommunity.com/my/inventory", function(err, response, body) {
this.httpRequestGet("https://steamcommunity.com/my/inventory", function(err, response, body) {
if(!callback) {
return;
}
@@ -319,11 +311,11 @@ SteamCommunity.prototype.resetItemNotifications = function(callback) {
}
callback(null);
});
}, "steamcommunity");
};
SteamCommunity.prototype.loggedIn = function(callback) {
this.request("https://steamcommunity.com/my", {"followRedirect": false}, function(err, response, body) {
this.httpRequest("https://steamcommunity.com/my", {"followRedirect": false}, function(err, response, body) {
if(err || (response.statusCode != 302 && response.statusCode != 403)) {
callback(err || new Error("HTTP error " + response.statusCode));
return;
@@ -335,22 +327,12 @@ SteamCommunity.prototype.loggedIn = function(callback) {
}
callback(null, !!response.headers.location.match(/steamcommunity\.com(\/(id|profiles)\/[^\/]+)\/?/), false);
});
};
SteamCommunity.prototype._checkCommunityError = function(html, callback) {
if(html.match(/<h1>Sorry!<\/h1>/)) {
var match = html.match(/<h3>(.+)<\/h3>/);
callback(new Error(match ? match[1] : "Unknown error occurred"));
return true;
}
return false;
}, "steamcommunity");
};
SteamCommunity.prototype._myProfile = function(endpoint, form, callback) {
var self = this;
this.request("https://steamcommunity.com/my", {"followRedirect": false}, function(err, response, body) {
this.httpRequest("https://steamcommunity.com/my", {"followRedirect": false}, function(err, response, body) {
if(err || response.statusCode != 302) {
callback(err || "HTTP error " + response.statusCode);
return;
@@ -361,32 +343,23 @@ SteamCommunity.prototype._myProfile = function(endpoint, form, callback) {
callback(new Error("Can't get profile URL"));
return;
}
(form ? self.request.post : self.request)("https://steamcommunity.com" + match[1] + "/" + endpoint, form ? {"form": form} : {}, callback);
});
var options = {
"uri": "https://steamcommunity.com" + mtch[1] + "/" + endpoint,
"method": "GET"
};
if (form) {
options.method = "POST";
options.form = form;
}
self.httpRequest(options, callback, "steamcommunity");
}, "steamcommunity");
};
SteamCommunity.prototype._checkHttpError = function(err, response, callback) {
if(err) {
callback(err);
return true;
}
if(response.statusCode >= 300 && response.statusCode <= 399 && response.headers.location.indexOf('/login') != -1) {
callback(new Error("Not Logged In"));
return true;
}
if(response.statusCode >= 400) {
var error = new Error("HTTP error " + response.statusCode);
error.code = response.statusCode;
callback(error);
return true;
}
return false;
};
require('./components/http.js');
require('./components/chat.js');
require('./components/profile.js');
require('./components/market.js');

View File

@@ -1,6 +1,6 @@
{
"name": "steamcommunity",
"version": "3.18.3",
"version": "3.19.0-beta2",
"description": "Provides an interface for logging into and interacting with the Steam Community website",
"keywords": ["steam", "steam community"],
"homepage": "https://github.com/DoctorMcKay/node-steamcommunity",
@@ -20,5 +20,8 @@
"cheerio": "^0.19.0",
"async": "^1.4.2",
"steam-totp": "^1.3.0"
},
"engines": {
"node": ">=4.0.0"
}
}
}