2013-04-10 21:43:11 +02:00
|
|
|
/**
|
|
|
|
* Module Name: imgur
|
|
|
|
* Description: Various imgur functionality
|
|
|
|
*/
|
|
|
|
|
|
|
|
var _ = require('underscore')._,
|
2013-05-01 11:42:42 +02:00
|
|
|
request = require('request'),
|
|
|
|
crypto = require('crypto');
|
2013-04-10 21:43:11 +02:00
|
|
|
|
|
|
|
var imgur = function(dbot) {
|
2013-04-13 02:42:20 +02:00
|
|
|
this.internalAPI = {
|
|
|
|
'infoString': function(imgData) {
|
2013-04-16 18:42:57 +02:00
|
|
|
info = '';
|
2013-05-21 18:05:20 +02:00
|
|
|
if(imgData && _.has(imgData, 'data') && !_.isUndefined(imgData.data.type)) {
|
2013-04-13 02:42:20 +02:00
|
|
|
imgData = imgData.data;
|
|
|
|
if(imgData.title) {
|
2013-04-16 18:42:57 +02:00
|
|
|
info += imgData.title + ' - ';
|
2013-04-13 02:42:20 +02:00
|
|
|
}
|
2013-04-13 20:37:47 +02:00
|
|
|
if(imgData.type) {
|
|
|
|
if(imgData.animated) {
|
|
|
|
info += 'an animated ' + imgData.type.split('/')[1] + ' with ';
|
|
|
|
} else {
|
2013-04-14 00:40:35 +02:00
|
|
|
info += 'a ' + imgData.type.split('/')[1] + ' with ';
|
2013-04-13 20:37:47 +02:00
|
|
|
}
|
2013-04-13 02:42:20 +02:00
|
|
|
} else {
|
2013-04-13 20:37:47 +02:00
|
|
|
info += 'an image with ';
|
2013-04-13 02:42:20 +02:00
|
|
|
}
|
2013-04-13 17:33:43 +02:00
|
|
|
info += imgData.views + ' views (';
|
2013-04-16 18:42:57 +02:00
|
|
|
info += imgData.width + 'x' + imgData.height + ')';
|
2013-04-13 02:42:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return info;
|
2013-04-16 19:58:37 +02:00
|
|
|
}.bind(this)
|
2013-04-13 02:42:20 +02:00
|
|
|
};
|
|
|
|
|
2013-04-10 21:43:11 +02:00
|
|
|
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-13 01:44:19 +02:00
|
|
|
|
|
|
|
var ext = [ 'gif', 'png', 'jpg' ];
|
|
|
|
var testSlug = random(5);
|
2013-04-10 21:43:11 +02:00
|
|
|
var testUrl = 'http://i.imgur.com/' +
|
2013-04-13 01:44:19 +02:00
|
|
|
testSlug +
|
|
|
|
'.' + ext[_.random(0, ext.length - 1)];
|
2013-04-22 16:38:03 +02:00
|
|
|
dbot.db.imgur.totalHttpRequests += 1;
|
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-22 16:38:03 +02:00
|
|
|
dbot.db.imgur.totalImages += 1;
|
2013-05-01 11:42:42 +02:00
|
|
|
var hash = crypto.createHash('md5').update(body).digest("hex");
|
2013-05-20 17:18:19 +02:00
|
|
|
if(_.has(dbot.modules, 'quotes')){
|
|
|
|
// autoadd: {"abcdef": "facebookman"}
|
|
|
|
if(_.has(dbot.config.imgur.autoadd,hash)){
|
|
|
|
var category = dbot.config.imgur.autoadd[hash];
|
|
|
|
if (_.contains(category, testUrl)){
|
|
|
|
// there's probably less than 62^5 chance of this happening
|
|
|
|
} else {
|
2013-05-20 17:40:13 +02:00
|
|
|
dbot.api.quotes.addQuote(category, testUrl,
|
|
|
|
dbot.config.name, function() { });
|
2013-05-20 17:18:19 +02:00
|
|
|
}
|
2013-05-17 19:04:53 +02:00
|
|
|
}
|
|
|
|
}
|
2013-05-01 11:42:42 +02:00
|
|
|
callback(testUrl, testSlug,hash);
|
2013-04-10 21:43:11 +02:00
|
|
|
} else {
|
|
|
|
this.api.getRandomImage(callback);
|
|
|
|
}
|
|
|
|
}.bind(this));
|
2013-04-13 01:27:31 +02:00
|
|
|
},
|
|
|
|
|
2013-04-16 18:42:57 +02:00
|
|
|
'getImageInfoString': function(slug, callback) {
|
|
|
|
this.api.getImageInfo(slug, function(imgData) {
|
|
|
|
callback(this.internalAPI.infoString(imgData));
|
|
|
|
}.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',
|
2013-04-13 01:44:19 +02:00
|
|
|
'json': true,
|
2013-04-13 01:27:31 +02:00
|
|
|
'headers': {
|
2013-04-16 23:03:50 +02:00
|
|
|
'Authorization': 'Client-ID ' + dbot.config.imgur.apikey
|
2013-04-13 01:27:31 +02:00
|
|
|
}
|
|
|
|
}, function(err, response, body) {
|
2013-04-22 16:38:03 +02:00
|
|
|
dbot.db.imgur.totalApiRequests += 1;
|
2013-04-13 01:27:31 +02:00
|
|
|
callback(body);
|
2013-04-16 19:58:37 +02:00
|
|
|
}.bind(this));
|
2013-04-10 21:43:11 +02:00
|
|
|
}
|
|
|
|
};
|
2013-04-14 19:21:24 +02:00
|
|
|
this.api['getRandomImage'].external = true;
|
|
|
|
this.api['getRandomImage'].extMap = [ 'callback' ];
|
2013-04-16 18:42:57 +02:00
|
|
|
this.api['getImageInfoString'].external = true;
|
|
|
|
this.api['getImageInfoString'].extMap = [ 'slug', 'callback' ];
|
2013-04-10 21:43:11 +02:00
|
|
|
|
|
|
|
this.commands = {
|
2013-04-10 22:36:26 +02:00
|
|
|
'~ri': function(event) {
|
2013-04-13 01:44:19 +02:00
|
|
|
this.api.getRandomImage(function(link, slug) {
|
|
|
|
this.api.getImageInfo(slug, function(imgData) {
|
2013-04-13 02:42:20 +02:00
|
|
|
var info = this.internalAPI.infoString(imgData);
|
2013-04-16 18:42:57 +02:00
|
|
|
event.reply(event.user + ': ' + link + ' [' + info + ']');
|
2013-04-13 02:42:20 +02:00
|
|
|
}.bind(this));
|
2013-04-13 01:44:19 +02:00
|
|
|
}.bind(this));
|
2013-04-10 21:43:11 +02:00
|
|
|
}
|
|
|
|
}
|
2013-04-13 01:27:31 +02:00
|
|
|
|
|
|
|
this.onLoad = function() {
|
2013-04-13 02:42:20 +02:00
|
|
|
var imgurHandler = function(event, matches, name) {
|
2013-04-13 17:41:13 +02:00
|
|
|
if(matches[1]) {
|
2013-04-13 02:42:20 +02:00
|
|
|
this.api.getImageInfo(matches[1], function(imgData) {
|
|
|
|
var info = this.internalAPI.infoString(imgData);
|
2013-04-16 18:42:57 +02:00
|
|
|
if(info) event.reply('[' + info + ']');
|
2013-04-13 02:42:20 +02:00
|
|
|
}.bind(this));
|
|
|
|
}
|
|
|
|
}.bind(this);
|
2013-04-13 17:41:13 +02:00
|
|
|
dbot.api.link.addHandler(this.name, /https?:\/\/i\.imgur\.com\/([a-zA-Z0-9]+)\.([jpg|png|gif])/, imgurHandler);
|
|
|
|
dbot.api.link.addHandler(this.name, /https?:\/\/imgur\.com\/([a-zA-Z0-9]+)/, imgurHandler);
|
2013-04-16 19:58:37 +02:00
|
|
|
if(!_.has(dbot.db.imgur, 'totalHttpRequests')) dbot.db.imgur.totalHttpRequests = 0;
|
|
|
|
if(!_.has(dbot.db.imgur, 'totalApiRequests')) dbot.db.imgur.totalApiRequests = 0;
|
|
|
|
if(!_.has(dbot.db.imgur, 'totalImages')) dbot.db.imgur.totalImages = 0;
|
2013-04-13 01:27:31 +02:00
|
|
|
}.bind(this);
|
2013-04-10 21:43:11 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.fetch = function(dbot) {
|
|
|
|
return new imgur(dbot);
|
|
|
|
}
|