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-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-04-13 18:54:48 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.onLoad = function() {
|
2013-04-13 21:20:02 +02:00
|
|
|
var rHandler = function(event, matches, name) {
|
|
|
|
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
|
|
|
|
});
|
|
|
|
if(info.over_18) infoString += ' [NSFW]';
|
|
|
|
event.reply(infoString);
|
|
|
|
}
|
|
|
|
});
|
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', {
|
|
|
|
'poster': info.author,
|
|
|
|
'subreddit': info.subreddit,
|
|
|
|
'comments': info.num_comments,
|
|
|
|
'score': info.score,
|
|
|
|
'up': info.ups,
|
|
|
|
'down': info.downs
|
|
|
|
});
|
2013-04-13 21:58:05 +02:00
|
|
|
if(info.over_18) infoString += ' [NSFW]';
|
2013-04-13 21:20:02 +02:00
|
|
|
event.reply(infoString);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} 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
|
|
|
|
});
|
|
|
|
if(info.over18) infoString += ' [NSFW]';
|
|
|
|
event.reply(infoString);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
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.
|
|
|
|
/https?:\/\/(www\.)?reddit\.com\/r\/([a-zA-Z0-9]+)(\/comments\/([a-zA-Z0-9]+)?\/([a-zA-Z0-9_]+)\/([a-zA-Z0-9_]+)?)?/,
|
|
|
|
rHandler);
|
2013-04-13 18:54:48 +02:00
|
|
|
}.bind(this);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.fetch = function(dbot) {
|
|
|
|
return new reddit(dbot);
|
|
|
|
}
|