From ac02f6cb494545dbe94cbf5261a825a52ff19ca6 Mon Sep 17 00:00:00 2001 From: Shaun Barratt Date: Sat, 16 Jan 2016 00:06:42 +0000 Subject: [PATCH] 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); + }); }); }); });