From 4152865f5eb2097124becd5c10deb53d7686c063 Mon Sep 17 00:00:00 2001 From: Shaun Barratt Date: Fri, 15 Jan 2016 17:48:57 +0000 Subject: [PATCH 1/5] Adding ability to edit an annoucement. Added an example. --- classes/CSteamGroup.js | 18 ++++--- components/groups.js | 39 ++++++++++++++ examples/edit-group-announcement.js | 79 +++++++++++++++++++++++++++++ 3 files changed, 129 insertions(+), 7 deletions(-) create mode 100644 examples/edit-group-announcement.js diff --git a/classes/CSteamGroup.js b/classes/CSteamGroup.js index 2ac4937..812f42b 100644 --- a/classes/CSteamGroup.js +++ b/classes/CSteamGroup.js @@ -6,27 +6,27 @@ SteamCommunity.prototype.getSteamGroup = function(id, callback) { if(typeof id !== 'string' && !(typeof id === 'object' && id.__proto__ === SteamID.prototype)) { throw new Error("id parameter should be a group URL string or a SteamID object"); } - + if(typeof id === 'object' && (id.universe != SteamID.Universe.PUBLIC || id.type != SteamID.Type.CLAN)) { throw new Error("SteamID must stand for a clan account in the public universe"); } - + var self = this; this.request("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; } - + if(self._checkCommunityError(body, callback)) { return; } - + xml2js.parseString(body, function(err, result) { if(err) { callback(err); return; } - + callback(null, new CSteamGroup(self, result.memberList)); }); }); @@ -34,7 +34,7 @@ SteamCommunity.prototype.getSteamGroup = function(id, callback) { function CSteamGroup(community, groupData) { this._community = community; - + this.steamID = new SteamID(groupData.groupID64[0]); this.name = groupData.groupDetails[0].groupName[0]; this.url = groupData.groupDetails[0].groupURL[0]; @@ -50,7 +50,7 @@ function CSteamGroup(community, groupData) { CSteamGroup.prototype.getAvatarURL = function(size, protocol) { size = size || ''; protocol = protocol || 'http://'; - + var url = protocol + "steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/" + this.avatarHash.substring(0, 2) + "/" + this.avatarHash; if(size == 'full' || size == 'medium') { return url + "_" + size + ".jpg"; @@ -80,6 +80,10 @@ CSteamGroup.prototype.postAnnouncement = function(headline, content, callback) { this._community.postGroupAnnouncement(this.steamID, headline, content, callback); }; +CSteamGroup.prototype.editAnnouncement = function(annoucementID, headline, content, callback) { + this._community.editGroupAnnouncement(this.steamID, annoucementID, headline, content, callback) +}; + CSteamGroup.prototype.scheduleEvent = function(name, type, description, time, server, callback) { this._community.scheduleGroupEvent(this.steamID, name, type, description, time, server, callback); }; diff --git a/components/groups.js b/components/groups.js index b12a6dc..767417b 100644 --- a/components/groups.js +++ b/components/groups.js @@ -159,6 +159,45 @@ SteamCommunity.prototype.postGroupAnnouncement = function(gid, headline, content }) }; +SteamCommunity.prototype.editGroupAnnouncement = function(gid, aid, headline, content, callback) { + if(typeof gid === 'string') { + gid = new SteamID(gid); + } + + var self = this; + + var submitData = { + "uri": "https://steamcommunity.com/gid/" + gid.getSteamID64() + "/announcements", + "form": { + "sessionID": this.getSessionID(), + "gid": aid, + "action": "update", + "headline": headline, + "body": content, + "languages[0][headline]": headline, + "languages[0][body]": content, + "languages[0][updated]": 1 + } + } + + this.request.post(submitData, function(err, response, body) { + if(!callback) { + return; + } + + if(err || response.statusCode >= 400) { + callback(err || new Error("HTTP error " + response.statusCode)); + return; + } + + if(self._checkCommunityError(body, callback)) { + return; + } + + callback(null); + }) +}; + SteamCommunity.prototype.scheduleGroupEvent = function(gid, name, type, description, time, server, callback) { if(typeof gid === 'string') { gid = new SteamID(gid); diff --git a/examples/edit-group-announcement.js b/examples/edit-group-announcement.js new file mode 100644 index 0000000..e59932b --- /dev/null +++ b/examples/edit-group-announcement.js @@ -0,0 +1,79 @@ +var SteamCommunity = require('../index.js'); +var ReadLine = require('readline'); +var fs = require('fs'); + +var community = new SteamCommunity(); +var rl = ReadLine.createInterface({ + "input": process.stdin, + "output": process.stdout +}); + +rl.question("Username: ", function(accountName) { + rl.question("Password: ", function(password) { + doLogin(accountName, password); + }); +}); + +function doLogin(accountName, password, authCode, twoFactorCode) { + community.login({ + "accountName": accountName, + "password": password, + "authCode": authCode, + "twoFactorCode": twoFactorCode + }, function(err, sessionID, cookies, steamguard) { + if(err) { + if(err.message == 'SteamGuardMobile') { + rl.question("Steam Authenticator Code: ", function(code) { + doLogin(accountName, password, null, code); + }); + + return; + } + + if(err.message == 'SteamGuard') { + console.log("An email has been sent to your address at " + err.emaildomain); + rl.question("Steam Guard Code: ", function(code) { + doLogin(accountName, password, code); + }); + + return; + } + + console.log(err); + process.exit(); + return; + } + + console.log("Logged on!"); + + rl.question("Group ID: ", function(gid) { + community.getSteamGroup(gid, function(err, group) { + if (err) { + console.log(err); + process.exit(1); + } + + rl.question("Annoucement ID: ", function(aid) { + rl.question("New title: ", function(header) { + rl.question("New body: ", function(content) { + // EW THE PYRAMID! + editAnnouncement(group, aid, header, content); + }); + }); + }); + }); + }); + }); +} + +function editAnnouncement(group, aid, header, content) { + // Actual community method. + group.editAnnouncement(aid, header, content, function(error) { + if(!error) { + console.log("Annoucement edited!"); + } else { + console.log("Unable to edit annoucement! %j", error); + process.exit(1); + } + }); +} From d84601e2567ed22e2dd9cca6a6325542e66bc67f Mon Sep 17 00:00:00 2001 From: Shaun Barratt Date: Fri, 15 Jan 2016 21:28:43 +0000 Subject: [PATCH 2/5] Adding the whitespace back (blame atom.io!) Converting spaces to tabs. --- classes/CSteamGroup.js | 16 ++++---- examples/edit-group-announcement.js | 60 ++++++++++++++--------------- 2 files changed, 38 insertions(+), 38 deletions(-) diff --git a/classes/CSteamGroup.js b/classes/CSteamGroup.js index 812f42b..2f74e7c 100644 --- a/classes/CSteamGroup.js +++ b/classes/CSteamGroup.js @@ -6,27 +6,27 @@ SteamCommunity.prototype.getSteamGroup = function(id, callback) { if(typeof id !== 'string' && !(typeof id === 'object' && id.__proto__ === SteamID.prototype)) { throw new Error("id parameter should be a group URL string or a SteamID object"); } - + if(typeof id === 'object' && (id.universe != SteamID.Universe.PUBLIC || id.type != SteamID.Type.CLAN)) { throw new Error("SteamID must stand for a clan account in the public universe"); } - + var self = this; this.request("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; } - + if(self._checkCommunityError(body, callback)) { return; } - + xml2js.parseString(body, function(err, result) { if(err) { callback(err); return; } - + callback(null, new CSteamGroup(self, result.memberList)); }); }); @@ -34,7 +34,7 @@ SteamCommunity.prototype.getSteamGroup = function(id, callback) { function CSteamGroup(community, groupData) { this._community = community; - + this.steamID = new SteamID(groupData.groupID64[0]); this.name = groupData.groupDetails[0].groupName[0]; this.url = groupData.groupDetails[0].groupURL[0]; @@ -50,7 +50,7 @@ function CSteamGroup(community, groupData) { CSteamGroup.prototype.getAvatarURL = function(size, protocol) { size = size || ''; protocol = protocol || 'http://'; - + var url = protocol + "steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/" + this.avatarHash.substring(0, 2) + "/" + this.avatarHash; if(size == 'full' || size == 'medium') { return url + "_" + size + ".jpg"; @@ -64,7 +64,7 @@ CSteamGroup.prototype.getMembers = function(addresses, callback) { callback = addresses; addresses = null; } - + this._community.getGroupMembers(this.steamID, callback, null, null, addresses, 0); }; diff --git a/examples/edit-group-announcement.js b/examples/edit-group-announcement.js index e59932b..fdea5e7 100644 --- a/examples/edit-group-announcement.js +++ b/examples/edit-group-announcement.js @@ -19,15 +19,15 @@ function doLogin(accountName, password, authCode, twoFactorCode) { "accountName": accountName, "password": password, "authCode": authCode, - "twoFactorCode": twoFactorCode + "twoFactorCode": twoFactorCode }, function(err, sessionID, cookies, steamguard) { if(err) { if(err.message == 'SteamGuardMobile') { - rl.question("Steam Authenticator Code: ", function(code) { - doLogin(accountName, password, null, code); + rl.question("Steam Authenticator Code: ", function(code) { + doLogin(accountName, password, null, code); }); - return; + return; } if(err.message == 'SteamGuard') { @@ -46,34 +46,34 @@ function doLogin(accountName, password, authCode, twoFactorCode) { console.log("Logged on!"); - rl.question("Group ID: ", function(gid) { - community.getSteamGroup(gid, function(err, group) { - if (err) { - console.log(err); - process.exit(1); - } + rl.question("Group ID: ", function(gid) { + community.getSteamGroup(gid, function(err, group) { + if (err) { + console.log(err); + process.exit(1); + } - rl.question("Annoucement ID: ", function(aid) { - rl.question("New title: ", function(header) { - rl.question("New body: ", function(content) { - // EW THE PYRAMID! - editAnnouncement(group, aid, header, content); - }); - }); - }); - }); - }); - }); + rl.question("Annoucement ID: ", function(aid) { + rl.question("New title: ", function(header) { + rl.question("New body: ", function(content) { + // EW THE PYRAMID! + editAnnouncement(group, aid, header, content); + }); + }); + }); + }); + }); + }); } function editAnnouncement(group, aid, header, content) { - // Actual community method. - group.editAnnouncement(aid, header, content, function(error) { - if(!error) { - console.log("Annoucement edited!"); - } else { - console.log("Unable to edit annoucement! %j", error); - process.exit(1); - } - }); + // Actual community method. + group.editAnnouncement(aid, header, content, function(error) { + if(!error) { + console.log("Annoucement edited!"); + } else { + console.log("Unable to edit annoucement! %j", error); + process.exit(1); + } + }); } From ac02f6cb494545dbe94cbf5261a825a52ff19ca6 Mon Sep 17 00:00:00 2001 From: Shaun Barratt Date: Sat, 16 Jan 2016 00:06:42 +0000 Subject: [PATCH 3/5] Added getAllAnnouncements Added example on how to use it --- classes/CSteamGroup.js | 4 +++ components/groups.js | 56 +++++++++++++++++++++++++++++ examples/edit-group-announcement.js | 21 ++++++++--- 3 files changed, 76 insertions(+), 5 deletions(-) diff --git a/classes/CSteamGroup.js b/classes/CSteamGroup.js index 2f74e7c..c7e7800 100644 --- a/classes/CSteamGroup.js +++ b/classes/CSteamGroup.js @@ -76,6 +76,10 @@ CSteamGroup.prototype.leave = function(callback) { this._community.leaveGroup(this.steamID, callback); }; +CSteamGroup.prototype.getAllAnnouncements = function(time, callback) { + this._community.getAllAnnouncements(this.steamID, time, callback); +}; + CSteamGroup.prototype.postAnnouncement = function(headline, content, callback) { this._community.postGroupAnnouncement(this.steamID, headline, content, callback); }; diff --git a/components/groups.js b/components/groups.js index 767417b..b9e088e 100644 --- a/components/groups.js +++ b/components/groups.js @@ -125,6 +125,62 @@ SteamCommunity.prototype.leaveGroup = function(gid, callback) { }); }; +SteamCommunity.prototype.getAllAnnouncements = function(gid, time, callback) { + if(typeof gid === 'string') { + gid = new SteamID(gid); + } + + if(typeof time === 'function') { + callback = time; + time = new Date(0); // The beginnig of time... + } + + var self = this; + this.request({ + "uri": "https://steamcommunity.com/gid/" + gid.getSteamID64() + "/rss/" + }, function(err, response, body) { + if(!callback) { + return; + } + + if(err || response.statusCode >= 400) { + callback(err || new Error("HTTP error " + response.statusCode)); + return; + } + + if(self._checkCommunityError(body, callback)) { + return; + } + + xml2js.parseString(body, function(err, results) { + if(err) { + return callback(err); + } + + if(!results.rss.channel[0].item) { + return callback(null, []); + } + + var announcements = results.rss.channel[0].item.map(function(announcement) { + var splitLink = announcement.link[0].split('/'); + return { + headline: announcement.title[0], + content: announcement.description[0], + date: new Date(announcement.pubDate[0]), + author: announcement.author[0], // Unfortunately, the RSS feed likes to give us personanames not steamid's + aid: splitLink[splitLink.length - 1] // The ID after the last / + // Note this is marked as guid (gid?) in the rss feed but can also be obtained from link + // and is actually a unique ID (or it seems that way) + } + }).filter(function(announcement) { // Only show the ones they wanted + return (announcement.date > time); + }); + + return callback(null, announcements); + }); + }); +} + SteamCommunity.prototype.postGroupAnnouncement = function(gid, headline, content, callback) { if(typeof gid === 'string') { gid = new SteamID(gid); diff --git a/examples/edit-group-announcement.js b/examples/edit-group-announcement.js index fdea5e7..bc4d7d3 100644 --- a/examples/edit-group-announcement.js +++ b/examples/edit-group-announcement.js @@ -53,11 +53,22 @@ function doLogin(accountName, password, authCode, twoFactorCode) { process.exit(1); } - rl.question("Annoucement ID: ", function(aid) { - rl.question("New title: ", function(header) { - rl.question("New body: ", function(content) { - // EW THE PYRAMID! - editAnnouncement(group, aid, header, content); + group.getAllAnnouncements(function(err, announcements) { + + if(announcements.length === 0) { + return console.log("This group has no announcements"); + } + + for (var i = announcements.length - 1; i >= 0; i--) { + console.log("[%s] %s %s: %s", announcements[i].date, announcements[i].aid, announcements[i].author, announcements[i].content); + }; + + rl.question("Annoucement ID: ", function(aid) { + rl.question("New title: ", function(header) { + rl.question("New body: ", function(content) { + // EW THE PYRAMID! + editAnnouncement(group, aid, header, content); + }); }); }); }); From 4223d88fd1e2e91149adceb81d7c38297eddedee Mon Sep 17 00:00:00 2001 From: Shaun Barratt Date: Sat, 16 Jan 2016 18:39:04 +0000 Subject: [PATCH 4/5] Catching HTTP errors properly Specifying the Group in getAllGroupAnnouncements Removing unnecessary comments Made the callback for getAllGroupAnnouncements mandatory --- classes/CSteamGroup.js | 2 +- components/groups.js | 24 ++++++++---------------- 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/classes/CSteamGroup.js b/classes/CSteamGroup.js index c7e7800..9c26b78 100644 --- a/classes/CSteamGroup.js +++ b/classes/CSteamGroup.js @@ -77,7 +77,7 @@ CSteamGroup.prototype.leave = function(callback) { }; CSteamGroup.prototype.getAllAnnouncements = function(time, callback) { - this._community.getAllAnnouncements(this.steamID, time, callback); + this._community.getAllGroupAnnouncements(this.steamID, time, callback); }; CSteamGroup.prototype.postAnnouncement = function(headline, content, callback) { diff --git a/components/groups.js b/components/groups.js index b9e088e..49df825 100644 --- a/components/groups.js +++ b/components/groups.js @@ -125,7 +125,7 @@ SteamCommunity.prototype.leaveGroup = function(gid, callback) { }); }; -SteamCommunity.prototype.getAllAnnouncements = function(gid, time, callback) { +SteamCommunity.prototype.getAllGroupAnnouncements = function(gid, time, callback) { if(typeof gid === 'string') { gid = new SteamID(gid); } @@ -136,15 +136,11 @@ SteamCommunity.prototype.getAllAnnouncements = function(gid, time, callback) { } var self = this; - this.request({ + this.request({ "uri": "https://steamcommunity.com/gid/" + gid.getSteamID64() + "/rss/" }, function(err, response, body) { - if(!callback) { - return; - } - if(err || response.statusCode >= 400) { - callback(err || new Error("HTTP error " + response.statusCode)); + if(self._checkHttpError(err, response, callback)) { return; } @@ -167,12 +163,10 @@ SteamCommunity.prototype.getAllAnnouncements = function(gid, time, callback) { headline: announcement.title[0], content: announcement.description[0], date: new Date(announcement.pubDate[0]), - author: announcement.author[0], // Unfortunately, the RSS feed likes to give us personanames not steamid's - aid: splitLink[splitLink.length - 1] // The ID after the last / - // Note this is marked as guid (gid?) in the rss feed but can also be obtained from link - // and is actually a unique ID (or it seems that way) + author: announcement.author[0], + aid: splitLink[splitLink.length - 1] } - }).filter(function(announcement) { // Only show the ones they wanted + }).filter(function(announcement) { return (announcement.date > time); }); @@ -202,8 +196,7 @@ SteamCommunity.prototype.postGroupAnnouncement = function(gid, headline, content return; } - if(err || response.statusCode >= 400) { - callback(err || new Error("HTTP error " + response.statusCode)); + if(self._checkHttpError(err, response, callback)) { return; } @@ -241,8 +234,7 @@ SteamCommunity.prototype.editGroupAnnouncement = function(gid, aid, headline, co return; } - if(err || response.statusCode >= 400) { - callback(err || new Error("HTTP error " + response.statusCode)); + if(self._checkHttpError(err, response, callback)) { return; } From 27ef78cc4bfd6f781b41284d59944d958259eefd Mon Sep 17 00:00:00 2001 From: Shaun Barratt Date: Sun, 17 Jan 2016 02:27:11 +0000 Subject: [PATCH 5/5] Added ability to deleteAnnoucement --- classes/CSteamGroup.js | 4 ++++ components/groups.js | 28 ++++++++++++++++++++++++ examples/edit-group-announcement.js | 33 +++++++++++++++++++++++------ 3 files changed, 58 insertions(+), 7 deletions(-) diff --git a/classes/CSteamGroup.js b/classes/CSteamGroup.js index 9c26b78..7b6801d 100644 --- a/classes/CSteamGroup.js +++ b/classes/CSteamGroup.js @@ -88,6 +88,10 @@ CSteamGroup.prototype.editAnnouncement = function(annoucementID, headline, conte this._community.editGroupAnnouncement(this.steamID, annoucementID, headline, content, callback) }; +CSteamGroup.prototype.deleteAnnouncement = function(annoucementID, headline, content, callback) { + this._community.deleteGroupAnnouncement(this.steamID, annoucementID, headline, content, callback) +}; + CSteamGroup.prototype.scheduleEvent = function(name, type, description, time, server, callback) { this._community.scheduleGroupEvent(this.steamID, name, type, description, time, server, callback); }; diff --git a/components/groups.js b/components/groups.js index 49df825..3b19c46 100644 --- a/components/groups.js +++ b/components/groups.js @@ -246,6 +246,34 @@ SteamCommunity.prototype.editGroupAnnouncement = function(gid, aid, headline, co }) }; +SteamCommunity.prototype.deleteGroupAnnouncement = function(gid, aid, callback) { + if(typeof gid === 'string') { + gid = new SteamID(gid); + } + + var self = this; + + var submitData = { + "uri": "https://steamcommunity.com/gid/" + gid.getSteamID64() + "/announcements/delete/" + aid + "?sessionID=" + this.getSessionID(), + } + + this.request.get(submitData, function(err, response, body) { + if(!callback) { + return; + } + + if(self._checkHttpError(err, response, callback)) { + return; + } + + if(self._checkCommunityError(body, callback)) { + return; + } + + callback(null); + }) +}; + SteamCommunity.prototype.scheduleGroupEvent = function(gid, name, type, description, time, server, callback) { if(typeof gid === 'string') { gid = new SteamID(gid); diff --git a/examples/edit-group-announcement.js b/examples/edit-group-announcement.js index bc4d7d3..4d6fbcc 100644 --- a/examples/edit-group-announcement.js +++ b/examples/edit-group-announcement.js @@ -54,7 +54,7 @@ function doLogin(accountName, password, authCode, twoFactorCode) { } group.getAllAnnouncements(function(err, announcements) { - + if(announcements.length === 0) { return console.log("This group has no announcements"); } @@ -63,12 +63,19 @@ function doLogin(accountName, password, authCode, twoFactorCode) { console.log("[%s] %s %s: %s", announcements[i].date, announcements[i].aid, announcements[i].author, announcements[i].content); }; - rl.question("Annoucement ID: ", function(aid) { - rl.question("New title: ", function(header) { - rl.question("New body: ", function(content) { - // EW THE PYRAMID! - editAnnouncement(group, aid, header, content); - }); + rl.question("Would you like to delete delete or edit an annoucement? (Type edit/delete): ", function(choice) { + rl.question("Annoucement ID: ", function(aid) { + if(choice === 'edit') { + rl.question("New title: ", function(header) { + rl.question("New body: ", function(content) { + // EW THE PYRAMID! + // Try replace this with delete! + editAnnouncement(group, aid, header, content); + }); + }); + } else { + deleteAnnouncement(group, aid); + } }); }); }); @@ -88,3 +95,15 @@ function editAnnouncement(group, aid, header, content) { } }); } + +function deleteAnnouncement(group, aid) { + // group.deleteAnnouncement(aid); + // Or + group.deleteAnnouncement(aid, function(err) { + if(!err) { + console.log("Deleted"); + } else { + console.log("Error deleting announcement."); + } + }) +}