3
0
mirror of https://github.com/reality/dbot.git synced 2024-12-25 04:02:39 +01:00

Only use one timer to update all feeds. Properly save lastPosted date

This commit is contained in:
amki 2014-05-24 15:16:56 +00:00
parent 61471e80dc
commit 64f5f61a0a

View File

@ -6,23 +6,22 @@ var FeedParser = require('feedparser')
, request = require('request'); , request = require('request');
var rss = function(dbot) { var rss = function(dbot) {
this.pollInterval = 60000; this.pollInterval = 120000;
var self = this; var self = this;
this.intervals = [];
this.internalAPI = { this.internalAPI = {
'makeRequest': function(id,server,channel,name) { 'makeRequest': function(id,feed) {
var url = dbot.db.feeds[id].url; var fid = id;
var lastPosted = dbot.db.feeds[id].lastPosted; var req = request(feed.url);
var req = request(url);
var feedparser = new FeedParser(); var feedparser = new FeedParser();
//dbot.say(feed.server,feed.channel,"Sending request for feed "+fid+" name "+feed.name+" lP:"+feed.lastPosted);
req.on('error', function (error) { req.on('error', function (error) {
dbot.say(server,channel,"RSS: Request for RSS feed got an error: "+error+" Start self-destruct sequence."); dbot.say(feed.server,feed.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,"RSS: RSS server returned status code "+res.statusCode+". Bastard."); dbot.say(feed.server,feed.channel,"RSS: RSS server returned status code "+res.statusCode+". Bastard.");
return; return;
} }
@ -30,7 +29,7 @@ var rss = function(dbot) {
}); });
feedparser.on('error', function(error) { feedparser.on('error', function(error) {
dbot.say(server,channel,"RSS: Feedparser encountered an error: "+error+";;; Inform administrator!"); dbot.say(feed.server,feed.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!
@ -39,21 +38,25 @@ var rss = function(dbot) {
, item; , item;
while (item = stream.read()) { while (item = stream.read()) {
if(item.pubdate - lastPosted > 0) { if(item.pubdate.getTime() - feed.lastPosted > 0) {
lastPosted = item.pubdate; feed.lastPosted = item.pubdate.getTime();
dbot.say(server,channel,"["+name+"] ["+item.title+"] [Post by "+item.author+" in "+item.categories[0]+"] - "+item.link); // FIXME: This doesn't work AT ALL
dbot.db.feeds.splice(fid,1);
dbot.db.feeds.push(feed);
//
dbot.say(feed.server,feed.channel,"["+feed.name+"] ["+item.title+"] [Post by "+item.author+" in "+item.categories[0]+"] - "+item.link);
} }
} }
}); });
}.bind(this), }.bind(this),
'reloadFeeds': function() { 'checkFeeds': function() {
console.log("Checking 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; this.internalAPI.makeRequest(i,dbot.db.feeds[i]);
var id = i;
this.intervals.push(setInterval(function() {
self.internalAPI.makeRequest(id,server,channel,name)
},self.pollInterval));
} }
}.bind(this),
'reloadFeeds': function() {
return setInterval(this.internalAPI.checkFeeds,self.pollInterval);
}.bind(this) }.bind(this)
}; };
this.commands = { this.commands = {
@ -82,12 +85,10 @@ var rss = function(dbot) {
} }
}; };
this.onLoad = function() { this.onLoad = function() {
this.internalAPI.reloadFeeds(); this.interval = this.internalAPI.reloadFeeds();
}; };
this.onDestroy = function() { this.onDestroy = function() {
for(var i=0;i<this.intervals.length;++i) { clearInterval(this.interval);
clearInterval(this.intervals[i]);
}
}; };
}; };