3
0
mirror of https://github.com/reality/dbot.git synced 2024-11-24 04:49:25 +01:00
dbot/modules/imgur/imgur.js

57 lines
1.6 KiB
JavaScript
Raw Normal View History

/**
* Module Name: imgur
* Description: Various imgur functionality
*/
var _ = require('underscore')._,
request = require('request');
var imgur = function(dbot) {
this.api = {
'getRandomImage': function(callback) {
var random = function(len) {
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
return len ? chars.charAt(~~(Math.random()*chars.length)) + random(len-1) : "";
};
var testUrl = 'http://i.imgur.com/' +
random(5) +
2013-04-10 22:08:04 +02:00
'.png';
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) {
callback(testUrl);
} else {
this.api.getRandomImage(callback);
}
}.bind(this));
},
'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);
});
}
};
this.commands = {
2013-04-10 22:36:26 +02:00
'~ri': function(event) {
this.api.getRandomImage(function(link) {
2013-04-11 21:33:33 +02:00
event.reply(event.user + ': (' + dbot.t('nsfw') + ') ' + link);
});
}
}
this.onLoad = function() {
}.bind(this);
};
exports.fetch = function(dbot) {
return new imgur(dbot);
}