From d8342f4489c53469a5b3bbe4fb5c7f3e3cb1b23c Mon Sep 17 00:00:00 2001 From: reality Date: Fri, 12 Apr 2013 22:38:00 +0000 Subject: [PATCH 1/5] Actually fix [#335] --- modules/link/link.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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); From 677a820638f69f60b509dfc165957a66219c5c06 Mon Sep 17 00:00:00 2001 From: reality <tinmachin3@gmail.com> Date: Fri, 12 Apr 2013 22:53:45 +0000 Subject: [PATCH 2/5] add web api function for getUrl instead of string in base, use externalAddress optional config key to define external behaviours [#264] --- modules/poll/commands.js | 8 +++----- modules/profile/commands.js | 2 +- modules/quotes/commands.js | 7 +------ modules/web/config.json | 3 ++- modules/web/web.js | 12 +++++++++++- strings.json | 3 --- 6 files changed, 18 insertions(+), 17 deletions(-) diff --git a/modules/poll/commands.js b/modules/poll/commands.js index ac36252..15db188 100644 --- a/modules/poll/commands.js +++ b/modules/poll/commands.js @@ -21,14 +21,12 @@ var commands = function(dbot) { for(var i=0;i<options.length;i++) { polls[name]['votes'][options[i]] = 0; } + + event.reply(dbot.t('poll_created', { 'name': name, 'description': description, - 'url': dbot.t('url', { - 'host': dbot.config.web.webHost, - 'port': dbot.config.web.webPort, - 'path': 'polls/' + name - }) + 'url': dbot.api.web.getUrl('/poll/' + 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 207a30d..1b04ac4 100644 --- a/modules/quotes/commands.js +++ b/modules/quotes/commands.js @@ -71,7 +71,6 @@ var commands = function(dbot) { }, // Search a given category for some text. - // TODO fix '~qsearch': function(event) { var haystack = event.input[1].trim().toLowerCase(); var needle = event.input[2]; @@ -202,11 +201,7 @@ var commands = function(dbot) { _.has(dbot.config.web, 'webPort')) { 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}" } } From 270ae138bfef5cea306b0aa7248475e45eb38ab8 Mon Sep 17 00:00:00 2001 From: reality <tinmachin3@gmail.com> Date: Fri, 12 Apr 2013 23:27:31 +0000 Subject: [PATCH 3/5] Initial API function for imgur detail getting [#350] --- modules/imgur/imgur.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/modules/imgur/imgur.js b/modules/imgur/imgur.js index 2bc6512..825049d 100644 --- a/modules/imgur/imgur.js +++ b/modules/imgur/imgur.js @@ -25,6 +25,17 @@ var imgur = function(dbot) { 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); + }); } }; @@ -35,6 +46,9 @@ var imgur = function(dbot) { }); } } + + this.onLoad = function() { + }.bind(this); }; exports.fetch = function(dbot) { From a9acaeaebd1c7ab3171cb2f4b323170d3c32b7e3 Mon Sep 17 00:00:00 2001 From: reality <tinmachin3@gmail.com> Date: Fri, 12 Apr 2013 23:44:19 +0000 Subject: [PATCH 4/5] show info about ~ri images [#350] --- modules/imgur/imgur.js | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/modules/imgur/imgur.js b/modules/imgur/imgur.js index 825049d..d3fa072 100644 --- a/modules/imgur/imgur.js +++ b/modules/imgur/imgur.js @@ -13,14 +13,16 @@ var imgur = function(dbot) { 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); } @@ -30,6 +32,7 @@ var imgur = function(dbot) { 'getImageInfo': function(slug, callback) { request.get({ 'url': 'https://api.imgur.com/3/image/' + slug + '.json', + 'json': true, 'headers': { 'Authorization': 'Client-ID 86fd3a8da348b65' } @@ -41,9 +44,26 @@ var imgur = function(dbot) { 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) { + imgData = imgData.data; + var info = '['; + if(imgData.title) { + info += imgData.title + ' is '; + } else { + info += 'no-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.]'; + + event.reply(event.user + ': ' + link + ' ' + info); + }); + + }.bind(this)); } } From 841ebc3f216366638d738723add253db47d98184 Mon Sep 17 00:00:00 2001 From: reality <tinmachin3@gmail.com> Date: Sat, 13 Apr 2013 00:42:20 +0000 Subject: [PATCH 5/5] auto handlers for imgur links [#350] --- modules/imgur/imgur.js | 49 ++++++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/modules/imgur/imgur.js b/modules/imgur/imgur.js index d3fa072..e47928c 100644 --- a/modules/imgur/imgur.js +++ b/modules/imgur/imgur.js @@ -7,6 +7,27 @@ 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) { @@ -46,28 +67,24 @@ var imgur = function(dbot) { '~ri': function(event) { this.api.getRandomImage(function(link, slug) { this.api.getImageInfo(slug, function(imgData) { - imgData = imgData.data; - var info = '['; - if(imgData.title) { - info += imgData.title + ' is '; - } else { - info += 'no-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.]'; - + 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); };