From a851a2bee4156d3c88666dcc5640b9c010cd17f4 Mon Sep 17 00:00:00 2001 From: reality Date: Sun, 26 May 2013 12:42:49 +0000 Subject: [PATCH 1/4] Fix possible crash with outputPrefix when module is not loaded properly [#462] --- run.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/run.js b/run.js index 07a017e..17d8c8e 100644 --- a/run.js +++ b/run.js @@ -97,7 +97,7 @@ DBot.prototype.t = function(string, formatData) { if(_.has(this.strings[string], lang)) { var module = this.stringMap[string]; formattedString = this.strings[string][lang].format(formatData); - if(this.config[module].outputPrefix) { + if(this.config[module] && this.config[module].outputPrefix) { formattedString = '[' + this.config[module].outputPrefix + '] ' + formattedString; } From 18e6053399b21c368bd24ebab4ca935ffc12066a Mon Sep 17 00:00:00 2001 From: reality Date: Sun, 26 May 2013 16:55:45 +0000 Subject: [PATCH 2/4] Support for imgur album data [#475] --- modules/imgur/imgur.js | 52 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/modules/imgur/imgur.js b/modules/imgur/imgur.js index 1a3ded1..cbaa0d1 100644 --- a/modules/imgur/imgur.js +++ b/modules/imgur/imgur.js @@ -12,7 +12,7 @@ var imgur = function(dbot) { this.internalAPI = { 'infoString': function(imgData) { info = ''; - if(imgData && _.has(imgData, 'data')) { + if(imgData && _.has(imgData, 'data') && !_.isUndefined(imgData.data.type)) { imgData = imgData.data; if(imgData.title) { info += imgData.title + ' - '; @@ -30,6 +30,25 @@ var imgur = function(dbot) { info += imgData.width + 'x' + imgData.height + ')'; } + return info; + }.bind(this), + + 'albumInfoString': function(albumData) { + var info = ''; + if(albumData && _.has(albumData, 'data') && !_.isUndefined(albumData.data.id)) { + albumData = albumData.data; + if(albumData.title) { + info += albumData.title + ' - '; + } + if(albumData.description) { + info += albumData.description + ' is '; + } + info += 'an album with ' + albumData.images_count + ' images '; + info += 'and ' + albumData.views + ' views'; + if(albumData.nsfw) { + info += ' - NSFW'; + } + } return info; }.bind(this) }; @@ -88,6 +107,19 @@ var imgur = function(dbot) { this.db.totalApiRequests += 1; callback(body); }.bind(this)); + }, + + 'getAlbumInfo': function(slug, callback) { + request.get({ + 'url': 'https://api.imgur.com/3/album/' + slug + '.json', + 'json': true, + 'headers': { + 'Authorization': 'Client-ID ' + dbot.config.imgur.apikey + } + }, function(err, response, body) { + this.db.totalApiRequests += 1; + callback(body); + }.bind(this)); } }; this.api['getRandomImage'].external = true; @@ -107,7 +139,8 @@ var imgur = function(dbot) { } this.onLoad = function() { - var imgurHandler = function(event, matches, name) { + var imgurImageHandler = function(event, matches, name) { + event.reply(matches[1]); if(matches[1]) { this.api.getImageInfo(matches[1], function(imgData) { var info = this.internalAPI.infoString(imgData); @@ -115,8 +148,19 @@ var imgur = function(dbot) { }.bind(this)); } }.bind(this); - 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); + var imgurAlbumHandler = function(event, matches, name) { + if(matches[1]) { + this.api.getAlbumInfo(matches[1], function(albumData) { + var info = this.internalAPI.albumInfoString(albumData); + if(info) event.reply(dbot.t('imgurinfo', { 'info': info })); + }.bind(this)); + } + }.bind(this); + + dbot.api.link.addHandler(this.name, /https?:\/\/imgur\.com\/a\/([a-zA-Z0-9]+)/, imgurAlbumHandler); + dbot.api.link.addHandler(this.name, /https?:\/\/i\.imgur\.com\/([a-zA-Z0-9]+)\.([jpg|png|gif])/, imgurImageHandler); + dbot.api.link.addHandler(this.name, /https?:\/\/imgur\.com\/([a-zA-Z0-9]+)/, imgurImageHandler); + 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; From 3afb53939af4cc6a1e132113d8f3759084cb80d7 Mon Sep 17 00:00:00 2001 From: reality Date: Sun, 26 May 2013 17:10:21 +0000 Subject: [PATCH 3/4] Get gallery info depending on if it is an image or album [#475] --- modules/imgur/imgur.js | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/modules/imgur/imgur.js b/modules/imgur/imgur.js index cbaa0d1..a108a4a 100644 --- a/modules/imgur/imgur.js +++ b/modules/imgur/imgur.js @@ -50,6 +50,18 @@ var imgur = function(dbot) { } } return info; + }.bind(this), + + 'galleryInfoString': function(galData) { + var info = ''; + if(galData && _.has(galData, 'data') && !_.isUndefined(galData.data.is_album)) { + if(galData.data.is_album === true) { + info = this.internalAPI.albumInfoString(galData); + } else { + info = this.internalAPI.infoString(galData); + } + } + return info; }.bind(this) }; @@ -120,6 +132,19 @@ var imgur = function(dbot) { this.db.totalApiRequests += 1; callback(body); }.bind(this)); + }, + + 'getGalleryInfo': function(slug, callback) { + request.get({ + 'url': 'https://api.imgur.com/3/gallery/' + slug + '.json', + 'json': true, + 'headers': { + 'Authorization': 'Client-ID ' + dbot.config.imgur.apikey + } + }, function(err, response, body) { + this.db.totalApiRequests += 1; + callback(body); + }.bind(this)); } }; this.api['getRandomImage'].external = true; @@ -140,7 +165,6 @@ var imgur = function(dbot) { this.onLoad = function() { var imgurImageHandler = function(event, matches, name) { - event.reply(matches[1]); if(matches[1]) { this.api.getImageInfo(matches[1], function(imgData) { var info = this.internalAPI.infoString(imgData); @@ -156,8 +180,17 @@ var imgur = function(dbot) { }.bind(this)); } }.bind(this); + var imgurGalleryHandler = function(event, matches, name) { + if(matches[1]) { + this.api.getGalleryInfo(matches[1], function(galData) { + var info = this.internalAPI.galleryInfoString(galData); + if(info) event.reply(dbot.t('imgurinfo', { 'info': info })); + }.bind(this)); + } + }.bind(this); dbot.api.link.addHandler(this.name, /https?:\/\/imgur\.com\/a\/([a-zA-Z0-9]+)/, imgurAlbumHandler); + dbot.api.link.addHandler(this.name, /https?:\/\/imgur\.com\/gallery\/([a-zA-Z0-9]+)/, imgurGalleryHandler); dbot.api.link.addHandler(this.name, /https?:\/\/i\.imgur\.com\/([a-zA-Z0-9]+)\.([jpg|png|gif])/, imgurImageHandler); dbot.api.link.addHandler(this.name, /https?:\/\/imgur\.com\/([a-zA-Z0-9]+)/, imgurImageHandler); From 0cb8147abbcd7e47b293bc1a016dff75b8c4c293 Mon Sep 17 00:00:00 2001 From: reality Date: Sun, 26 May 2013 17:19:25 +0000 Subject: [PATCH 4/4] improve imgur linkhandler efficiency by using only one callback and link name feature [#475] --- modules/imgur/imgur.js | 48 +++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/modules/imgur/imgur.js b/modules/imgur/imgur.js index a108a4a..42edc79 100644 --- a/modules/imgur/imgur.js +++ b/modules/imgur/imgur.js @@ -164,35 +164,35 @@ var imgur = function(dbot) { } this.onLoad = function() { - var imgurImageHandler = function(event, matches, name) { - if(matches[1]) { - this.api.getImageInfo(matches[1], function(imgData) { - var info = this.internalAPI.infoString(imgData); - if(info) event.reply(dbot.t('imgurinfo', { 'info': info })); - }.bind(this)); - } - }.bind(this); - var imgurAlbumHandler = function(event, matches, name) { + var imgurHandler = function(event, matches, name) { if(matches[1]) { - this.api.getAlbumInfo(matches[1], function(albumData) { - var info = this.internalAPI.albumInfoString(albumData); + 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); + } + if(info) event.reply(dbot.t('imgurinfo', { 'info': info })); - }.bind(this)); - } - }.bind(this); - var imgurGalleryHandler = function(event, matches, name) { - if(matches[1]) { - this.api.getGalleryInfo(matches[1], function(galData) { - var info = this.internalAPI.galleryInfoString(galData); - if(info) event.reply(dbot.t('imgurinfo', { 'info': info })); - }.bind(this)); + }.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); + } } }.bind(this); - dbot.api.link.addHandler(this.name, /https?:\/\/imgur\.com\/a\/([a-zA-Z0-9]+)/, imgurAlbumHandler); - dbot.api.link.addHandler(this.name, /https?:\/\/imgur\.com\/gallery\/([a-zA-Z0-9]+)/, imgurGalleryHandler); - dbot.api.link.addHandler(this.name, /https?:\/\/i\.imgur\.com\/([a-zA-Z0-9]+)\.([jpg|png|gif])/, imgurImageHandler); - dbot.api.link.addHandler(this.name, /https?:\/\/imgur\.com\/([a-zA-Z0-9]+)/, imgurImageHandler); + 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); if(!_.has(dbot.db.imgur, 'totalHttpRequests')) dbot.db.imgur.totalHttpRequests = 0; if(!_.has(dbot.db.imgur, 'totalApiRequests')) dbot.db.imgur.totalApiRequests = 0;