3
0
mirror of https://github.com/reality/dbot.git synced 2025-01-25 19:44:22 +01:00

improve imgur linkhandler efficiency by using only one callback and link name feature [#475]

This commit is contained in:
reality 2013-05-26 17:19:25 +00:00
parent 3afb53939a
commit 0cb8147abb

View File

@ -164,35 +164,35 @@ var imgur = function(dbot) {
} }
this.onLoad = function() { this.onLoad = function() {
var imgurImageHandler = function(event, matches, name) { var imgurHandler = function(event, matches, name) {
if(matches[1]) { if(matches[1]) {
this.api.getImageInfo(matches[1], function(imgData) { var dataCallback = function(data) {
var info = this.internalAPI.infoString(imgData); 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 })); if(info) event.reply(dbot.t('imgurinfo', { 'info': info }));
}.bind(this)); }.bind(this);
}
}.bind(this); if(name == 'imgurimage') {
var imgurAlbumHandler = function(event, matches, name) { this.api.getImageInfo(matches[1], dataCallback);
if(matches[1]) { } else if(name == 'imguralbum') {
this.api.getAlbumInfo(matches[1], function(albumData) { this.api.getAlbumInfo(matches[1], dataCallback);
var info = this.internalAPI.albumInfoString(albumData); } else if(name == 'imgurgallery') {
if(info) event.reply(dbot.t('imgurinfo', { 'info': info })); this.api.getGalleryInfo(matches[1], dataCallback);
}.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); }.bind(this);
dbot.api.link.addHandler(this.name, /https?:\/\/imgur\.com\/a\/([a-zA-Z0-9]+)/, imgurAlbumHandler); dbot.api.link.addHandler('imguralbum', /https?:\/\/imgur\.com\/a\/([a-zA-Z0-9]+)/, imgurHandler);
dbot.api.link.addHandler(this.name, /https?:\/\/imgur\.com\/gallery\/([a-zA-Z0-9]+)/, imgurGalleryHandler); dbot.api.link.addHandler('imgurgallery', /https?:\/\/imgur\.com\/gallery\/([a-zA-Z0-9]+)/, imgurHandler);
dbot.api.link.addHandler(this.name, /https?:\/\/i\.imgur\.com\/([a-zA-Z0-9]+)\.([jpg|png|gif])/, imgurImageHandler); dbot.api.link.addHandler('imgurimage', /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]+)/, imgurImageHandler); 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, 'totalHttpRequests')) dbot.db.imgur.totalHttpRequests = 0;
if(!_.has(dbot.db.imgur, 'totalApiRequests')) dbot.db.imgur.totalApiRequests = 0; if(!_.has(dbot.db.imgur, 'totalApiRequests')) dbot.db.imgur.totalApiRequests = 0;