mirror of
https://github.com/reality/dbot.git
synced 2024-12-25 04:02:39 +01:00
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:
parent
49beccf849
commit
1dada50559
@ -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,19 +15,52 @@ 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) {
|
||||||
|
if(matches[6]) { // It's a comment
|
||||||
|
|
||||||
|
} 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
|
||||||
|
});
|
||||||
|
if(info.over18) infoString += ' [NSFW]';
|
||||||
|
event.reply(infoString);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else if(matches[2]) { // It's a subreddit
|
||||||
this.api.getSubredditInfo(matches[2], function(info) {
|
this.api.getSubredditInfo(matches[2], function(info) {
|
||||||
if(info.data) {
|
if(info) {
|
||||||
info = info.data;
|
|
||||||
var infoString = dbot.t('about_subreddit', {
|
var infoString = dbot.t('about_subreddit', {
|
||||||
'display_name': info.display_name,
|
'display_name': info.display_name,
|
||||||
'subscribers': info.subscribers,
|
'subscribers': info.subscribers,
|
||||||
@ -36,8 +70,12 @@ var reddit = function(dbot) {
|
|||||||
event.reply(infoString);
|
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);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -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})]"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user