3
0
mirror of https://github.com/reality/dbot.git synced 2024-11-23 20:39:25 +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'); , request = require('request');
var rss = function(dbot) { var rss = function(dbot) {
this.feedparser = new FeedParser(); var self = this;
this.feeds = []; 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); dbot.say(server,channel,"Starting request for "+name+" with url "+url);
var req = request(url); var req = request(url);
var feedparser = new FeedParser();
req.on('error', function (error) { req.on('error', function (error) {
// handle any request errors dbot.say(server,channel,"Request got an error: "+error);
}); });
req.on('response', function (res) { req.on('response', function (res) {
var stream = this; 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) { feedparser.on('error', function(error) {
// always handle errors dbot.say(server,channel,"Feedparser got an error: "+error);
}); });
this.feedparser.on('readable', function() { feedparser.on('readable', function() {
// This is where the action is! // This is where the action is!
var stream = this var stream = this
, meta = this.meta // **NOTE** the "meta" is always available in the context of the feedparser instance , meta = this.meta // **NOTE** the "meta" is always available in the context of the feedparser instance
, item; , item;
while (item = stream.read()) { 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), }.bind(this),
'reloadFeeds': function() { 'reloadFeeds': function() {
console.log("rss: reloading feeds..."); 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) }.bind(this)
}; };
this.commands = { this.commands = {
@ -49,13 +60,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));
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]);
}, },
'~getrssfeed': function(event) { '~rsstest': function(event) {
event.reply("Can't :'("); event.reply("Nothing to test. Go home.");
},
'~test': function(event) {
this.internalAPI.makeRequest(event.server,event.channel.name,event.params[1],event.params[2]);
}, },
'~delrssfeed': function(event) { '~delrssfeed': function(event) {
for(var i=0;i<dbot.db.feeds.length;++i) { for(var i=0;i<dbot.db.feeds.length;++i) {
@ -69,23 +78,12 @@ var rss = function(dbot) {
}; };
this.onLoad = function() { this.onLoad = function() {
this.internalAPI.reloadFeeds(); this.internalAPI.reloadFeeds();
/* };
this.feedparser = new FeedParser(); this.onDestroy = function() {
this.feedparser.on('error', function(error) { for(var i=0;i<this.intervals.length;++i) {
// always handle errors clearInterval(this.intervals[i]);
}); }
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+" <-");
}
});
*/
}
}; };
exports.fetch = function(dbot) { exports.fetch = function(dbot) {