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) {
|
2013-04-13 02:42:20 +02:00
|
|
|
this.internalAPI = {
|
|
|
|
'infoString': function(imgData) {
|
|
|
|
info = null;
|
|
|
|
if(imgData && _.has(imgData, 'data')) {
|
|
|
|
imgData = imgData.data;
|
|
|
|
info = '[';
|
|
|
|
if(imgData.title) {
|
|
|
|
info += imgData.title + ' is ';
|
|
|
|
}
|
|
|
|
if(imgData.animated) {
|
|
|
|
info += 'an animated ' + imgData.type.split('/')[1] + ' with ';
|
|
|
|
} else {
|
|
|
|
info += 'a non-animated ' + imgData.type.split('/')[1] + ' with ';
|
|
|
|
}
|
|
|
|
info += imgData.views + ' views].';
|
|
|
|
}
|
|
|
|
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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-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-13 01:44:19 +02:00
|
|
|
callback(testUrl, testSlug);
|
2013-04-10 21:43:11 +02:00
|
|
|
} 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',
|
2013-04-13 01:44:19 +02:00
|
|
|
'json': true,
|
2013-04-13 01:27:31 +02:00
|
|
|
'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-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-13 01:44:19 +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) {
|
|
|
|
if(matches[2]) { // TODO: handle this in the regex
|
|
|
|
this.api.getImageInfo(matches[1], function(imgData) {
|
|
|
|
var info = this.internalAPI.infoString(imgData);
|
|
|
|
if(info) event.reply(info);
|
|
|
|
}.bind(this));
|
|
|
|
}
|
|
|
|
}.bind(this);
|
|
|
|
dbot.api.link.addHandler(this.name, /http:\/\/i\.imgur\.com\/([a-zA-Z0-9]+)\.([jpg|png|gif])/, imgurHandler);
|
|
|
|
dbot.api.link.addHandler(this.name, /\bhttps?:\/\/imgur\.com\/([a-zA-Z0-9]+)\b/i, imgurHandler);
|
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);
|
|
|
|
}
|