3
0
mirror of https://github.com/reality/dbot.git synced 2024-12-24 11:42:36 +01:00

some updates to the rss module

This commit is contained in:
reality 2014-06-19 14:41:57 +00:00
parent c50975672e
commit daa5c9ccc2

View File

@ -2,26 +2,29 @@
* Module Name: RSS * Module Name: RSS
* Description: Allows to read RSS feeds * Description: Allows to read RSS feeds
*/ */
var FeedParser = require('feedparser') var FeedParser = require('feedparser'),
, request = require('request') request = require('request'),
, _ = require('underscore')._; _ = require('underscore')._;
var rss = function(dbot) { var rss = function(dbot) {
this.pollInterval = 120000; this.pollInterval = 120000;
var self = this;
this.internalAPI = { this.internalAPI = {
'makeRequest': function(id,feed) { 'makeRequest': function(id,feed) {
var fid = id; var fid = id,
var req = request(feed.url); req = request(feed.url),
var feedparser = new FeedParser(); 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) {
if(dbot.config.debugMode) {
dbot.say(feed.server,feed.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(feed.server,feed.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,13 +33,16 @@ var rss = function(dbot) {
}); });
feedparser.on('error', function(error) { feedparser.on('error', function(error) {
dbot.say(feed.server,feed.channel,"RSS: Feedparser encountered an error: "+error+";;; Inform administrator!"); if(dbot.config.debugMode) {
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!
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()) {
if(item.pubdate.getTime() - feed.lastPosted > 0) { if(item.pubdate.getTime() - feed.lastPosted > 0) {
@ -72,16 +78,19 @@ var rss = function(dbot) {
dbot.db.feeds[fid] = feed; dbot.db.feeds[fid] = feed;
}); });
}.bind(this), }.bind(this),
'checkFeeds': function() { 'checkFeeds': function() {
console.log("Checking feeds..."); console.log("Checking feeds...");
for(var i=0;i<dbot.db.feeds.length;++i) { for(var i=0;i<dbot.db.feeds.length;++i) {
this.internalAPI.makeRequest(i,dbot.db.feeds[i]); this.internalAPI.makeRequest(i,dbot.db.feeds[i]);
} }
}.bind(this), }.bind(this),
'reloadFeeds': function() { 'reloadFeeds': function() {
return setInterval(this.internalAPI.checkFeeds,self.pollInterval); return setInterval(this.internalAPI.checkFeeds, this.pollInterval);
}.bind(this) }.bind(this)
}; };
this.commands = { this.commands = {
'~addrssfeed': function(event) { '~addrssfeed': function(event) {
if(event.params.length < 3) { if(event.params.length < 3) {
@ -92,24 +101,27 @@ var rss = function(dbot) {
dbot.db.feeds.push({server:event.server, channel:event.channel.name, name:event.params[1], url:event.params[2], lastPosted: now, newTime: now}); dbot.db.feeds.push({server:event.server, channel:event.channel.name, name:event.params[1], url:event.params[2], lastPosted: now, newTime: now});
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("I posted RSS last @ "+this.lastPosted);
event.reply("Nothing to test. Go home."); event.reply("Nothing to test. Go home.");
}, },
'~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) {
if(dbot.db.feeds[i].server == event.server && dbot.db.feeds[i].channel == event.channel.name && dbot.db.feeds[i].name == event.params[1]) { if(dbot.db.feeds[i].server == event.server && dbot.db.feeds[i].channel == event.channel.name && dbot.db.feeds[i].name == event.params[1]) {
event.reply("Found feed "+event.params[1]+" you were looking for..."); dbot.db.feeds.splice(i, 1);
dbot.db.feeds.splice(i,1); event.reply("Removed feed "+event.params[1]+" you were looking for...");
event.reply("... removed!");
break; break;
} }
} }
} }
}; };
this.onLoad = function() { this.onLoad = function() {
this.interval = this.internalAPI.reloadFeeds(); this.interval = this.internalAPI.reloadFeeds();
}; };
this.onDestroy = function() { this.onDestroy = function() {
clearInterval(this.interval); clearInterval(this.interval);
}; };