Add ability to get post information and write regex (unimplemented handler for comments currently) which handles all three Things [#293]

This commit is contained in:
reality 2013-04-13 19:20:02 +00:00
parent 49beccf849
commit 1dada50559
2 changed files with 57 additions and 16 deletions

View File

@ -7,6 +7,7 @@ var _ = require('underscore')._,
var reddit = function(dbot) { var reddit = function(dbot) {
this.ApiRoot = 'http://reddit.com/'; this.ApiRoot = 'http://reddit.com/';
this.UserAgent = 'dbot by u/realitone';
this.api = { this.api = {
'getSubredditInfo': function(name, callback) { 'getSubredditInfo': function(name, callback) {
@ -14,30 +15,67 @@ var reddit = function(dbot) {
'url': this.ApiRoot + 'r/' + name + '/about.json', 'url': this.ApiRoot + 'r/' + name + '/about.json',
'json': true, 'json': true,
'headers': { 'headers': {
'User-Agent': 'dbot by u/realitone' 'User-Agent': this.UserAgent
} }
}, function(err, response, body) { }, function(err, response, body) {
callback(body); 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);
}
}); });
} }
}; };
this.onLoad = function() { this.onLoad = function() {
var srHandler = function(event, matches, name) { var rHandler = function(event, matches, name) {
this.api.getSubredditInfo(matches[2], function(info) { if(matches[6]) { // It's a comment
if(info.data) {
info = info.data; } else if(matches[4]) { // It's a post
var infoString = dbot.t('about_subreddit', { this.api.getPostInfo(matches[4], function(info) {
'display_name': info.display_name, if(info) {
'subscribers': info.subscribers, var infoString = dbot.t('about_post', {
'active': info.accounts_active 'poster': info.author,
}); 'subreddit': info.subreddit,
if(info.over18) infoString += ' [NSFW]'; 'comments': info.num_comments,
event.reply(infoString); 'score': info.score,
} 'up': info.ups,
}); 'down': info.downs
});
if(info.over18) infoString += ' [NSFW]';
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);
}
});
}
}.bind(this); }.bind(this);
dbot.api.link.addHandler(this.name, /https?:\/\/(www\.)?reddit\.com\/r\/([a-zA-Z0-9]+)/, srHandler);
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);
}.bind(this); }.bind(this);
}; };

View File

@ -1,5 +1,8 @@
{ {
"about_subreddit": { "about_subreddit": {
"en": "[{display_name} has {subscribers} subscribers ({active} active)]" "en": "[{display_name} has {subscribers} subscribers ({active} active)]"
},
"about_post": {
"en": "[Post by {poster} in {subreddit}. Comments: {comments}. Score: {score} ({up}|{down})]"
} }
} }