Compare commits

...

40 Commits

Author SHA1 Message Date
Alexander Corn
4f618c0a27 2.4.0 2015-06-29 16:38:36 -04:00
Alexander Corn
19fe951d96 Added ability to spoof UI mode 2015-06-29 16:38:24 -04:00
Alexander Corn
a348112ccb 2.3.0 2015-06-28 22:18:48 -04:00
Alexander Corn
dff2727f85 Don't fire callback if not provided 2015-06-28 22:16:49 -04:00
Alexander Corn
1fd12dd4d0 Added CMarketItem 2015-06-28 20:19:42 -04:00
Alexander Corn
31093da1a8 Use better dependency semver 2015-06-28 19:40:00 -04:00
Alexander Corn
1e609524e8 2.2.1 2015-06-24 13:31:17 -04:00
Alexander Corn
2a758fc180 Remember Steam login so that cookies don't expire if logged in elsewhere 2015-06-24 13:31:01 -04:00
Alexander Corn
eb2081c325 2.2.0 2015-06-18 00:57:23 -04:00
Alexander Corn
c60d4942e0 Removed useless timestamp parameter from chatMessage event 2015-06-18 00:41:55 -04:00
Alexander Corn
1aa42b89f7 Added public static CSteamUser.getAvatarURL method 2015-06-18 00:41:26 -04:00
Alexander Corn
e01d05c140 Added PersonaStateFlag enum 2015-06-18 00:36:26 -04:00
Alexander Corn
8ec0ef84c2 Added PersonaState enum 2015-06-18 00:33:34 -04:00
Alexander Corn
0e5929df24 Lowered chat poll interval to 500ms 2015-06-18 00:24:54 -04:00
Alexander Corn
74bf2dcfca Added webchat 2015-06-18 00:22:05 -04:00
Alexander Corn
e8634e17ca 2.1.0 2015-06-09 14:45:34 -04:00
Alexander Corn
2a2947344f Export request 2015-06-09 14:45:07 -04:00
Alexander Corn
e789d07833 2.0.0 2015-05-30 23:53:59 -04:00
Alexander Corn
88059596b3 Use a string for the steamguard value instead of an object 2015-05-30 23:45:46 -04:00
Alexander Corn
399cc5ee84 1.0.8 2015-05-29 01:03:22 -04:00
Alexander Corn
a24bb9d4ad Fixed API key registration not working 2015-05-29 01:03:07 -04:00
Alexander Corn
81eb26b30d 1.0.7 2015-05-25 13:43:38 -04:00
Alexander Corn
0c95e75e39 Added getNotifications method 2015-05-25 13:37:01 -04:00
Alexander Corn
2eaa5b057d Added resetItemNotifications method 2015-05-25 13:20:47 -04:00
Alexander Corn
d5592246cf 1.0.6 2015-05-16 00:44:42 -04:00
Alexander Corn
22ab86434a Fixed repository URL 2015-05-16 00:44:33 -04:00
Alexander Corn
d367ceb2a6 1.0.5 2015-05-11 15:56:57 -04:00
Alexander Corn
5ab5a6d401 Made callback optional for parentalUnlock 2015-05-11 15:56:41 -04:00
Alexander Corn
a88a39c62c 1.0.4 2015-05-11 15:48:13 -04:00
Alexander Corn
5f4f4a03a4 Added parentalUnlock method 2015-05-11 15:48:00 -04:00
Alexander Corn
079db4b7bf 1.0.3 2015-05-11 01:09:58 -04:00
Alexander Corn
22ba30f5da Added getWebApiKey method 2015-05-11 01:07:14 -04:00
Alexander Corn
8ca763d9b9 1.0.2 2015-04-26 01:33:29 -04:00
Alexander Corn
eb88ace3d7 Added removeFriend, blockCommunication, and unblockCommunication 2015-04-26 01:16:27 -04:00
Alexander Corn
4138d1c14a Added the beginning of CSteamUser 2015-04-25 14:46:25 -04:00
Alexander Corn
4c0cd4dc8b Removed rogue console.log 2015-04-25 14:07:45 -04:00
Alexander Corn
8bad7dd09f Fixed empty string error when Steam Guard is needed 2015-04-25 14:06:46 -04:00
Alexander Corn
3402acb33a 1.0.1 2015-01-30 02:46:24 -05:00
Alexander Corn
00426a6aea Updated description 2015-01-30 02:46:03 -05:00
Alexander Corn
116c57e6ff Fixed readme 2014-12-17 04:12:26 -05:00
7 changed files with 765 additions and 36 deletions

