From 3a2374ad596107c66ebaed6449bf598ff6826206 Mon Sep 17 00:00:00 2001 From: reality Date: Thu, 19 Jun 2014 14:51:54 +0000 Subject: [PATCH] dont repeat items with the same title within a bound of 30 --- modules/rss/rss.js | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/modules/rss/rss.js b/modules/rss/rss.js index a9cf11e..f913da8 100644 --- a/modules/rss/rss.js +++ b/modules/rss/rss.js @@ -8,6 +8,8 @@ var FeedParser = require('feedparser'), var rss = function(dbot) { this.pollInterval = 120000; + this.titleCache = []; + var self = this; this.internalAPI = { 'makeRequest': function(id,feed) { @@ -50,26 +52,32 @@ var rss = function(dbot) { feed.newTime = item.pubdate.getTime(); } var rss = item; - var options = { - uri: 'https://www.googleapis.com/urlshortener/v1/url', - method: 'POST', - json: { - "longUrl": rss.link - } - }; - request(options, function (error, response, body) { - if (!error && response.statusCode === 200) { - var rString = "["+feed.name+"] ["+rss.title+"] "; - if(rss.author !== null && !_.isUndefined(rss.categories[0])) { - rString += "[Post by "+rss.author+" in "+rss.categories[0]+"] "; + if(!_.include(self.titleCache, rss.title)) { + var options = { + uri: 'https://www.googleapis.com/urlshortener/v1/url', + method: 'POST', + json: { + "longUrl": rss.link } - rString += "- "+body.id; - dbot.say(feed.server,feed.channel, rString); - } else { - dbot.say(feed.server,feed.channel,"RSS: Request to shorten URL returned: "+body.id); + }; + + request(options, function (error, response, body) { + if (!error && response.statusCode === 200) { + var rString = "["+feed.name+"] ["+rss.title+"] "; + if(rss.author !== null && !_.isUndefined(rss.categories[0])) { + rString += "[Post by "+rss.author+" in "+rss.categories[0]+"] "; + } + rString += "- "+body.id; + dbot.say(feed.server,feed.channel, rString); + } + }); + + if(self.titleCache.length > 30) { + self.titleCache.splice(0, 1); } - }); + self.titleCache.push(feed.title); + } } } });