2013-04-10 21:43:11 +02:00
|
|
|
/**
|
|
|
|
* Module Name: imgur
|
|
|
|
* Description: Various imgur functionality
|
|
|
|
*/
|
|
|
|
|
|
|
|
var _ = require('underscore')._,
|
|
|
|
request = require('request');
|
|
|
|
|
|
|
|
var imgur = function(dbot) {
|
|
|
|
this.api = {
|
|
|
|
'getRandomImage': function(callback) {
|
2013-04-10 22:14:54 +02:00
|
|
|
var random = function(len) {
|
|
|
|
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
|
|
|
|
return len ? chars.charAt(~~(Math.random()*chars.length)) + random(len-1) : "";
|
|
|
|
};
|
|
|
|
|
2013-04-10 21:43:11 +02:00
|
|
|
var testUrl = 'http://i.imgur.com/' +
|
2013-04-10 22:14:54 +02:00
|
|
|
random(5) +
|
2013-04-10 22:08:04 +02:00
|
|
|
'.png';
|
2013-04-10 21:43:11 +02:00
|
|
|
var image = request(testUrl, function(error, response, body) {
|
2013-04-10 22:05:57 +02:00
|
|
|
// 492 is body.length of a removed image
|
|
|
|
if(!error && response.statusCode == 200 && body.length != 492) {
|
2013-04-10 21:43:11 +02:00
|
|
|
callback(testUrl);
|
|
|
|
} else {
|
|
|
|
this.api.getRandomImage(callback);
|
|
|
|
}
|
|
|
|
}.bind(this));
|
2013-04-13 01:27:31 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
'getImageInfo': function(slug, callback) {
|
|
|
|
request.get({
|
|
|
|
'url': 'https://api.imgur.com/3/image/' + slug + '.json',
|
|
|
|
'headers': {
|
|
|
|
'Authorization': 'Client-ID 86fd3a8da348b65'
|
|
|
|
}
|
|
|
|
}, function(err, response, body) {
|
|
|
|
callback(body);
|
|
|
|
});
|
2013-04-10 21:43:11 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.commands = {
|
2013-04-10 22:36:26 +02:00
|
|
|
'~ri': function(event) {
|
2013-04-10 21:43:11 +02:00
|
|
|
this.api.getRandomImage(function(link) {
|
2013-04-11 21:33:33 +02:00
|
|
|
event.reply(event.user + ': (' + dbot.t('nsfw') + ') ' + link);
|
2013-04-10 21:43:11 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2013-04-13 01:27:31 +02:00
|
|
|
|
|
|
|
this.onLoad = function() {
|
|
|
|
}.bind(this);
|
2013-04-10 21:43:11 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.fetch = function(dbot) {
|
|
|
|
return new imgur(dbot);
|
|
|
|
}
|