From e1563d03ba31d701fd6443fe316083ccd7f75923 Mon Sep 17 00:00:00 2001 From: Revadike Date: Sun, 10 Oct 2021 06:29:00 +0200 Subject: [PATCH 1/2] Implement steam awards --- classes/CSteamUser.js | 9 ++++- components/awards.js | 89 +++++++++++++++++++++++++++++++++++++++++++ components/users.js | 52 +++++++++++++++++++++++++ index.js | 37 ++++++++++++++++++ 4 files changed, 186 insertions(+), 1 deletion(-) create mode 100644 components/awards.js 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..3a49430d --- /dev/null +++ b/components/awards.js @@ -0,0 +1,89 @@ +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?access_token=" + + this.getAccessToken() + "&input_json=%7B%22steamid%22:%22" + + this.steamID.toString() + "%22%7D", + "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?input_json=%7B%7D", + "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?access_token=" + this.getAccessToken(), + "form": { + "input_json": `{ "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..4f37f689 100644 --- a/components/users.js +++ b/components/users.js @@ -322,6 +322,58 @@ 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/?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 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'); From 3c54bf28e10055f09993fc5a9564e94f13c648f6 Mon Sep 17 00:00:00 2001 From: Revadike Date: Thu, 13 Oct 2022 06:19:08 +0200 Subject: [PATCH 2/2] qs --- components/awards.js | 26 ++++++++++++++++++++------ components/users.js | 10 ++++++++-- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/components/awards.js b/components/awards.js index 3a49430d..2927a164 100644 --- a/components/awards.js +++ b/components/awards.js @@ -10,9 +10,13 @@ SteamCommunity.TargetType = { SteamCommunity.prototype.getPointsSummary = function(callback) { this.httpRequestGet({ - "uri": "https://api.steampowered.com/ILoyaltyRewardsService/GetSummary/v1?access_token=" + - this.getAccessToken() + "&input_json=%7B%22steamid%22:%22" + - this.steamID.toString() + "%22%7D", + "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) { @@ -31,7 +35,10 @@ SteamCommunity.prototype.getPointsSummary = function(callback) { SteamCommunity.prototype.listReactions = function(callback) { this.httpRequestGet({ - "uri": "https://api.steampowered.com/ILoyaltyRewardsService/GetReactionConfig/v1?input_json=%7B%7D", + "uri": "https://api.steampowered.com/ILoyaltyRewardsService/GetReactionConfig/v1", + "qs": { + "input_json": "{}" + }, "json": true }, (err, response, body) => { if (err) { @@ -50,9 +57,16 @@ SteamCommunity.prototype.listReactions = function(callback) { SteamCommunity.prototype.award = function(targetType, targetID, reactionID, callback) { this.httpRequestPost({ - "uri": "https://api.steampowered.com/ILoyaltyRewardsService/AddReaction/v1?access_token=" + this.getAccessToken(), + "uri": "https://api.steampowered.com/ILoyaltyRewardsService/AddReaction/v1", + "qs": { + "access_token": this.getAccessToken() + }, "form": { - "input_json": `{ "target_type": "${targetType}", "targetid": "${targetID}", "reactionid": "${reactionID}" }` + "input_json": JSON.stringify({ + "target_type": targetType, + "targetid": targetID, + "reactionid": reactionID + }) }, "json": true }, function(err, response, body) { // TODO: Investigate body diff --git a/components/users.js b/components/users.js index 4f37f689..6001f5ea 100644 --- a/components/users.js +++ b/components/users.js @@ -333,7 +333,10 @@ SteamCommunity.prototype.getUserAwards = function(userID, callback) { } this.httpRequestGet({ - "uri": "https://steamcommunity.com/profiles/" + userID.getSteamID64() + "/awards/?l=english", + "uri": "https://steamcommunity.com/profiles/" + userID.getSteamID64() + "/awards", + "qs": { + "l": "english" + }, "json": true }, function(err, response, body) { if (err) { @@ -707,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/' },