diff --git a/classes/CSteamUser.js b/classes/CSteamUser.js index f019b77b..b229163f 100644 --- a/classes/CSteamUser.js +++ b/classes/CSteamUser.js @@ -139,7 +139,6 @@ CSteamUser.prototype.acceptFriendRequest = function(callback) { CSteamUser.prototype.removeFriend = function(callback) { this._community.removeFriend(this.steamID, callback); - }; CSteamUser.prototype.blockCommunication = function(callback) { @@ -170,6 +169,14 @@ CSteamUser.prototype.getAliases = function(callback) { this._community.getUserAliases(this.steamID, callback); }; +CSteamUser.prototype.getAwards = function(callback) { + this._community.getUserAwards(this.steamID, callback); +}; + +CSteamUser.prototype.awardProfile = function(reactionID, callback) { + this._community.awardUserProfile(this.steamID, reactionID, callback); +}; + CSteamUser.prototype.getInventoryContexts = function(callback) { this._community.getUserInventoryContexts(this.steamID, callback); }; diff --git a/components/awards.js b/components/awards.js new file mode 100644 index 00000000..2927a164 --- /dev/null +++ b/components/awards.js @@ -0,0 +1,103 @@ +const SteamCommunity = require('../index.js'); + +SteamCommunity.TargetType = { + "UserReview": 1, + "SharedFile": 2, + "Profile": 3, + "Topic": 4, + "Comment": 5 +}; + +SteamCommunity.prototype.getPointsSummary = function(callback) { + this.httpRequestGet({ + "uri": "https://api.steampowered.com/ILoyaltyRewardsService/GetSummary/v1", + "qs": { + "access_token": this.getAccessToken(), + "input_json": JSON.stringify({ + "steamid": this.steamID.toString() + }) + }, + "json": true + }, (err, response, body) => { + if (err) { + callback(err); + return; + } + + if (!body || !body.response || !body.response.summary) { + callback(new Error("Malformed response")); + return; + } + + callback(null, body.response.summary); + }); +}; + +SteamCommunity.prototype.listReactions = function(callback) { + this.httpRequestGet({ + "uri": "https://api.steampowered.com/ILoyaltyRewardsService/GetReactionConfig/v1", + "qs": { + "input_json": "{}" + }, + "json": true + }, (err, response, body) => { + if (err) { + callback(err); + return; + } + + if (!body || !body.response || !body.response.reactions) { + callback(new Error("Malformed response")); + return; + } + + callback(null, body.response.reactions); + }); +}; + +SteamCommunity.prototype.award = function(targetType, targetID, reactionID, callback) { + this.httpRequestPost({ + "uri": "https://api.steampowered.com/ILoyaltyRewardsService/AddReaction/v1", + "qs": { + "access_token": this.getAccessToken() + }, + "form": { + "input_json": JSON.stringify({ + "target_type": targetType, + "targetid": targetID, + "reactionid": reactionID + }) + }, + "json": true + }, function(err, response, body) { // TODO: Investigate body + if(!callback) { + return; + } + + if(err || response.statusCode != 200) { + callback(err || new Error("HTTP error " + response.statusCode)); + } else { + callback(null); + } + }); +}; + +SteamCommunity.prototype.awardUserReview(recommendationID, reactionID, callback) { + this.award(SteamCommunity.TargetType.UserReview, recommendationID, reactionID, callback); +}; + +SteamCommunity.prototype.awardSharedFile = function(sharedFileID, reactionID, callback) { + this.award(SteamCommunity.TargetType.SharedFile, sharedFileID, reactionID, callback); +}; + +SteamCommunity.prototype.awardUserProfile = function(userID, reactionID, callback) { + this.award(SteamCommunity.TargetType.Profile, userID, reactionID, callback); +}; + +SteamCommunity.prototype.awardTopic = function(topicID, reactionID, callback) { + this.award(SteamCommunity.TargetType.Topic, topicID, reactionID, callback); +}; + +SteamCommunity.prototype.awardComment = function(commentID, reactionID, callback) { + this.award(SteamCommunity.TargetType.Comment, commentID, reactionID, callback); +}; diff --git a/components/users.js b/components/users.js index f598a2a9..6001f5ea 100644 --- a/components/users.js +++ b/components/users.js @@ -322,6 +322,61 @@ SteamCommunity.prototype.getUserAliases = function(userID, callback) { }, "steamcommunity"); }; +SteamCommunity.prototype.getUserAwards = function(userID, callback) { + if (!userID) { + callback(new Error("No SteamID specified")); + return; + } + + if (typeof userID === 'string') { + userID = new SteamID(userID); + } + + this.httpRequestGet({ + "uri": "https://steamcommunity.com/profiles/" + userID.getSteamID64() + "/awards", + "qs": { + "l": "english" + }, + "json": true + }, function(err, response, body) { + if (err) { + callback(err); + return; + } + + var $ = Cheerio.load(body); + var awards = {"received": [], "given": []}; + var points = { + "received": parseInt($(".profile_awards_header_subtitle:first").text().split("(")[1].match(/\d/g).join(""), 10), + "given": parseInt($(".profile_awards_header_subtitle:last").text().split("(")[1].match(/\d/g).join(""), 10), + }; + + $(".profile_awards_section:first .profile_award").each(function () { + var split = $(this).find('.profile_award_name').text().split("("); + var name = split[0].trim(); + var icon = $(this).find('.profile_award_icon').attr('src'); + var id = parseInt(icon.split("/").pop().split(".")[0], 10); + var amount = parseInt(split[1].match(/\d/g).join(""), 10); + for (var j = 0; j < amount; j++) { + awards.received.push({id, name, icon}); + } + }); + + $(".profile_awards_section:last .profile_award").each(function () { + var split = $(this).find('.profile_award_name').text().split("("); + var name = split[0].trim(); + var icon = $(this).find('.profile_award_icon').attr('src'); + var id = parseInt(icon.split("/").pop().split(".")[0], 10); + var amount = parseInt(split[1].match(/\d/g).join(""), 10); + for (var j = 0; j < amount; j++) { + awards.given.push({id, name, icon}); + } + }); + + callback(null, awards, points); + }, "steamcommunity"); +}; + /** * Get the background URL of user's profile. * @param {SteamID|string} userID - The user's SteamID as a SteamID object or a string which can parse into one @@ -655,7 +710,10 @@ SteamCommunity.prototype.sendImageToUser = function(userID, imageContentsBuffer, var filename = Date.now() + '_image.' + imageDetails.type; this.httpRequestPost({ - uri: 'https://steamcommunity.com/chat/beginfileupload/?l=english', + uri: 'https://steamcommunity.com/chat/beginfileupload/', + qs: { + "l": "english", + }, headers: { referer: 'https://steamcommunity.com/chat/' }, diff --git a/index.js b/index.js index 54d028d7..d4fbc425 100644 --- a/index.js +++ b/index.js @@ -318,6 +318,42 @@ SteamCommunity.prototype.getSessionID = function(host = "http://steamcommunity.c return sessionID; }; +SteamCommunity.prototype.getAccessToken = function(host = "http://steamcommunity.com", callback) { + var self = this; + if (self._accessToken[host]) { + callback(null, self._accessToken[host]); + return; + } + + self.httpRequestGet({ + "uri": host + '/pointssummary/ajaxgetasyncconfig', + "json": true + }, (err, res, body) => { + if (err) { + callback(err ? err : new Error('HTTP error ' + res.statusCode)); + return; + } + + if (body.success != 1) { + callback(Helpers.eresultError(body.success)); + return; + } + + if (!body.data || !body.data.webapi_token) { + callback(new Error('Malformed response')); + return; + } + + self._accessToken = self._accessToken || {}; + self._accessToken[host] = body.data.webapi_token; + setTimeout(function () { + delete self._accessToken[host]; // delete the cache + }, 60000).unref(); + + callback(null, body.data.webapi_token); + }); +}; + function generateSessionID() { return require('crypto').randomBytes(12).toString('hex'); } @@ -568,6 +604,7 @@ SteamCommunity.prototype.getFriendsList = function(callback) { }; require('./components/http.js'); +require('./components/awards.js'); require('./components/chat.js'); require('./components/profile.js'); require('./components/market.js');