2014-05-23 03:24:45 +02:00
|
|
|
/**
|
|
|
|
* Module Name: RSS
|
|
|
|
* Description: Allows to read RSS feeds
|
|
|
|
*/
|
2014-06-19 16:41:57 +02:00
|
|
|
var FeedParser = require('feedparser'),
|
|
|
|
request = require('request'),
|
|
|
|
_ = require('underscore')._;
|
2014-05-23 03:24:45 +02:00
|
|
|
|
|
|
|
var rss = function(dbot) {
|
2014-05-24 17:16:56 +02:00
|
|
|
this.pollInterval = 120000;
|
2014-06-19 16:51:54 +02:00
|
|
|
this.titleCache = [];
|
|
|
|
var self = this;
|
2014-06-19 16:41:57 +02:00
|
|
|
|
2014-05-23 03:24:45 +02:00
|
|
|
this.internalAPI = {
|
2014-05-24 17:16:56 +02:00
|
|
|
'makeRequest': function(id,feed) {
|
2014-06-19 16:41:57 +02:00
|
|
|
var fid = id,
|
|
|
|
req = request(feed.url),
|
|
|
|
feedparser = new FeedParser();
|
|
|
|
|
2014-05-23 03:24:45 +02:00
|
|
|
req.on('error', function (error) {
|
2014-06-19 16:41:57 +02:00
|
|
|
if(dbot.config.debugMode) {
|
2014-05-24 17:16:56 +02:00
|
|
|
dbot.say(feed.server,feed.channel,"RSS: Request for RSS feed got an error: "+error+" Start self-destruct sequence.");
|
2014-06-19 16:41:57 +02:00
|
|
|
}
|
2014-05-23 03:24:45 +02:00
|
|
|
});
|
2014-06-19 16:41:57 +02:00
|
|
|
|
2014-05-23 03:24:45 +02:00
|
|
|
req.on('response', function (res) {
|
|
|
|
var stream = this;
|
2014-06-19 16:41:57 +02:00
|
|
|
if (res.statusCode !== 200){
|
2014-05-24 17:16:56 +02:00
|
|
|
dbot.say(feed.server,feed.channel,"RSS: RSS server returned status code "+res.statusCode+". Bastard.");
|
2014-05-24 01:12:47 +02:00
|
|
|
return;
|
|
|
|
}
|
2014-05-23 03:24:45 +02:00
|
|
|
|
2014-05-24 01:12:47 +02:00
|
|
|
stream.pipe(feedparser);
|
2014-05-23 03:24:45 +02:00
|
|
|
});
|
|
|
|
|
2014-05-24 01:12:47 +02:00
|
|
|
feedparser.on('error', function(error) {
|
2014-06-19 16:41:57 +02:00
|
|
|
if(dbot.config.debugMode) {
|
|
|
|
dbot.say(feed.server,feed.channel,"RSS: Feedparser encountered an error: "+error+";;; Inform administrator!");
|
|
|
|
}
|
2014-05-23 03:24:45 +02:00
|
|
|
});
|
2014-06-19 16:41:57 +02:00
|
|
|
|
2014-05-24 01:12:47 +02:00
|
|
|
feedparser.on('readable', function() {
|
2014-05-23 03:24:45 +02:00
|
|
|
// This is where the action is!
|
2014-06-19 16:41:57 +02:00
|
|
|
var stream = this,
|
|
|
|
meta = this.meta, // **NOTE** the "meta" is always available in the context of the feedparser instance
|
|
|
|
item;
|
2014-05-23 03:24:45 +02:00
|
|
|
|
|
|
|
while (item = stream.read()) {
|
2014-09-18 23:06:17 +02:00
|
|
|
// If the feed does not give a pubdate for this post, ignore it...
|
|
|
|
if(!item.pubdate) {
|
|
|
|
return;
|
|
|
|
}
|
2015-04-04 01:24:50 +02:00
|
|
|
if(dbot.config.debugMode) {
|
|
|
|
dbot.say(feed.server,feed.channel,"RSS: Should post question: Is "+(item.pubdate.getTime()-feed.lastPosted)+" > 0?");
|
|
|
|
}
|
2014-05-24 17:16:56 +02:00
|
|
|
if(item.pubdate.getTime() - feed.lastPosted > 0) {
|
2014-05-25 00:22:34 +02:00
|
|
|
if(item.pubdate.getTime() > feed.newTime) {
|
|
|
|
feed.newTime = item.pubdate.getTime();
|
|
|
|
}
|
2014-05-24 19:13:40 +02:00
|
|
|
var rss = item;
|
2015-04-04 01:24:50 +02:00
|
|
|
if(dbot.config.debugMode) {
|
|
|
|
dbot.say(feed.server,feed.channel,"RSS: I shall post a new link! PERFECT.");
|
|
|
|
}
|
2014-06-19 16:51:54 +02:00
|
|
|
if(!_.include(self.titleCache, rss.title)) {
|
|
|
|
var options = {
|
|
|
|
uri: 'https://www.googleapis.com/urlshortener/v1/url',
|
|
|
|
method: 'POST',
|
|
|
|
json: {
|
|
|
|
"longUrl": rss.link
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
request(options, function (error, response, body) {
|
|
|
|
if (!error && response.statusCode === 200) {
|
|
|
|
var rString = "["+feed.name+"] ["+rss.title+"] ";
|
|
|
|
if(rss.author !== null && !_.isUndefined(rss.categories[0])) {
|
|
|
|
rString += "[Post by "+rss.author+" in "+rss.categories[0]+"] ";
|
|
|
|
}
|
|
|
|
rString += "- "+body.id;
|
|
|
|
dbot.say(feed.server,feed.channel, rString);
|
2015-04-04 01:24:50 +02:00
|
|
|
} else {
|
|
|
|
var rString = "["+feed.name+"] ["+rss.title+"] ";
|
|
|
|
if(rss.author !== null && !_.isUndefined(rss.categories[0])) {
|
|
|
|
rString += "[Post by "+rss.author+" in "+rss.categories[0]+"] ";
|
|
|
|
}
|
|
|
|
rString += "- "+rss.link;
|
|
|
|
dbot.say(feed.server,feed.channel, rString);
|
|
|
|
console.log("RSS: Url shortener request returned error statuscode "+response.statusCode+": "+body.error.message);
|
2014-06-16 17:53:27 +02:00
|
|
|
}
|
2014-06-19 16:51:54 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if(self.titleCache.length > 30) {
|
|
|
|
self.titleCache.splice(0, 1);
|
2014-05-24 19:13:40 +02:00
|
|
|
}
|
2014-06-19 17:11:04 +02:00
|
|
|
self.titleCache.push(rss.title);
|
2014-06-19 16:51:54 +02:00
|
|
|
}
|
2014-05-24 02:46:01 +02:00
|
|
|
}
|
2014-05-23 03:24:45 +02:00
|
|
|
}
|
|
|
|
});
|
2014-05-25 00:22:34 +02:00
|
|
|
feedparser.on('end', function() {
|
|
|
|
feed.lastPosted = feed.newTime;
|
|
|
|
dbot.db.feeds[fid] = feed;
|
|
|
|
});
|
2014-05-23 03:24:45 +02:00
|
|
|
}.bind(this),
|
2014-06-19 16:41:57 +02:00
|
|
|
|
2014-05-24 17:16:56 +02:00
|
|
|
'checkFeeds': function() {
|
|
|
|
console.log("Checking feeds...");
|
2015-04-04 00:28:36 +02:00
|
|
|
if(dbot.db.feeds == null) {
|
|
|
|
console.log("No active feeds...");
|
|
|
|
return;
|
|
|
|
}
|
2014-05-24 01:12:47 +02:00
|
|
|
for(var i=0;i<dbot.db.feeds.length;++i) {
|
2014-05-24 17:16:56 +02:00
|
|
|
this.internalAPI.makeRequest(i,dbot.db.feeds[i]);
|
2014-05-24 01:12:47 +02:00
|
|
|
}
|
2014-05-24 17:16:56 +02:00
|
|
|
}.bind(this),
|
2014-06-19 16:41:57 +02:00
|
|
|
|
2014-05-24 17:16:56 +02:00
|
|
|
'reloadFeeds': function() {
|
2014-06-19 16:41:57 +02:00
|
|
|
return setInterval(this.internalAPI.checkFeeds, this.pollInterval);
|
2014-05-23 03:24:45 +02:00
|
|
|
}.bind(this)
|
|
|
|
};
|
2014-06-19 16:41:57 +02:00
|
|
|
|
2014-05-23 03:24:45 +02:00
|
|
|
this.commands = {
|
|
|
|
'~addrssfeed': function(event) {
|
|
|
|
if(event.params.length < 3) {
|
|
|
|
event.reply("GIMME TWO PARAMETERS DUDE");
|
|
|
|
return;
|
|
|
|
}
|
2014-05-24 02:46:01 +02:00
|
|
|
var now = Date.now();
|
2015-04-04 00:28:36 +02:00
|
|
|
if(dbot.db.feeds == null)
|
|
|
|
dbot.db.feeds = [];
|
2015-04-04 01:24:50 +02:00
|
|
|
dbot.db.feeds.push({server:event.server, channel:event.channel.name, name:event.params[1], url:event.params[2], lastPosted: 0, newTime: 0});
|
2014-05-23 03:24:45 +02:00
|
|
|
event.reply("Adding RSS feed named "+event.params[1]+" with URL "+event.params[2]);
|
|
|
|
},
|
2014-06-19 16:41:57 +02:00
|
|
|
|
2014-05-24 01:12:47 +02:00
|
|
|
'~rsstest': function(event) {
|
2014-06-19 16:41:57 +02:00
|
|
|
event.reply("I posted RSS last @ "+this.lastPosted);
|
2015-04-04 00:28:36 +02:00
|
|
|
event.reply("Checking feeds manually...");
|
|
|
|
this.internalAPI.checkFeeds();
|
|
|
|
event.reply("Call got through!");
|
2014-05-23 03:24:45 +02:00
|
|
|
},
|
2014-06-19 16:41:57 +02:00
|
|
|
|
2014-05-23 03:24:45 +02:00
|
|
|
'~delrssfeed': function(event) {
|
|
|
|
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]) {
|
2014-06-19 16:41:57 +02:00
|
|
|
dbot.db.feeds.splice(i, 1);
|
|
|
|
event.reply("Removed feed "+event.params[1]+" you were looking for...");
|
2014-05-24 20:15:12 +02:00
|
|
|
break;
|
2014-05-23 03:24:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2014-06-19 16:41:57 +02:00
|
|
|
|
2014-05-23 03:24:45 +02:00
|
|
|
this.onLoad = function() {
|
2014-05-24 17:16:56 +02:00
|
|
|
this.interval = this.internalAPI.reloadFeeds();
|
2014-05-24 01:12:47 +02:00
|
|
|
};
|
2014-06-19 16:41:57 +02:00
|
|
|
|
2014-05-24 01:12:47 +02:00
|
|
|
this.onDestroy = function() {
|
2014-05-24 17:16:56 +02:00
|
|
|
clearInterval(this.interval);
|
2014-05-24 01:12:47 +02:00
|
|
|
};
|
2014-05-23 03:24:45 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.fetch = function(dbot) {
|
|
|
|
return new rss(dbot);
|
|
|
|
};
|