diff --git a/modules/imgur/imgur.js b/modules/imgur/imgur.js index 2bc6512..e47928c 100644 --- a/modules/imgur/imgur.js +++ b/modules/imgur/imgur.js @@ -7,34 +7,85 @@ var _ = require('underscore')._, request = require('request'); var imgur = function(dbot) { + this.internalAPI = { + 'infoString': function(imgData) { + info = null; + if(imgData && _.has(imgData, 'data')) { + imgData = imgData.data; + info = '['; + if(imgData.title) { + info += imgData.title + ' is '; + } + if(imgData.animated) { + info += 'an animated ' + imgData.type.split('/')[1] + ' with '; + } else { + info += 'a non-animated ' + imgData.type.split('/')[1] + ' with '; + } + info += imgData.views + ' views].'; + } + + return info; + } + }; + this.api = { 'getRandomImage': function(callback) { var random = function(len) { var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz"; return len ? chars.charAt(~~(Math.random()*chars.length)) + random(len-1) : ""; }; - + + var ext = [ 'gif', 'png', 'jpg' ]; + var testSlug = random(5); var testUrl = 'http://i.imgur.com/' + - random(5) + - '.png'; + testSlug + + '.' + ext[_.random(0, ext.length - 1)]; var image = request(testUrl, function(error, response, body) { // 492 is body.length of a removed image if(!error && response.statusCode == 200 && body.length != 492) { - callback(testUrl); + callback(testUrl, testSlug); } else { this.api.getRandomImage(callback); } }.bind(this)); + }, + + 'getImageInfo': function(slug, callback) { + request.get({ + 'url': 'https://api.imgur.com/3/image/' + slug + '.json', + 'json': true, + 'headers': { + 'Authorization': 'Client-ID 86fd3a8da348b65' + } + }, function(err, response, body) { + callback(body); + }); } }; this.commands = { '~ri': function(event) { - this.api.getRandomImage(function(link) { - event.reply(event.user + ': (' + dbot.t('nsfw') + ') ' + link); - }); + this.api.getRandomImage(function(link, slug) { + this.api.getImageInfo(slug, function(imgData) { + var info = this.internalAPI.infoString(imgData); + event.reply(event.user + ': ' + link + ' ' + info); + }.bind(this)); + }.bind(this)); } } + + this.onLoad = function() { + var imgurHandler = function(event, matches, name) { + if(matches[2]) { // TODO: handle this in the regex + this.api.getImageInfo(matches[1], function(imgData) { + var info = this.internalAPI.infoString(imgData); + if(info) event.reply(info); + }.bind(this)); + } + }.bind(this); + dbot.api.link.addHandler(this.name, /http:\/\/i\.imgur\.com\/([a-zA-Z0-9]+)\.([jpg|png|gif])/, imgurHandler); + dbot.api.link.addHandler(this.name, /\bhttps?:\/\/imgur\.com\/([a-zA-Z0-9]+)\b/i, imgurHandler); + }.bind(this); }; exports.fetch = function(dbot) { diff --git a/modules/link/link.js b/modules/link/link.js index e145538..837b3fa 100644 --- a/modules/link/link.js +++ b/modules/link/link.js @@ -14,7 +14,7 @@ var link = function(dbot) { this.fetchTitle = function(event, link) { var limit = 1000000, size = 0, - page = request(link, function(error, response, body) { + page = request(link.replace('https', 'http'), function(error, response, body) { if(!error && response.statusCode == 200) { body = body.replace(/(\r\n|\n\r|\n)/gm, " "); var title = body.valMatch(/(.*)<\/title>/, 2); diff --git a/modules/poll/commands.js b/modules/poll/commands.js index 276cc94..1df1a6e 100644 --- a/modules/poll/commands.js +++ b/modules/poll/commands.js @@ -27,11 +27,7 @@ var commands = function(dbot) { event.reply(dbot.t('poll_created', { 'name': name, 'description': description, - 'url': dbot.t('url', { - 'host': 'test.com', - 'port': 80, - 'path': 'polls/' + name - }) + 'url': dbot.api.web.getUrl('/poll/' + name) })); } else if(err instanceof AlreadyExistsError) { event.reply(dbot.t('poll_exists', { 'name': name })); diff --git a/modules/profile/commands.js b/modules/profile/commands.js index a402066..ece4638 100644 --- a/modules/profile/commands.js +++ b/modules/profile/commands.js @@ -33,7 +33,7 @@ var commands = function(dbot){ if(event.params[1]){ var primary = dbot.api.users.resolveUser(event.server, event.params[1]); if(_.has(dbot.db.profiles[event.server], primary.toLowerCase())){ - event.reply("http://"+dbot.config.web.webHost+":"+dbot.config.web.webPort+"/profile/"+event.server+"/"+primary.toLowerCase()); + event.reply(dbot.api.web.getUrl("/profile/"+event.server+"/"+primary.toLowerCase()); } else{ event.reply("No profile found for "+event.params[1]); diff --git a/modules/quotes/commands.js b/modules/quotes/commands.js index d7e6de3..fdd35f5 100644 --- a/modules/quotes/commands.js +++ b/modules/quotes/commands.js @@ -91,7 +91,17 @@ var commands = function(dbot) { { 'count': rmCacheCount })); }, - // Remove last quote from category + // Retrieve quote from a category in the database. + '~q': function(event) { + var key = event.input[1].trim().toLowerCase(); + var quote = this.api.getQuote(event, event.input[1]); + if(quote) { + event.reply(key + ': ' + quote); + } else { + event.reply(dbot.t('category_not_found', {'category': key})); + } + }, + '~rmlast': function(event) { if(this.rmAllowed === true || _.include(dbot.config.admins, event.user)) { var key = event.input[1].trim().toLowerCase(), @@ -352,11 +362,7 @@ var commands = function(dbot) { if(_.has(dbot.config, 'web') && _.has(dbot.config.web, 'webHost')) { event.reply(dbot.t('quote_link', { 'category': key, - 'url': dbot.t('url', { - 'host': dbot.config.web.webHost, - 'port': dbot.config.web.webPort, - 'path': 'quotes/' + encodeURIComponent(key) - }) + 'url': dbot.api.web.getUrl('quotes/' + encodeURIComponent(key)) })); } else { event.reply(dbot.t('web_not_configured')); diff --git a/modules/web/config.json b/modules/web/config.json index 981e0f7..7ed62f6 100644 --- a/modules/web/config.json +++ b/modules/web/config.json @@ -1,4 +1,5 @@ { "webHost": "localhost", - "webPort": 8080 + "webPort": 8080, + "externalPath": false } diff --git a/modules/web/web.js b/modules/web/web.js index a71dac3..452beb4 100644 --- a/modules/web/web.js +++ b/modules/web/web.js @@ -37,7 +37,17 @@ var webInterface = function(dbot) { this.onDestroy = function() { server.close(); - } + }; + + this.api = { + 'getUrl': function(path) { + if(this.config.externalPath) { + return this.config.externalPath + '/' + path; + } else { + return 'http://' + this.config.webHost + ':' + port + '/' + path; + } + }; + }; }; exports.fetch = function(dbot) { diff --git a/strings.json b/strings.json index 1f67531..f9d5189 100644 --- a/strings.json +++ b/strings.json @@ -10,8 +10,5 @@ "es": "No se pudó cargar el módulo: {moduleName}", "na'vi": "Oeru Oel {moduleName}it sung.", "cy": "Wedi methu a llwytho modiwl: {moduleName}" - }, - "url": { - "en": "http://{host}:{port}/{path}" } }