3
0
mirror of https://github.com/reality/dbot.git synced 2024-11-27 14:29:29 +01:00

Automatic feed reloading and timed requests work

This commit is contained in:
amki 2014-05-23 23:12:47 +00:00
parent f38af0836c
commit 358e8b95bb

View File

@ -6,40 +6,51 @@ var FeedParser = require('feedparser')
, request = require('request');
var rss = function(dbot) {
this.feedparser = new FeedParser();
this.feeds = [];
var self = this;
this.intervals = [];
this.internalAPI = {
'makeRequest': function(server,channel,name,url) {
dbot.say(server,channel,"Starting request for "+name+" with url "+url);
var req = request(url);
var feedparser = new FeedParser();
req.on('error', function (error) {
// handle any request errors
dbot.say(server,channel,"Request got an error: "+error);
});
req.on('response', function (res) {
var stream = this;
if (res.statusCode != 200) return this.emit('error', new Error('Bad status code'));
if (res.statusCode != 200){
dbot.say(server,channel,"Request had status code "+res.statusCode);
return;
}
stream.pipe(this.feedparser);
stream.pipe(feedparser);
dbot.say(server,channel,"Request piped to feedparser!");
});
this.feedparser.on('error', function(error) {
// always handle errors
feedparser.on('error', function(error) {
dbot.say(server,channel,"Feedparser got an error: "+error);
});
this.feedparser.on('readable', function() {
feedparser.on('readable', function() {
// This is where the action is!
var stream = this
, meta = this.meta // **NOTE** the "meta" is always available in the context of the feedparser instance
, item;
while (item = stream.read()) {
dbot.say(server,channel,"FEED: "+item.title);
dbot.say(server,channel,"@ "+item.pubdate+": ["+name+"] ["+item.title+"] [Post by "+item.author+" in "+item.categories[0]+"] - "+item.link);
}
});
}.bind(this),
'reloadFeeds': function() {
console.log("rss: reloading feeds...");
// TODO actually reload feeds, set timers to refresh, refresh
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;
console.log("Server: "+server+"; Channel: "+channel+" Loading feed "+i+" named "+name);
this.intervals.push(setInterval(function() {
self.internalAPI.makeRequest(server,channel,name,url)
},30000));
}
}.bind(this)
};
this.commands = {
@ -49,13 +60,11 @@ var rss = function(dbot) {
return;
}
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));
event.reply("Adding RSS feed named "+event.params[1]+" with URL "+event.params[2]);
},
'~getrssfeed': function(event) {
event.reply("Can't :'(");
},
'~test': function(event) {
this.internalAPI.makeRequest(event.server,event.channel.name,event.params[1],event.params[2]);
'~rsstest': function(event) {
event.reply("Nothing to test. Go home.");
},
'~delrssfeed': function(event) {
for(var i=0;i<dbot.db.feeds.length;++i) {
@ -69,23 +78,12 @@ var rss = function(dbot) {
};
this.onLoad = function() {
this.internalAPI.reloadFeeds();
/*
this.feedparser = new FeedParser();
this.feedparser.on('error', function(error) {
// always handle errors
});
this.feedparser.on('readable', function() {
// This is where the action is!
var stream = this
, meta = this.meta // **NOTE** the "meta" is always available in the context of the feedparser instance
, item;
while (item = stream.read()) {
event.reply("roiroiroi -> "+item.title+" <- -> "+item.link+" <-");
}
});
*/
}
};
this.onDestroy = function() {
for(var i=0;i<this.intervals.length;++i) {
clearInterval(this.intervals[i]);
}
};
};
exports.fetch = function(dbot) {