Only post when there is something new, smaller fixes

This commit is contained in:
amki 2014-05-23 23:27:57 +00:00
parent 358e8b95bb
commit fc4ba61e86

View File

@ -10,26 +10,24 @@ var rss = function(dbot) {
this.intervals = []; this.intervals = [];
this.internalAPI = { this.internalAPI = {
'makeRequest': function(server,channel,name,url) { 'makeRequest': function(server,channel,name,url) {
dbot.say(server,channel,"Starting request for "+name+" with url "+url);
var req = request(url); var req = request(url);
var feedparser = new FeedParser(); var feedparser = new FeedParser();
req.on('error', function (error) { req.on('error', function (error) {
dbot.say(server,channel,"Request got an error: "+error); dbot.say(server,channel,"RSS: Request for RSS feed got an error: "+error+" Start self-destruct sequence.");
}); });
req.on('response', function (res) { req.on('response', function (res) {
var stream = this; var stream = this;
if (res.statusCode != 200){ if (res.statusCode != 200){
dbot.say(server,channel,"Request had status code "+res.statusCode); dbot.say(server,channel,"RSS: RSS server returned status code "+res.statusCode+". Bastard.");
return; return;
} }
stream.pipe(feedparser); stream.pipe(feedparser);
dbot.say(server,channel,"Request piped to feedparser!");
}); });
feedparser.on('error', function(error) { feedparser.on('error', function(error) {
dbot.say(server,channel,"Feedparser got an error: "+error); dbot.say(server,channel,"RSS: Feedparser encountered an error: "+error+";;; Inform administrator!");
}); });
feedparser.on('readable', function() { feedparser.on('readable', function() {
// This is where the action is! // This is where the action is!
@ -38,18 +36,17 @@ var rss = function(dbot) {
, item; , item;
while (item = stream.read()) { while (item = stream.read()) {
dbot.say(server,channel,"@ "+item.pubdate+": ["+name+"] ["+item.title+"] [Post by "+item.author+" in "+item.categories[0]+"] - "+item.link); if(item.pubdate - self.lastPosted > 0)
dbot.say(server,channel,"["+name+"] ["+item.title+"] [Post by "+item.author+" in "+item.categories[0]+"] - "+item.link);
} }
}); });
}.bind(this), }.bind(this),
'reloadFeeds': function() { 'reloadFeeds': function() {
console.log("rss: reloading feeds...");
for(var i=0;i<dbot.db.feeds.length;++i) { for(var i=0;i<dbot.db.feeds.length;++i) {
var server = dbot.db.feeds[i].server, channel = dbot.db.feeds[i].channel, name = dbot.db.feeds[i].name, url = dbot.db.feeds[i].url; var server = dbot.db.feeds[i].server, channel = dbot.db.feeds[i].channel, name = dbot.db.feeds[i].name, url = dbot.db.feeds[i].url;
console.log("Server: "+server+"; Channel: "+channel+" Loading feed "+i+" named "+name);
this.intervals.push(setInterval(function() { this.intervals.push(setInterval(function() {
self.internalAPI.makeRequest(server,channel,name,url) self.internalAPI.makeRequest(server,channel,name,url)
},30000)); },300000));
} }
}.bind(this) }.bind(this)
}; };
@ -60,10 +57,11 @@ var rss = function(dbot) {
return; return;
} }
dbot.db.feeds.push({server:event.server, channel:event.channel.name, name:event.params[1], url:event.params[2]}); dbot.db.feeds.push({server:event.server, channel:event.channel.name, name:event.params[1], url:event.params[2]});
this.intervals.push(setInterval(function() {self.internalAPI.makeRequest(event.server,event.channel.name,event.params[1],event.params[2])},30000)); this.intervals.push(setInterval(function() {self.internalAPI.makeRequest(event.server,event.channel.name,event.params[1],event.params[2])},300000));
event.reply("Adding RSS feed named "+event.params[1]+" with URL "+event.params[2]); event.reply("Adding RSS feed named "+event.params[1]+" with URL "+event.params[2]);
}, },
'~rsstest': function(event) { '~rsstest': function(event) {
event.reply("I posted RSS last @ "+self.lastPosted);
event.reply("Nothing to test. Go home."); event.reply("Nothing to test. Go home.");
}, },
'~delrssfeed': function(event) { '~delrssfeed': function(event) {
@ -78,6 +76,7 @@ var rss = function(dbot) {
}; };
this.onLoad = function() { this.onLoad = function() {
this.internalAPI.reloadFeeds(); this.internalAPI.reloadFeeds();
self.lastPosted = new Date();
}; };
this.onDestroy = function() { this.onDestroy = function() {
for(var i=0;i<this.intervals.length;++i) { for(var i=0;i<this.intervals.length;++i) {