2013-04-13 18:54:48 +02:00
|
|
|
/**
|
|
|
|
* Module Name: reddit
|
|
|
|
* Description: Various reddit functionality
|
|
|
|
*/
|
|
|
|
var _ = require('underscore')._,
|
|
|
|
request = require('request');
|
|
|
|
|
|
|
|
var reddit = function(dbot) {
|
|
|
|
this.ApiRoot = 'http://reddit.com/';
|
2013-04-13 21:20:02 +02:00
|
|
|
this.UserAgent = 'dbot by u/realitone';
|
2013-12-19 21:02:50 +01:00
|
|
|
this.ints = [];
|
2013-04-13 18:54:48 +02:00
|
|
|
|
2013-12-19 19:48:49 +01:00
|
|
|
this.internalAPI = {
|
|
|
|
'getChannelFeeds': function(server, cName, callback) {
|
2015-01-09 22:25:33 +01:00
|
|
|
this.db.read('reddit_feeds', cName + '.' + server, function(err, cFeeds) {
|
|
|
|
if(err || !cFeeds) {
|
|
|
|
callback(null, {
|
|
|
|
'id': cName + '.' + server,
|
|
|
|
'server': server,
|
|
|
|
'channel': cName,
|
|
|
|
'feeds': {}
|
2013-12-19 19:48:49 +01:00
|
|
|
});
|
|
|
|
} else {
|
2015-01-09 22:25:33 +01:00
|
|
|
callback(null, cFeeds);
|
2013-12-19 19:48:49 +01:00
|
|
|
}
|
2015-01-09 22:25:33 +01:00
|
|
|
});
|
2013-12-19 19:48:49 +01:00
|
|
|
}.bind(this),
|
|
|
|
|
|
|
|
'updateChannelFeeds': function(cFeeds, callback) {
|
|
|
|
this.db.update('reddit_feeds', cFeeds.id, cFeeds, callback);
|
|
|
|
}.bind(this),
|
|
|
|
|
|
|
|
'reloadChannelFeeds': function() {
|
|
|
|
var channels = [],
|
|
|
|
checkTimes = [];
|
2016-04-14 00:15:03 +02:00
|
|
|
console.log('getting feeds');
|
2013-12-19 19:48:49 +01:00
|
|
|
|
2013-12-19 21:02:50 +01:00
|
|
|
for(i=0;i<this.ints.length;i++) {
|
|
|
|
clearInterval(this.ints[i]);
|
|
|
|
}
|
2013-12-21 23:58:36 +01:00
|
|
|
this.ints = [];
|
2013-12-19 21:02:50 +01:00
|
|
|
|
2014-01-25 15:35:53 +01:00
|
|
|
this.db.scan('reddit_feeds', function(channel) {
|
2013-12-19 19:48:49 +01:00
|
|
|
if(channel) {
|
|
|
|
channels.push(channel);
|
|
|
|
}
|
|
|
|
}, function() {
|
|
|
|
_.each(channels, function(channel) {
|
|
|
|
checkTimes[channel.id] = {};
|
|
|
|
_.each(channel.feeds, function(feed) {
|
|
|
|
checkTimes[channel.id][feed.subreddit] = Date.now();
|
2013-12-21 23:58:36 +01:00
|
|
|
var intervalId = setInterval(function() {
|
2013-12-19 19:48:49 +01:00
|
|
|
this.api.getNewPosts(feed.subreddit, checkTimes[channel.id][feed.subreddit], function(err, posts) {
|
|
|
|
if(!err && posts.length > 0) {
|
|
|
|
_.each(posts, function(post) {
|
2016-04-14 00:25:51 +02:00
|
|
|
if(_.has(this.config.colours, post.subreddit)) {
|
|
|
|
post.subreddit = this.config.colours[post.subreddit] + post.subreddit + "\u0003";
|
2016-04-14 00:09:20 +02:00
|
|
|
}
|
2014-07-22 13:28:54 +02:00
|
|
|
dbot.say(channel.server, channel.channel, dbot.t('about_new_post', {
|
2014-07-12 20:15:56 +02:00
|
|
|
'title': _.unescape(post.title.trim()),
|
2013-12-19 19:48:49 +01:00
|
|
|
'poster': post.author,
|
|
|
|
'subreddit': post.subreddit,
|
|
|
|
'comments': post.num_comments,
|
|
|
|
'score': post.score,
|
|
|
|
'up': post.ups,
|
|
|
|
'down': post.downs,
|
|
|
|
'url': this.ApiRoot + post.id
|
|
|
|
}));
|
|
|
|
}, this);
|
|
|
|
checkTimes[channel.id][feed.subreddit] = Date.now();
|
|
|
|
}
|
|
|
|
}.bind(this));
|
2013-12-21 23:58:36 +01:00
|
|
|
}.bind(this), feed.interval);
|
|
|
|
this.ints.push(intervalId);
|
2013-12-19 19:48:49 +01:00
|
|
|
}.bind(this));
|
|
|
|
}.bind(this));
|
2014-01-25 15:35:53 +01:00
|
|
|
}.bind(this));
|
2013-12-19 19:48:49 +01:00
|
|
|
}.bind(this)
|
|
|
|
};
|
|
|
|
|
2013-04-13 18:54:48 +02:00
|
|
|
this.api = {
|
|
|
|
'getSubredditInfo': function(name, callback) {
|
|
|
|
request.get({
|
|
|
|
'url': this.ApiRoot + 'r/' + name + '/about.json',
|
|
|
|
'json': true,
|
|
|
|
'headers': {
|
2013-04-13 21:20:02 +02:00
|
|
|
'User-Agent': this.UserAgent
|
2013-04-13 18:54:48 +02:00
|
|
|
}
|
|
|
|
}, function(err, response, body) {
|
2013-04-13 21:20:02 +02:00
|
|
|
var data = null;
|
|
|
|
if(_.has(body, 'data')) data = body.data;
|
|
|
|
callback(data);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
'getPostInfo': function(name, callback) {
|
|
|
|
request.get({
|
|
|
|
'url': this.ApiRoot + 'comments/' + name + '.json',
|
|
|
|
'json': true,
|
|
|
|
'headers': {
|
|
|
|
'User-Agent': this.UserAgent
|
|
|
|
}
|
|
|
|
}, function(err, response, body) {
|
|
|
|
if(body[0] && _.has(body[0], 'data')) {
|
|
|
|
callback(body[0].data.children[0].data);
|
|
|
|
}
|
2013-04-13 18:54:48 +02:00
|
|
|
});
|
2013-04-13 21:58:05 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
'getCommentInfo': function(post, name, callback) {
|
|
|
|
request.get({
|
|
|
|
'url': this.ApiRoot + 'comments/' + post + '.json',
|
|
|
|
'qs': {
|
|
|
|
'comment': name
|
|
|
|
},
|
|
|
|
'json': true,
|
|
|
|
'headers': {
|
|
|
|
'User-Agent': this.UserAgent
|
|
|
|
}
|
|
|
|
}, function(err, response, body) {
|
|
|
|
if(body[1] && _.has(body[1], 'data')) {
|
|
|
|
callback(body[1].data.children[0].data);
|
|
|
|
}
|
|
|
|
});
|
2013-12-19 19:48:49 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
'getNewPosts': function(sr, last, callback) {
|
|
|
|
request.get({
|
|
|
|
'url': this.ApiRoot + 'r/' + sr + '/new.json',
|
|
|
|
'json': true,
|
|
|
|
'headers': {
|
|
|
|
'User-Agent': this.UserAgent
|
|
|
|
}
|
|
|
|
}, function(err, response, body) {
|
|
|
|
if(!err && body && body.kind === 'Listing') {
|
|
|
|
var posts = _.pluck(body.data.children, 'data');
|
|
|
|
newPosts = _.filter(posts, function(post) {
|
|
|
|
return post.created_utc > (last / 1000);
|
|
|
|
});
|
|
|
|
callback(null, newPosts);
|
|
|
|
} else {
|
|
|
|
callback(true, null);
|
|
|
|
}
|
|
|
|
});
|
2014-06-26 21:40:54 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
'getTopPosts': function(sr, callback) {
|
|
|
|
request.get({
|
|
|
|
'url': this.ApiRoot + 'r/' + sr + '/top.json',
|
|
|
|
'json': true,
|
|
|
|
'headers': {
|
|
|
|
'User-Agent': this.UserAgent
|
|
|
|
}
|
|
|
|
}, function(err, response, body) {
|
|
|
|
if(!err && body && body.kind === 'Listing') {
|
|
|
|
var posts = _.pluck(body.data.children, 'data');
|
|
|
|
callback(null, posts);
|
|
|
|
} else {
|
|
|
|
callback(true, null);
|
|
|
|
}
|
|
|
|
});
|
2013-04-13 18:54:48 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-12-19 19:48:49 +01:00
|
|
|
this.commands = {
|
2014-06-26 21:40:54 +02:00
|
|
|
'~question': function(event) {
|
|
|
|
this.api.getTopPosts('askreddit', function(err, posts) {
|
|
|
|
if(!err) {
|
2014-06-26 21:50:45 +02:00
|
|
|
var qPost = posts[_.random(0, posts.length - 1)];
|
2014-06-26 21:40:54 +02:00
|
|
|
event.reply('Question: ' + qPost.title.trim());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2014-07-22 13:33:36 +02:00
|
|
|
'~squestion': function(event) {
|
|
|
|
this.api.getNewPosts('askscience', 0, function(err, posts) {
|
|
|
|
if(!err) {
|
|
|
|
var qPost = posts[_.random(0, posts.length - 1)];
|
|
|
|
event.reply('Question: ' + qPost.title.trim());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-03-28 13:55:06 +01:00
|
|
|
'~r': function(event) {
|
|
|
|
var sr = event.params[1].trim();
|
|
|
|
this.api.getNewPosts(sr, 0, function(err, posts) {
|
|
|
|
if(!err) {
|
|
|
|
var qPost = posts[_.random(0, posts.length -1)];
|
|
|
|
event.reply(dbot.t('about_post', {
|
|
|
|
'title': _.unescape(qPost.title.trim()),
|
|
|
|
'poster': qPost.author,
|
|
|
|
'subreddit': qPost.subreddit,
|
|
|
|
'comments': qPost.num_comments,
|
|
|
|
'score': qPost.score,
|
|
|
|
'up': qPost.ups,
|
|
|
|
'down': qPost.downs,
|
|
|
|
'url': qPost.url
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
event.reply('Could not find that subreddit');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-12-19 19:48:49 +01:00
|
|
|
'~addredditfeed': function(event) {
|
|
|
|
var channel = event.input[1],
|
|
|
|
subreddit = event.input[2].replace('r/', ''),
|
|
|
|
interval = event.input[3] * 60000;
|
|
|
|
|
|
|
|
this.internalAPI.getChannelFeeds(event.server, channel, function(err, channel) {
|
|
|
|
if(!err) {
|
|
|
|
if(!_.has(channel.feeds, subreddit)) {
|
|
|
|
channel.feeds[subreddit] = {
|
|
|
|
'subreddit': subreddit,
|
|
|
|
'interval': interval
|
|
|
|
};
|
|
|
|
this.internalAPI.updateChannelFeeds(channel, function() {
|
|
|
|
this.internalAPI.reloadChannelFeeds();
|
|
|
|
}.bind(this));
|
|
|
|
event.reply(dbot.t('added_channel_feed', {
|
|
|
|
'subreddit': subreddit,
|
|
|
|
'interval': interval / 60000,
|
|
|
|
'channel': channel.channel
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
event.reply(dbot.t('feed_already_watched'));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(err === 'NoSuchChannel') {
|
|
|
|
event.reply(dbot.t('no_such_channel'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
|
|
|
'~rmredditfeed': function(event) {
|
|
|
|
var channel = event.input[1],
|
|
|
|
subreddit = event.input[2].replace('r/', '');
|
|
|
|
|
|
|
|
this.internalAPI.getChannelFeeds(event.server, channel, function(err, channel) {
|
|
|
|
if(!err && channel && _.has(channel.feeds, subreddit)) {
|
|
|
|
delete channel.feeds[subreddit];
|
|
|
|
this.internalAPI.updateChannelFeeds(channel, function() {
|
|
|
|
this.internalAPI.reloadChannelFeeds();
|
|
|
|
}.bind(this));
|
|
|
|
|
|
|
|
event.reply(dbot.t('removed_channel_feed', {
|
|
|
|
'subreddit': subreddit,
|
|
|
|
'channel': channel.channel
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
event.reply(dbot.t('no_such_feed'));
|
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
}
|
|
|
|
};
|
2013-12-29 19:38:24 +01:00
|
|
|
this.commands['~addredditfeed'].regex = [/^addredditfeed ([^ ]+) ([^ ]+) ([^ ]+)$/, 4];
|
|
|
|
this.commands['~rmredditfeed'].regex = [/^rmredditfeed ([^ ]+) ([^ ]+)$/, 3];
|
2013-12-19 19:51:49 +01:00
|
|
|
this.commands['~addredditfeed'].access = 'moderator';
|
|
|
|
this.commands['~rmredditfeed'].access = 'moderator';
|
2013-12-19 19:48:49 +01:00
|
|
|
|
2013-12-19 21:28:27 +01:00
|
|
|
this.onDestroy = function() {
|
|
|
|
for(i=0;i<this.ints.length;i++) {
|
|
|
|
clearInterval(this.ints[i]);
|
|
|
|
}
|
|
|
|
}.bind(this);
|
|
|
|
|
2013-04-13 18:54:48 +02:00
|
|
|
this.onLoad = function() {
|
2013-12-19 19:48:49 +01:00
|
|
|
this.internalAPI.reloadChannelFeeds();
|
|
|
|
|
2013-07-25 23:56:44 +02:00
|
|
|
var rHandler = function(matches, name, callback) {
|
2013-04-13 21:20:02 +02:00
|
|
|
if(matches[6]) { // It's a comment
|
2013-04-13 21:58:05 +02:00
|
|
|
this.api.getCommentInfo(matches[4], matches[6], function(info) {
|
|
|
|
if(info) {
|
|
|
|
var infoString = dbot.t('about_comment', {
|
|
|
|
'poster': info.author,
|
|
|
|
'subreddit': info.subreddit,
|
|
|
|
'comments': info.num_comments,
|
|
|
|
'score': info.ups - info.downs,
|
|
|
|
'up': info.ups,
|
|
|
|
'down': info.downs
|
|
|
|
});
|
2013-04-14 03:35:04 +02:00
|
|
|
if(info.over_18) infoString += " " + dbot.t("nsfw");
|
2013-07-25 23:56:44 +02:00
|
|
|
callback(infoString);
|
2013-04-13 21:58:05 +02:00
|
|
|
}
|
|
|
|
});
|
2013-04-13 21:20:02 +02:00
|
|
|
} else if(matches[4]) { // It's a post
|
|
|
|
this.api.getPostInfo(matches[4], function(info) {
|
|
|
|
if(info) {
|
|
|
|
var infoString = dbot.t('about_post', {
|
2014-07-12 20:19:59 +02:00
|
|
|
'title': _.unescape(info.title.trim()),
|
2013-04-13 21:20:02 +02:00
|
|
|
'poster': info.author,
|
|
|
|
'subreddit': info.subreddit,
|
|
|
|
'comments': info.num_comments,
|
|
|
|
'score': info.score,
|
|
|
|
'up': info.ups,
|
2013-04-13 22:04:06 +02:00
|
|
|
'down': info.downs,
|
|
|
|
'url': this.ApiRoot + matches[4]
|
2013-04-13 21:20:02 +02:00
|
|
|
});
|
2013-04-14 03:35:04 +02:00
|
|
|
if(info.over_18) infoString += " " + dbot.t("nsfw");
|
2013-07-25 23:56:44 +02:00
|
|
|
callback(infoString);
|
2013-04-13 21:20:02 +02:00
|
|
|
}
|
2013-04-13 22:04:06 +02:00
|
|
|
}.bind(this));
|
2013-04-13 21:20:02 +02:00
|
|
|
} else if(matches[2]) { // It's a subreddit
|
|
|
|
this.api.getSubredditInfo(matches[2], function(info) {
|
|
|
|
if(info) {
|
|
|
|
var infoString = dbot.t('about_subreddit', {
|
|
|
|
'display_name': info.display_name,
|
|
|
|
'subscribers': info.subscribers,
|
|
|
|
'active': info.accounts_active
|
|
|
|
});
|
2013-04-14 03:35:04 +02:00
|
|
|
if(info.over18) infoString += dbot.t("nsfw");
|
2013-07-25 23:56:44 +02:00
|
|
|
callback(infoString);
|
2013-04-13 21:20:02 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2013-04-13 18:54:48 +02:00
|
|
|
}.bind(this);
|
2013-04-13 21:20:02 +02:00
|
|
|
|
|
|
|
dbot.api.link.addHandler(this.name, // I'm so sorry, Jesus.
|
2013-12-22 00:24:18 +01:00
|
|
|
/https?:\/\/([a-z]+\.)?reddit\.com\/r\/([a-zA-Z0-9]+)(\/comments\/([a-zA-Z0-9]+)?\/([a-zA-Z0-9_]+)\/([a-zA-Z0-9_]+)?)?/,
|
2013-04-13 21:20:02 +02:00
|
|
|
rHandler);
|
2013-11-20 11:25:05 +01:00
|
|
|
dbot.api.link.addHandler(this.name + 'short',
|
2013-12-22 00:24:18 +01:00
|
|
|
/https?:\/\/([a-z]\.)?redd.it\/([a-zA-Z0-9]+)/,
|
2013-11-20 11:25:05 +01:00
|
|
|
function(match, name, callback) {
|
|
|
|
this.api.getPostInfo(match[2], function(info) {
|
|
|
|
if(info) {
|
|
|
|
var infoString = dbot.t('about_post', {
|
2014-07-12 20:19:59 +02:00
|
|
|
'title': _.unescape(info.title.trim()),
|
2013-11-20 11:25:05 +01:00
|
|
|
'poster': info.author,
|
|
|
|
'subreddit': info.subreddit,
|
|
|
|
'comments': info.num_comments,
|
|
|
|
'score': info.score,
|
|
|
|
'up': info.ups,
|
|
|
|
'down': info.downs,
|
|
|
|
'url': this.ApiRoot + match[2]
|
|
|
|
});
|
|
|
|
if(info.over_18) infoString += " " + dbot.t("nsfw");
|
|
|
|
callback(infoString);
|
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
}.bind(this));
|
2013-04-13 18:54:48 +02:00
|
|
|
}.bind(this);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.fetch = function(dbot) {
|
|
|
|
return new reddit(dbot);
|
|
|
|
}
|