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'),
|
2013-09-10 19:21:05 +02:00
|
|
|
async = require('async'),
|
2013-08-21 23:44:10 +02:00
|
|
|
crypto = require('crypto');
|
2013-04-10 21:43:11 +02:00
|
|
|
|
|
|
|
var imgur = function(dbot) {
|
2013-08-25 21:02:34 +02:00
|
|
|
this.ApiRoot = 'https://api.imgur.com/3/';
|
2013-10-16 10:15:00 +02:00
|
|
|
this.ExcludeRes = [
|
|
|
|
{ 'w': 800, 'h': 600 },
|
|
|
|
{ 'w': 1024, 'h': 768 },
|
|
|
|
{ 'w': 1280, 'h': 768 },
|
|
|
|
{ 'w': 1280, 'h': 960 },
|
|
|
|
{ 'w': 1366, 'h': 768 },
|
|
|
|
{ 'w': 1600, 'h': 900 },
|
|
|
|
{ 'w': 1680, 'h': 1050 },
|
2013-11-25 17:33:27 +01:00
|
|
|
{ 'w': 1920, 'h': 1080 },
|
|
|
|
{ 'w': 1024, 'h': 640 }
|
2013-10-16 10:15:00 +02:00
|
|
|
];
|
2013-09-10 19:07:34 +02:00
|
|
|
this.riCache = [];
|
2013-08-25 21:02:34 +02:00
|
|
|
|
2013-04-13 02:42:20 +02:00
|
|
|
this.internalAPI = {
|
|
|
|
'infoString': function(imgData) {
|
2013-04-16 18:42:57 +02:00
|
|
|
info = '';
|
2013-09-16 04:07:37 +02:00
|
|
|
if(!_.isUndefined(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
|
|
|
}
|
|
|
|
|
2013-05-26 18:55:45 +02:00
|
|
|
return info;
|
|
|
|
}.bind(this),
|
|
|
|
|
|
|
|
'albumInfoString': function(albumData) {
|
|
|
|
var info = '';
|
2013-09-16 04:07:37 +02:00
|
|
|
if(!_.isUndefined(albumData) && _.has(albumData, 'data') && !_.isUndefined(albumData.data.id)) {
|
2013-05-26 18:55:45 +02:00
|
|
|
albumData = albumData.data;
|
|
|
|
if(albumData.title) {
|
|
|
|
info += albumData.title + ' - ';
|
|
|
|
}
|
|
|
|
if(albumData.description) {
|
2013-08-25 22:58:11 +02:00
|
|
|
info += albumData.description.split('\n')[0] + ' is ';
|
2013-05-26 18:55:45 +02:00
|
|
|
}
|
|
|
|
info += 'an album with ' + albumData.images_count + ' images ';
|
|
|
|
info += 'and ' + albumData.views + ' views';
|
|
|
|
if(albumData.nsfw) {
|
|
|
|
info += ' - NSFW';
|
|
|
|
}
|
|
|
|
}
|
2013-04-13 02:42:20 +02:00
|
|
|
return info;
|
2013-05-26 19:10:21 +02:00
|
|
|
}.bind(this),
|
|
|
|
|
|
|
|
'galleryInfoString': function(galData) {
|
|
|
|
var info = '';
|
2013-09-16 04:07:37 +02:00
|
|
|
if(!_.isUndefined(galData) && _.has(galData, 'data') && !_.isUndefined(galData.data.is_album)) {
|
2013-05-26 19:10:21 +02:00
|
|
|
if(galData.data.is_album === true) {
|
|
|
|
info = this.internalAPI.albumInfoString(galData);
|
|
|
|
} else {
|
|
|
|
info = this.internalAPI.infoString(galData);
|
|
|
|
}
|
|
|
|
}
|
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 +
|
2015-02-07 22:53:27 +01:00
|
|
|
'.' + ext[_.random(0, ext.length - 1)],
|
|
|
|
fbman = false;
|
2013-04-22 16:38:03 +02:00
|
|
|
dbot.db.imgur.totalHttpRequests += 1;
|
2013-09-10 18:29:23 +02:00
|
|
|
|
|
|
|
request(testUrl, function(error, response, body) {
|
2013-08-21 17:38:43 +02:00
|
|
|
// 492 is body.length of a removed image
|
|
|
|
if(!error && response.statusCode == 200 && body.length != 492) {
|
|
|
|
dbot.db.imgur.totalImages += 1;
|
|
|
|
var hash = crypto.createHash('md5').update(body).digest("hex");
|
|
|
|
if(_.has(dbot.modules, 'quotes')){
|
|
|
|
// autoadd: {"abcdef": "facebookman"}
|
|
|
|
if(_.has(dbot.config.modules.imgur.autoadd,hash)){
|
2015-02-07 22:53:27 +01:00
|
|
|
fbman = true;
|
2013-09-10 20:11:39 +02:00
|
|
|
var category = this.config.autoadd[hash];
|
2013-08-21 17:38:43 +02:00
|
|
|
if (_.contains(category, testUrl)){
|
|
|
|
// there's probably less than 62^5 chance of this happening
|
|
|
|
} else {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2015-02-07 22:53:27 +01:00
|
|
|
callback(testUrl, testSlug, hash, fbman);
|
2013-08-21 17:38:43 +02:00
|
|
|
} else {
|
|
|
|
this.api.getRandomImage(callback);
|
|
|
|
}
|
|
|
|
}.bind(this));
|
2013-04-13 01:27:31 +02:00
|
|
|
},
|
|
|
|
|
2013-09-10 18:29:23 +02:00
|
|
|
'getGoodRandomImage': function(callback) {
|
|
|
|
this.api.getRandomImage(function(url, slug, hash) {
|
|
|
|
this.api.getImageInfo(slug, function(imgData) {
|
2013-10-16 10:15:00 +02:00
|
|
|
if(!_.isUndefined(imgData) &&
|
|
|
|
imgData.data &&
|
|
|
|
imgData.data.height > 500 && imgData.data.width > 500 &&
|
|
|
|
!_.any(this.ExcludeRes, function(res) {
|
|
|
|
return imgData.data.height == res.h && imgData.data.width == res.w;
|
|
|
|
})) {
|
2013-09-10 18:29:23 +02:00
|
|
|
callback(url, imgData);
|
2015-02-07 22:53:27 +01:00
|
|
|
} else if(fbman === true) {
|
|
|
|
callback(url, imgData);
|
|
|
|
} else {
|
2013-09-10 18:29:23 +02:00
|
|
|
this.api.getGoodRandomImage(callback);
|
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
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-06-05 22:13:40 +02:00
|
|
|
'Authorization': 'Client-ID ' + this.config.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-05-26 18:55:45 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
'getAlbumInfo': function(slug, callback) {
|
|
|
|
request.get({
|
|
|
|
'url': 'https://api.imgur.com/3/album/' + slug + '.json',
|
|
|
|
'json': true,
|
|
|
|
'headers': {
|
2013-06-05 22:13:40 +02:00
|
|
|
'Authorization': 'Client-ID ' + this.config.apikey
|
2013-05-26 18:55:45 +02:00
|
|
|
}
|
|
|
|
}, function(err, response, body) {
|
|
|
|
this.db.totalApiRequests += 1;
|
|
|
|
callback(body);
|
|
|
|
}.bind(this));
|
2013-05-26 19:10:21 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
'getGalleryInfo': function(slug, callback) {
|
|
|
|
request.get({
|
|
|
|
'url': 'https://api.imgur.com/3/gallery/' + slug + '.json',
|
|
|
|
'json': true,
|
|
|
|
'headers': {
|
2013-06-05 22:13:40 +02:00
|
|
|
'Authorization': 'Client-ID ' + this.config.apikey
|
2013-05-26 19:10:21 +02:00
|
|
|
}
|
|
|
|
}, function(err, response, body) {
|
|
|
|
this.db.totalApiRequests += 1;
|
|
|
|
callback(body);
|
|
|
|
}.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-08-24 15:28:46 +02:00
|
|
|
var local = event.user;
|
|
|
|
if(event.params[1]) {
|
2013-08-24 15:31:12 +02:00
|
|
|
local = event.params.splice(1, event.params.length - 1).join(' ').trim();
|
2013-08-24 15:28:46 +02:00
|
|
|
}
|
2013-09-10 19:07:34 +02:00
|
|
|
|
|
|
|
var postImage = function(link, imgData) {
|
|
|
|
var info = this.internalAPI.infoString(imgData);
|
|
|
|
event.reply(local + ': ' + link + ' [' + info + ']');
|
|
|
|
}.bind(this);
|
|
|
|
var newCacheImage = function(link, imgData) {
|
|
|
|
this.riCache.push([link, imgData]);
|
|
|
|
}.bind(this);
|
|
|
|
var callback = postImage;
|
|
|
|
|
|
|
|
if(this.riCache.length > 0) {
|
|
|
|
var image = this.riCache.pop();
|
|
|
|
postImage(image[0], image[1]);
|
|
|
|
callback = newCacheImage;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.api.getGoodRandomImage(callback);
|
2013-08-25 21:02:34 +02:00
|
|
|
},
|
|
|
|
|
2013-09-10 19:07:34 +02:00
|
|
|
// Legacy RI
|
|
|
|
'~lri': function(event) {
|
2013-09-10 18:29:23 +02:00
|
|
|
var local = event.user;
|
|
|
|
if(event.params[1]) {
|
|
|
|
local = event.params.splice(1, event.params.length - 1).join(' ').trim();
|
|
|
|
}
|
2013-09-10 19:07:34 +02:00
|
|
|
this.api.getRandomImage(function(link, slug) {
|
|
|
|
this.api.getImageInfo(slug, function(imgData) {
|
|
|
|
var info = this.internalAPI.infoString(imgData);
|
|
|
|
event.reply(local + ': ' + link + ' [' + info + ']');
|
|
|
|
}.bind(this));
|
2013-09-10 18:29:23 +02:00
|
|
|
}.bind(this));
|
|
|
|
},
|
2013-09-10 19:07:34 +02:00
|
|
|
|
2013-09-10 18:29:23 +02:00
|
|
|
// Super RI
|
2013-09-09 21:30:23 +02:00
|
|
|
'~sri': function(event) {
|
|
|
|
var local = event.user;
|
|
|
|
if(event.params[1]) {
|
|
|
|
local = event.params.splice(1, event.params.length - 1).join(' ').trim();
|
|
|
|
}
|
|
|
|
request.get({
|
|
|
|
'url': this.ApiRoot + 'gallery/random/random/',
|
|
|
|
'json': true,
|
|
|
|
'headers': {
|
|
|
|
'Authorization': 'Client-ID ' + this.config.apikey
|
|
|
|
}
|
|
|
|
}, function(err, response, body) {
|
2013-09-13 16:54:17 +02:00
|
|
|
if(!_.isUndefined(body) && body.data && body.data[0] != undefined) {
|
2013-09-09 21:30:23 +02:00
|
|
|
var num = _.random(0, body.data.length - 1);
|
|
|
|
this.api.getGalleryInfo(body.data[num].id, function(gal) {
|
|
|
|
event.reply(local + ': ' + gal.data.link + ' [' +
|
|
|
|
this.internalAPI.galleryInfoString(gal) + ']');
|
|
|
|
}.bind(this));
|
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
2013-08-25 21:02:34 +02:00
|
|
|
'~imgur': function(event) {
|
|
|
|
var term = event.input[1];
|
|
|
|
request.get({
|
|
|
|
'url': this.ApiRoot + 'gallery/search/',
|
|
|
|
'json': true,
|
|
|
|
'headers': {
|
|
|
|
'Authorization': 'Client-ID ' + this.config.apikey
|
|
|
|
},
|
|
|
|
'qs': {
|
|
|
|
'q': term
|
|
|
|
}
|
|
|
|
}, function(err, response, body) {
|
2013-09-16 04:07:37 +02:00
|
|
|
if(!_.isUndefined(body) && body.data && body.data[0] != undefined) {
|
2013-08-25 22:45:47 +02:00
|
|
|
var num = _.random(0, body.data.length - 1);
|
|
|
|
this.api.getGalleryInfo(body.data[num].id, function(gal) {
|
2013-08-25 21:02:34 +02:00
|
|
|
event.reply(dbot.t('imgurinfo', {
|
|
|
|
'info': this.internalAPI.galleryInfoString(gal)
|
|
|
|
}) + ' - ' + gal.data.link);
|
|
|
|
}.bind(this));
|
2013-08-25 21:04:42 +02:00
|
|
|
} else {
|
|
|
|
event.reply(dbot.t('imgur_noresults'));
|
2013-08-25 21:02:34 +02:00
|
|
|
}
|
|
|
|
}.bind(this));
|
2013-04-10 21:43:11 +02:00
|
|
|
}
|
|
|
|
}
|
2013-12-29 19:38:24 +01:00
|
|
|
this.commands['~imgur'].regex = [/^imgur ([\d\w\s-]*)/, 2];
|
2013-04-13 01:27:31 +02:00
|
|
|
|
|
|
|
this.onLoad = function() {
|
2013-07-25 23:56:44 +02:00
|
|
|
var imgurHandler = function(matches, name, callback) {
|
2013-05-26 18:55:45 +02:00
|
|
|
if(matches[1]) {
|
2013-05-26 19:19:25 +02:00
|
|
|
var dataCallback = function(data) {
|
|
|
|
var info;
|
|
|
|
if(name == 'imgurimage') {
|
|
|
|
info = this.internalAPI.infoString(data);
|
|
|
|
} else if(name == 'imguralbum') {
|
|
|
|
info = this.internalAPI.albumInfoString(data);
|
|
|
|
} else if(name == 'imgurgallery') {
|
|
|
|
info = this.internalAPI.galleryInfoString(data);
|
|
|
|
}
|
|
|
|
|
2013-07-25 23:56:44 +02:00
|
|
|
if(info) callback(dbot.t('imgurinfo', { 'info': info }));
|
2013-05-26 19:19:25 +02:00
|
|
|
}.bind(this);
|
|
|
|
|
|
|
|
if(name == 'imgurimage') {
|
|
|
|
this.api.getImageInfo(matches[1], dataCallback);
|
|
|
|
} else if(name == 'imguralbum') {
|
|
|
|
this.api.getAlbumInfo(matches[1], dataCallback);
|
|
|
|
} else if(name == 'imgurgallery') {
|
|
|
|
this.api.getGalleryInfo(matches[1], dataCallback);
|
|
|
|
}
|
2013-04-13 02:42:20 +02:00
|
|
|
}
|
|
|
|
}.bind(this);
|
2013-05-26 18:55:45 +02:00
|
|
|
|
2013-05-26 19:19:25 +02:00
|
|
|
dbot.api.link.addHandler('imguralbum', /https?:\/\/imgur\.com\/a\/([a-zA-Z0-9]+)/, imgurHandler);
|
|
|
|
dbot.api.link.addHandler('imgurgallery', /https?:\/\/imgur\.com\/gallery\/([a-zA-Z0-9]+)/, imgurHandler);
|
|
|
|
dbot.api.link.addHandler('imgurimage', /https?:\/\/i\.imgur\.com\/([a-zA-Z0-9]+)\.([jpg|png|gif])/, imgurHandler);
|
|
|
|
dbot.api.link.addHandler('imgurimage', /https?:\/\/imgur\.com\/([a-zA-Z0-9]+)/, imgurHandler);
|
2013-05-26 18:55:45 +02:00
|
|
|
|
2014-02-26 16:49:44 +01:00
|
|
|
async.times(this.config.ricachelength, function(n, next) {
|
2013-09-10 19:07:34 +02:00
|
|
|
this.api.getGoodRandomImage(function(link, imgData) {
|
|
|
|
this.riCache.push([ link, imgData ]);
|
2013-09-10 19:21:05 +02:00
|
|
|
next();
|
2013-09-10 19:07:34 +02:00
|
|
|
}.bind(this));
|
2013-09-10 20:25:20 +02:00
|
|
|
}.bind(this), function() {});
|
2013-09-10 19:07:34 +02:00
|
|
|
|
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-05-28 19:40:25 +02:00
|
|
|
this.db = dbot.db.imgur;
|
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);
|
|
|
|
}
|