View File

@@ -1,4 +1,4 @@
simply # Steam Community for Node.js
# Steam Community for Node.js
This lightweight module provides an easy interface for the Steam Community website. This module can be used to simply login to steamcommunity.com for use with other libraries, or to interact with steamcommunity.com.

131
classes/CMarketItem.js Normal file
View File

@@ -0,0 +1,131 @@
var SteamCommunity = require('../index.js');
var Cheerio = require('cheerio');
SteamCommunity.prototype.getMarketItem = function(appid, hashName, callback) {
var self = this;
this.request("https://steamcommunity.com/market/listings/" + appid + "/" + encodeURIComponent(hashName), function(err, response, body) {
if(err || response.statusCode != 200) {
callback(err ? err.message : "HTTP error " + response.statusCode);
return;
}
var $ = Cheerio.load(body);
if($('.market_listing_table_message') && $('.market_listing_table_message').text().trim() == 'There are no listings for this item.') {
callback('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);
}
});
};
function CMarketItem(community, body, $) {
this._community = community;
this._$ = $;
this._country = "US";
var match = body.match(/var g_strCountryCode = "([^"]+)";/);
if(match) {
this._country = match[1];
}
this.commodity = false;
var match = body.match(/Market_LoadOrderSpread\(\s*(\d+)\s*\);/);
if(match) {
this.commodity = true;
this.commodityID = parseInt(match[1], 10);
}
this.medianSalePrices = null;
match = body.match(/var line1=([^;]+);/);
if(match) {
try {
this.medianSalePrices = JSON.parse(match[1]);
this.medianSalePrices = this.medianSalePrices.map(function(item) {
return {
"hour": new Date(item[0]),
"price": item[1],
"quantity": parseInt(item[2], 10)
};
});
} catch(e) {
// 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);
}
}
// TODO: Buying listings and placing buy orders
}
CMarketItem.prototype.updatePrice = function(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,
}, function(err, response, body) {
if(err || response.statusCode != 200) {
if(callback) {
callback(err ? err.message : "HTTP error " + response.statusCode);
}
return;
}
if(body.success != 1) {
if(callback) {
callback("Error " + body.success);
}
return;
}
var match = (body.sell_order_summary || '').match(/<span class="market_commodity_orders_header_promote">(\d+)<\/span>/);
if(match) {
self.quantity = parseInt(match[1], 10);
}
self.buyQuantity = 0;
match = (body.buy_order_summary || '').match(/<span class="market_commodity_orders_header_promote">(\d+)<\/span>/);
if(match) {
self.buyQuantity = parseInt(match[1], 10);
}
self.lowestPrice = parseInt(body.lowest_sell_order, 10);
self.highestBuyOrder = parseInt(body.highest_buy_order, 10);
// TODO: The tables?
if(callback) {
callback();
}
});
};

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.request("https://steamcommunity.com/" + (typeof id === 'string' ? "groups/" + id : "gid/" + id.toString()) + "/memberslistxml/?xml=1", function(err, response, body) {
if(err || response.statusCode != 200) {
callback(err || "HTTP error " + response.statusCode);
return;
@@ -65,7 +65,7 @@ CSteamGroup.prototype.getMembers = function(callback, members, link) {
link = link || "http://steamcommunity.com/gid/" + this.steamID.toString() + "/memberslistxml/?xml=1";
var self = this;
this._community._request(link, function(err, response, body) {
this._community.request(link, function(err, response, body) {
if(err || response.statusCode != 200) {
callback(err || "HTTP error " + response.statusCode);
return;
@@ -97,7 +97,7 @@ CSteamGroup.prototype.join = function(callback) {
};
var self = this;
this._community._request.post("https://steamcommunity.com/gid/" + this.steamID.toString(), {"form": form}, function(err, response, body) {
this._community.request.post("https://steamcommunity.com/gid/" + this.steamID.toString(), {"form": form}, function(err, response, body) {
if(!callback) {
return;
}
@@ -149,7 +149,7 @@ CSteamGroup.prototype.postAnnouncement = function(headline, content, callback) {
"body": content
};
this._community._request.post("https://steamcommunity.com/gid/" + this.steamID.toString() + "/announcements", {"form": form}, function(err, response, body) {
this._community.request.post("https://steamcommunity.com/gid/" + this.steamID.toString() + "/announcements", {"form": form}, function(err, response, body) {
if(!callback) {
return;
}
@@ -208,7 +208,7 @@ CSteamGroup.prototype.scheduleEvent = function(name, type, description, time, se
}
var self = this;
this._community._request.post("https://steamcommunity.com/gid/" + this.steamID.toString() + "/eventEdit", {"form": form}, function(err, response, body) {
this._community.request.post("https://steamcommunity.com/gid/" + this.steamID.toString() + "/eventEdit", {"form": form}, function(err, response, body) {
if(!callback) {
return;
}
@@ -235,7 +235,7 @@ CSteamGroup.prototype.setPlayerOfTheWeek = function(steamID, callback) {
};
var self = this;
this._community._request.post("https://steamcommunity.com/gid/" + this.steamID.toString() + "/potwEdit", {"form": form}, function(err, response, body) {
this._community.request.post("https://steamcommunity.com/gid/" + this.steamID.toString() + "/potwEdit", {"form": form}, function(err, response, body) {
if(!callback) {
return;
}
@@ -269,7 +269,7 @@ CSteamGroup.prototype.kick = function(steamID, callback) {
};
var self = this;
this._community._request.post("https://steamcommunity.com/gid/" + this.steamID.toString() + "/membersManage", {"form": form}, function(err, response, body) {
this._community.request.post("https://steamcommunity.com/gid/" + this.steamID.toString() + "/membersManage", {"form": form}, function(err, response, body) {
if(!callback) {
return;
}

231
classes/CSteamUser.js Normal file
View File

@@ -0,0 +1,231 @@
var SteamCommunity = require('../index.js');
var SteamID = require('steamid');
var xml2js = require('xml2js');
SteamCommunity.prototype.getSteamUser = function(id, callback) {
if(typeof id !== 'string' && !(typeof id === 'object' && id.__proto__ === SteamID.prototype)) {
throw new Error("id parameter should be a user URL string or a SteamID object");
}
if(typeof id === 'object' && (id.universe != SteamID.Universe.PUBLIC || id.type != SteamID.Type.INDIVIDUAL)) {
throw new Error("SteamID must stand for an individual account in the public universe");
}
var self = this;
this.request("http://steamcommunity.com/" + (typeof id === 'string' ? "id/" + id : "profiles/" + id.toString()) + "/?xml=1", function(err, response, body) {
if(err || response.statusCode != 200) {
callback(err || "HTTP error " + response.statusCode);
return;
}
if(self._checkCommunityError(body, callback)) {
return;
}
xml2js.parseString(body, function(err, result) {
if(err || (!result.response && !result.profile)) {
callback(err || "No valid response");
return;
}
if(result.response && result.response.error && result.response.error.length) {
callback(result.response.error[0]);
return;
}
// Try and find custom URL from redirect
var customurl = null;
if(response.request.redirects && response.request.redirects.length) {
var match = response.request.redirects[0].redirectUri.match(/https?:\/\/steamcommunity\.com\/id\/([^/])+\/\?xml=1/);
if(match) {
customurl = match[1];
}
}
callback(null, new CSteamUser(self, result.profile, customurl));
});
});
};
function CSteamUser(community, userData, customurl) {
this._community = community;
this.steamID = new SteamID(userData.steamID64[0]);
this.name = userData.steamID[0];
this.onlineState = userData.onlineState[0];
this.stateMessage = userData.stateMessage[0];
this.privacyState = userData.privacyState[0];
this.visibilityState = userData.visibilityState[0];
this.avatarHash = userData.avatarIcon[0].match(/([0-9a-f]+)\.[a-z]+$/)[1];
this.vacBanned = !!userData.vacBanned[0];
this.tradeBanState = userData.tradeBanState[0];
this.isLimitedAccount = !!userData.isLimitedAccount[0];
this.customURL = userData.customURL ? userData.customURL[0] : customurl;
if(this.visibilityState == 3) {
this.memberSince = new Date(userData.memberSince[0].replace(/(\d{1,2})(st|nd|th)/, "$1"));
this.location = userData.location[0] || null;
this.realName = userData.realname[0] || null;
this.summary = userData.summary[0] || null;
} else {
this.memberSince = null;
this.location = null;
this.realName = null;
this.summary = null;
}
// Maybe handle mostPlayedGames?
this.groups = null;
this.primaryGroup = null;
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) {
self.primaryGroup = new SteamID(group.groupID64[0]);
}
return new SteamID(group.groupID64[0]);
});
}
}
CSteamUser.getAvatarURL = function(hash, size, protocol) {
size = size || '';
protocol = protocol || 'http://';
var url = protocol + "steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/" + hash.substring(0, 2) + "/" + hash;
if(size == 'full' || size == 'medium') {
return url + "_" + size + ".jpg";
} else {
return url + ".jpg";
}
};
CSteamUser.prototype.getAvatarURL = function(size, protocol) {
return CSteamUser.getAvatarURL(this.avatarHash, size, protocol);
};
CSteamUser.prototype.addFriend = function(callback) {
this._community.request.post('https://steamcommunity.com/actions/AddFriendAjax', {"form": {"accept_invite": 0, "sessionID": this._community.getSessionID(), "steamid": this.steamID.toString()}}, function(err, response, body) {
if(!callback) {
return;
}
if(err || response.statusCode != 200) {
callback(err || "HTTP error " + response.statusCode);
return;
}
var json;
try {
json = JSON.parse(body);
} catch(e) {
callback(e);
return;
}
if(json.success) {
callback();
} else {
callback("Unknown error");
}
});
};
CSteamUser.prototype.acceptFriendRequest = function(callback) {
this._community.request.post('https://steamcommunity.com/actions/AddFriendAjax', {"form": {"accept_invite": 1, "sessionID": this._community.getSessionID(), "steamid": this.steamID.toString()}}, function(err, response, body) {
if(!callback) {
return;
}
if(err || response.statusCode != 200) {
callback(err || "HTTP error " + response.statusCode);
return;
}
callback();
});
};
CSteamUser.prototype.removeFriend = function(callback) {
this._community.request.post('https://steamcommunity.com/actions/RemoveFriendAjax', {"form": {"sessionID": this._community.getSessionID(), "steamid": this.steamID.toString()}}, function(err, response, body) {
if(!callback) {
return;
}
if(err || response.statusCode != 200) {
callback(err || "HTTP error " + response.statusCode);
return;
}
callback();
});
};
CSteamUser.prototype.blockCommunication = function(callback) {
this._community.request.post('https://steamcommunity.com/actions/BlockUserAjax', {"form": {"sessionID": this._community.getSessionID(), "steamid": this.steamID.toString()}}, function(err, response, body) {
if(!callback) {
return;
}
if(err || response.statusCode != 200) {
callback(err || "HTTP error " + response.statusCode);
return;
}
callback();
});
};
CSteamUser.prototype.unblockCommunication = function(callback) {
var form = {"action": "unignore"};
form['friends[' + this.steamID.toString() + ']'] = 1;
this._community._myProfile('friends/blocked/', form, function(err, response, body) {
if(!callback) {
return;
}
if(err || response.statusCode >= 400) {
callback(err || "HTTP error " + response.statusCode);
return;
}
callback();
});
};
CSteamUser.prototype.comment = function(message, callback) {
this._community.request.post('https://steamcommunity.com/comment/Profile/post/' + this.steamID.toString() + '/-1/', {"form": {
"comment": message,
"count": 6,
"sessionid": this._community.getSessionID()
}}, function(err, response, body) {
if(!callback) {
return;
}
if(err || response.statusCode != 200) {
callback(err || "HTTP error " + response.statusCode);
return;
}
var json;
try {
json = JSON.parse(body);
} catch(e) {
callback(e);
return;
}
if(json.success) {
callback();
} else if(json.error) {
callback(json.error);
} else {
callback("Unknown error");
}
});
};

251
components/chat.js Normal file
View File

@@ -0,0 +1,251 @@
var SteamCommunity = require('../index.js');
var SteamID = require('steamid');
SteamCommunity.ChatState = {
"Offline": 0,
"LoggingOn": 1,
"LogOnFailed": 2,
"LoggedOn": 3
};
SteamCommunity.PersonaState = {
"Offline": 0,
"Online": 1,
"Busy": 2,
"Away": 3,
"Snooze": 4,
"LookingToTrade": 5,
"LookingToPlay": 6,
"Max": 7
};
SteamCommunity.PersonaStateFlag = {
"HasRichPresence": 1,
"InJoinableGame": 2,
"OnlineUsingWeb": 256,
"OnlineUsingMobile": 512,
"OnlineUsingBigPicture": 1024
};
SteamCommunity.prototype.chatLogon = function(interval, uiMode) {
if(this.chatState == SteamCommunity.ChatState.LoggingOn || this.chatState == SteamCommunity.ChatState.LoggedOn) {
return;
}
interval = interval || 500;
uiMode = uiMode || "web";
this.emit('debug', 'Requesting chat WebAPI token');
this.chatState = SteamCommunity.ChatState.LoggingOn;
var self = this;
this.request("https://steamcommunity.com/chat", function(err, response, body) {
if(err || response.statusCode != 200) {
self.emit('debug', 'Error requesting chat WebAPI token: ' + (err ? err.message : "HTTP error " + response.statusCode));
self.chatState = SteamCommunity.ChatState.LogOnFailed;
setTimeout(self.chatLogon.bind(self), 5000);
return;
}
var match = body.match(/[0-9a-f]{32}/);
if(!match) {
self.emit('debug', 'Couldn\'t find a WebAPI chat token in the response.');
self.chatState = SteamCommunity.ChatState.LogOnFailed;
setTimeout(self.chatLogon.bind(self), 5000);
return;
}
self.request.post({
"uri": "https://api.steampowered.com/ISteamWebUserPresenceOAuth/Logon/v1",
"form": {
"ui_mode": uiMode,
"access_token": match[0]
},
"json": true
}, function(err, response, body) {
if(err || response.statusCode != 200) {
self.emit('debug', 'Error logging into webchat: ' + (err ? err.message : "HTTP error " + response.statusCode));
self.chatState = SteamCommunity.ChatState.LogOnFailed;
setTimeout(self.chatLogon.bind(self), 5000);
return;
}
if(body.error != 'OK') {
self.emit('debug', 'Error logging into webchat: ' + body.error);
self.chatState = SteamCommunity.ChatState.LogOnFailed;
setTimeout(self.chatLogon.bind(self), 5000);
return;
}
self._chat = {
"umqid": body.umqid,
"message": body.message,
"accessToken": match[0],
"interval": interval
};
self.chatFriends = {};
self.chatState = SteamCommunity.ChatState.LoggedOn;
self.emit('chatLoggedOn');
self._chatPoll();
});
});
};
SteamCommunity.prototype.chatMessage = function(recipient, text, type, callback) {
if(this.chatState != SteamCommunity.ChatState.LoggedOn) {
throw new Error("Chat must be logged on before messages can be sent");
}
if(typeof recipient === 'string') {
recipient = new SteamID(recipient);
}
if(typeof type === 'function') {
callback = type;
type = 'saytext';
}
type = type || 'saytext';
this.request.post({
"uri": "https://api.steampowered.com/ISteamWebUserPresenceOAuth/Message/v1",
"form": {
"access_token": this._chat.accessToken,
"steamid_dst": recipient.toString(),
"text": text,
"type": type,
"umqid": this._chat.umqid
},
"json": true
}, function(err, response, body) {
if(!callback) {
return;
}
if(err || response.statusCode != 200) {
callback(err ? err.message : "HTTP error " + response.statusCode);
} else if(body.error != 'OK') {
callback(body.error);
} else {
callback();
}
});
};
SteamCommunity.prototype.chatLogoff = function() {
var self = this;
this.request.post({
"uri": "https://api.steampowered.com/ISteamWebUserPresenceOAuth/Logoff/v1",
"form": {
"access_token": this._chat.accessToken,
"umqid": this._chat.umqid
}
}, function(err, response, body) {
if(err || response.statusCode != 200) {
self.emit('debug', 'Error logging off of chat: ' + (err ? err.message : "HTTP error " + response.statusCode));
setTimeout(self.chatLogoff.bind(self), 1000);
} else {
self.emit('chatLoggedOff');
clearTimeout(self._chat.timer);
delete self._chat;
delete self.chatFriends;
self.chatState = SteamCommunity.ChatState.Offline;
}
});
};
SteamCommunity.prototype._chatPoll = function() {
this.emit('debug', 'Doing chat poll');
var self = this;
this.request.post({
"uri": "https://api.steampowered.com/ISteamWebUserPresenceOAuth/Poll/v1",
"form": {
"umqid": self._chat.umqid,
"message": self._chat.message,
"pollid": 1,
"sectimeout": 20,
"secidletime": 0,
"use_accountids": 1,
"access_token": self._chat.accessToken
},
"json": true
}, function(err, response, body) {
if(self.chatState == SteamCommunity.ChatState.Offline) {
return;
}
self._chat.timer = setTimeout(self._chatPoll.bind(self), self._chat.interval);
if(err || response.statusCode != 200) {
self.emit('debug', 'Error in chat poll: ' + (err ? err.message : "HTTP error " + response.statusCode));
return;
}
if(body.error != 'OK') {
self.emit('debug', 'Error in chat poll: ' + body.error);
return;
}
self._chat.message = body.messagelast;
(body.messages || []).forEach(function(message) {
var sender = new SteamID();
sender.universe = SteamID.Universe.PUBLIC;
sender.type = SteamID.Type.INDIVIDUAL;
sender.instance = SteamID.Instance.DESKTOP;
sender.accountid = message.accountid_from;
switch(message.type) {
case 'personastate':
self._chatUpdatePersona(sender);
break;
case 'saytext':
self.emit('chatMessage', sender, message.text);
break;
case 'typing':
self.emit('chatTyping', sender);
break;
default:
self.emit('debug', 'Unhandled chat message type: ' + message.type);
}
});
});
};
SteamCommunity.prototype._chatUpdatePersona = function(steamID) {
this.emit('debug', 'Updating persona data for ' + steamID);
var self = this;
this.request({
"uri": "https://steamcommunity.com/chat/friendstate/" + steamID.accountid,
"json": true
}, function(err, response, body) {
if(err || response.statusCode != 200) {
self.emit('debug', 'Chat update persona error: ' + (err ? err.message : "HTTP error " + response.statusCode));
setTimeout(function() {
self._chatUpdatePersona(steamID);
}, 2000);
return;
}
var persona = {
"steamID": steamID,
"personaName": body.m_strName,
"personaState": body.m_ePersonaState,
"personaStateFlags": body.m_nPersonaStateFlags || 0,
"avatarHash": body.m_strAvatarHash,
"inGame": !!body.m_bInGame,
"inGameAppID": body.m_nInGameAppID ? parseInt(body.m_nInGameAppID, 10) : null,
"inGameName": body.m_strInGameName || null
};
self.emit('chatPersonaState', steamID, persona);
self.chatFriends[steamID.getSteamID64()] = persona;
});
};

157
index.js
View File

@@ -3,22 +3,26 @@ var RSA = require('node-bignumber').Key;
var hex2b64 = require('node-bignumber').hex2b64;
var SteamID = require('steamid');
require('util').inherits(SteamCommunity, require('events').EventEmitter);
module.exports = SteamCommunity;
SteamCommunity.SteamID = SteamID;
function SteamCommunity() {
this._jar = Request.jar();
this._request = Request.defaults({"jar": this._jar});
this.request = Request.defaults({"jar": this._jar});
this.chatState = SteamCommunity.ChatState.Offline;
}
SteamCommunity.prototype.login = function(details, callback) {
if(details.steamID && details.sentry) {
this._jar.setCookie(Request.cookie('steamMachineAuth' + details.steamID.getSteamID64() + '=' + encodeURIComponent(details.sentry)), 'https://steamcommunity.com');
if(details.steamguard) {
var parts = details.steamguard.split('||');
this._jar.setCookie(Request.cookie('steamMachineAuth' + parts[0] + '=' + encodeURIComponent(parts[1])), 'https://steamcommunity.com');
}
var self = this;
this._request.post("https://steamcommunity.com/login/getrsakey/", {"form": {"username": details.accountName}}, function(err, response, body) {
this.request.post("https://steamcommunity.com/login/getrsakey/", {"form": {"username": details.accountName}}, function(err, response, body) {
if(err) {
callback(err);
return;
@@ -42,34 +46,31 @@ SteamCommunity.prototype.login = function(details, callback) {
"emailsteamid": "",
"loginfriendlyname": "",
"password": hex2b64(key.encrypt(details.password)),
"remember_login": false,
"remember_login": "true",
"rsatimestamp": json.timestamp,
"twofactorcode": "",
"username": details.accountName
};
console.log(self._jar.getCookieString("https://steamcommunity.com"));
self._request.post("https://steamcommunity.com/login/dologin/", {"form": form}, function(err, response, body) {
self.request.post({
"uri": "https://steamcommunity.com/login/dologin/",
"json": true,
"form": form
}, function(err, response, body) {
if(err) {
callback(err);
return;
}
var json;
try {
json = JSON.parse(body);
} catch(e) {
callback(e);
return;
}
if(!json.success) {
callback(json.message);
if(!body.success && body.emailauth_needed) {
callback("Please provide the authorization code sent to your address at " + body.emaildomain);
} else if(!body.success) {
callback(body.message || "Unknown error");
} else {
var sessionID = generateSessionID();
self._jar.setCookie(Request.cookie('sessionid=' + sessionID), 'http://steamcommunity.com');
self.steamID = new SteamID(json.transfer_parameters.steamid);
self.steamID = new SteamID(body.transfer_parameters.steamid);
var cookies = self._jar.getCookieString("https://steamcommunity.com").split(';').map(function(cookie) {
return cookie.trim();
});
@@ -79,7 +80,7 @@ SteamCommunity.prototype.login = function(details, callback) {
for(var i = 0; i < cookies.length; i++) {
var parts = cookies[i].split('=');
if(parts[0] == 'steamMachineAuth' + self.steamID) {
steamguard = {"steamID": self.steamID, "sentry": decodeURIComponent(parts[1])};
steamguard = self.steamID.toString() + '||' + decodeURIComponent(parts[1]);
break;
}
}
@@ -120,6 +121,117 @@ function generateSessionID() {
return Math.floor(Math.random() * 1000000000);
};
SteamCommunity.prototype.getWebApiKey = function(domain, callback) {
var self = this;
this.request("https://steamcommunity.com/dev/apikey", function(err, response, body) {
if(err || response.statusCode != 200) {
return callback(err.message || "HTTP error " + response.statusCode);
}
if(body.match(/<h2>Access Denied<\/h2>/)) {
return callback("Access Denied");
}
var match = body.match(/<p>Key: ([0-9A-F]+)<\/p>/);
if(match) {
// We already have an API key registered
callback(null, match[1]);
} else {
// We need to register a new API key
self.request.post('https://steamcommunity.com/dev/registerkey', {
"form": {
"domain": domain,
"agreeToTerms": "agreed",
"sessionid": self.getSessionID(),
"Submit": "Register"
}
}, function(err, response, body) {
if(err || response.statusCode >= 400) {
return callback(err.message || "HTTP error " + response.statusCode);
}
self.getWebApiKey(domain, callback);
});
}
});
};
SteamCommunity.prototype.parentalUnlock = function(pin, callback) {
this.request.post("https://steamcommunity.com/parental/ajaxunlock", {
"json": true,
"form": {
"pin": pin
}
}, function(err, response, body) {
if(!callback) {
return;
}
if(err || response.statusCode != 200) {
return callback(err.message || "HTTP error " + response.statusCode);
}
if(!body || typeof body.success !== 'boolean') {
return callback("Invalid response");
}
if(!body.success) {
return callback("Incorrect PIN");
}
callback();
}.bind(this));
};
SteamCommunity.prototype.getNotifications = function(callback) {
this.request.get("https://steamcommunity.com/actions/RefreshNotificationArea", function(err, response, body) {
if(err || response.statusCode != 200) {
return callback(err.message || "HTTP error " + response.statusCode);
}
var notifications = {
"comments": 0,
"items": 0,
"invites": 0,
"gifts": 0,
"chat": 0,
"trades": 0
};
var items = {
"comments": /(\d+) new comments?/,
"items": /(\d+) new items? in your inventory/,
"invites": /(\d+) new invites?/,
"gifts": /(\d+) new gifts?/,
"chat": /(\d+) unread chat messages?/,
"trades": /(\d+) new trade notifications?/
};
var match;
for(var i in items) {
if(match = body.match(items[i])) {
notifications[i] = parseInt(match[1], 10);
}
}
callback(null, notifications);
});
};
SteamCommunity.prototype.resetItemNotifications = function(callback) {
this.request.get("https://steamcommunity.com/my/inventory", function(err, response, body) {
if(!callback) {
return;
}
if(err || response.statusCode != 200) {
callback(err.message || "HTTP error " + response.statusCode);
} else {
callback();
}
});
};
SteamCommunity.prototype._checkCommunityError = function(html, callback) {
if(html.match(/<h1>Sorry!<\/h1>/)) {
var match = html.match(/<h3>(.+)<\/h3>/);
@@ -132,7 +244,7 @@ SteamCommunity.prototype._checkCommunityError = function(html, callback) {
SteamCommunity.prototype._myProfile = function(endpoint, form, callback) {
var self = this;
this._request("https://steamcommunity.com/my", {"followRedirect": false}, function(err, response, body) {
this.request("https://steamcommunity.com/my", {"followRedirect": false}, function(err, response, body) {
if(err || response.statusCode != 302) {
callback(err || "HTTP error " + response.statusCode);
return;
@@ -144,8 +256,11 @@ SteamCommunity.prototype._myProfile = function(endpoint, form, callback) {
return;
}
(form ? self._request.post : self._request)("https://steamcommunity.com" + match[1] + "/" + endpoint, form ? {"form": form} : {}, callback);
(form ? self.request.post : self.request)("https://steamcommunity.com" + match[1] + "/" + endpoint, form ? {"form": form} : {}, callback);
});
};
require('./classes/CMarketItem.js');
require('./classes/CSteamGroup.js');
require('./classes/CSteamUser.js');
require('./components/chat.js');

View File

@@ -1,7 +1,7 @@
{
"name": "steamcommunity",
"version": "1.0.0",
"description": "Facilitates logins to the Steam Communtiy website",
"version": "2.4.0",
"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",
"bugs": {
@@ -10,12 +10,13 @@
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/Doctor_McKay/node-steamcommunity.git"
"url": "https://github.com/DoctorMcKay/node-steamcommunity.git"
},
"dependencies": {
"request": "2.51.x",
"node-bignumber": "1.2.x",
"steamid": "0.1.x",
"xml2js": "0.4.x"
"request": "^2.58.0",
"node-bignumber": "^1.2.1",
"steamid": "^0.3.0",
"xml2js": "^0.4.9",
"cheerio": "^0.19.0"
}
}