From 4fa870e34edc1136858b23d89643045ea640f6e6 Mon Sep 17 00:00:00 2001 From: reality Date: Wed, 10 Apr 2013 19:43:11 +0000 Subject: [PATCH 1/5] Initial imgur module with ~randomimgur command --- modules/imgur/imgur.js | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 modules/imgur/imgur.js diff --git a/modules/imgur/imgur.js b/modules/imgur/imgur.js new file mode 100644 index 0000000..2dad0b6 --- /dev/null +++ b/modules/imgur/imgur.js @@ -0,0 +1,36 @@ +/** + * Module Name: imgur + * Description: Various imgur functionality + */ + +var _ = require('underscore')._, + request = require('request'); + +var imgur = function(dbot) { + this.api = { + 'getRandomImage': function(callback) { + var testUrl = 'http://i.imgur.com/' + + Math.random().toString(36).substr(2,6) + + '.jpg'; + var image = request(testUrl, function(error, response, body) { + if(!error && response.statusCode == 200) { + callback(testUrl); + } else { + this.api.getRandomImage(callback); + } + }.bind(this)); + } + }; + + this.commands = { + '~randomimgur': function(event) { + this.api.getRandomImage(function(link) { + event.reply(event.user + ': ' + link); + }); + } + } +}; + +exports.fetch = function(dbot) { + return new imgur(dbot); +} From 88037de55ee2b9e7d2371438301bccf5af1f9912 Mon Sep 17 00:00:00 2001 From: reality Date: Wed, 10 Apr 2013 20:05:57 +0000 Subject: [PATCH 2/5] prevent returning deleted imgurs --- modules/imgur/imgur.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/imgur/imgur.js b/modules/imgur/imgur.js index 2dad0b6..744e390 100644 --- a/modules/imgur/imgur.js +++ b/modules/imgur/imgur.js @@ -13,7 +13,8 @@ var imgur = function(dbot) { Math.random().toString(36).substr(2,6) + '.jpg'; var image = request(testUrl, function(error, response, body) { - if(!error && response.statusCode == 200) { + // 492 is body.length of a removed image + if(!error && response.statusCode == 200 && body.length != 492) { callback(testUrl); } else { this.api.getRandomImage(callback); From d82108de831bab57613385d9bdace6908d5fca75 Mon Sep 17 00:00:00 2001 From: reality Date: Wed, 10 Apr 2013 20:08:04 +0000 Subject: [PATCH 3/5] imgur better --- modules/imgur/imgur.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/imgur/imgur.js b/modules/imgur/imgur.js index 744e390..ac5a578 100644 --- a/modules/imgur/imgur.js +++ b/modules/imgur/imgur.js @@ -11,7 +11,7 @@ var imgur = function(dbot) { 'getRandomImage': function(callback) { var testUrl = 'http://i.imgur.com/' + Math.random().toString(36).substr(2,6) + - '.jpg'; + '.png'; var image = request(testUrl, function(error, response, body) { // 492 is body.length of a removed image if(!error && response.statusCode == 200 && body.length != 492) { From 8f977f89231816762cb102362edce6da3ff756f0 Mon Sep 17 00:00:00 2001 From: reality Date: Wed, 10 Apr 2013 20:14:54 +0000 Subject: [PATCH 4/5] faster and includes uppercase characters (which are apparently the larger images) --- modules/imgur/imgur.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/modules/imgur/imgur.js b/modules/imgur/imgur.js index ac5a578..fb4c264 100644 --- a/modules/imgur/imgur.js +++ b/modules/imgur/imgur.js @@ -9,8 +9,13 @@ var _ = require('underscore')._, 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/' + - Math.random().toString(36).substr(2,6) + + random(5) + '.png'; var image = request(testUrl, function(error, response, body) { // 492 is body.length of a removed image From 8cd703940ffa27938657c20d4c3524498db2b90e Mon Sep 17 00:00:00 2001 From: reality Date: Wed, 10 Apr 2013 20:36:26 +0000 Subject: [PATCH 5/5] ~ri instead of ~randomimgur --- modules/imgur/imgur.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/imgur/imgur.js b/modules/imgur/imgur.js index fb4c264..3db7f1c 100644 --- a/modules/imgur/imgur.js +++ b/modules/imgur/imgur.js @@ -29,7 +29,7 @@ var imgur = function(dbot) { }; this.commands = { - '~randomimgur': function(event) { + '~ri': function(event) { this.api.getRandomImage(function(link) { event.reply(event.user + ': ' + link); });