Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement steam awards #295

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion classes/CSteamUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
};
Expand Down
103 changes: 103 additions & 0 deletions components/awards.js
Original file line number Diff line number Diff line change
@@ -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);
};
60 changes: 59 additions & 1 deletion components/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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/'
},
Expand Down
37 changes: 37 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down Expand Up @@ -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');
Expand Down