From 19cc4c474d253f8e8f2a2dbc2f5ce4eef65f5419 Mon Sep 17 00:00:00 2001 From: Scritches Date: Mon, 26 Mar 2018 13:52:06 -0400 Subject: [PATCH 1/2] New minify module Added a module that provides URL minifier support for dbot. Updated spotify and lastfm modules with support for url minimization via the include 'bitly' minimizer (which minimizes spotify.com links to spoti.fi - very nice) --- modules/lastfm/lastfm.js | 8 ++- modules/minify/config.json | 9 +++ modules/minify/minify.js | 116 ++++++++++++++++++++++++++++++++++++ modules/minify/strings.json | 11 ++++ modules/spotify/spotify.js | 46 ++++++++------ 5 files changed, 172 insertions(+), 18 deletions(-) create mode 100644 modules/minify/config.json create mode 100644 modules/minify/minify.js create mode 100644 modules/minify/strings.json diff --git a/modules/lastfm/lastfm.js b/modules/lastfm/lastfm.js index 1ad9813..4571714 100644 --- a/modules/lastfm/lastfm.js +++ b/modules/lastfm/lastfm.js @@ -297,7 +297,13 @@ var lastfm = function(dbot) { spotify: function(cb) { dbot.api.spotify.spotifySearch(term, function(body, url, uri) { if(body) { - cb(null, { url:url, uri:uri }); + if (!dbot.modules.minify) { + cb(null, { url: url, uri:uri }); + } else { + dbot.modules.minify.api.minify(url, "bitly", function(mini) { + cb(null, { url:mini || url, uri:uri }); + }); + } } else { cb(null, undefined); } diff --git a/modules/minify/config.json b/modules/minify/config.json new file mode 100644 index 0000000..7a42fda --- /dev/null +++ b/modules/minify/config.json @@ -0,0 +1,9 @@ +{ + "dependencies": [ ], + "ignorable": true, + "outputPrefix": "minify", + "defaultMinifier": "bitly", + + "minifier-bitly-url": "https://api-ssl.bitly.com/v3/shorten", + "minifier-bitly-access_token": "insert token here" +} \ No newline at end of file diff --git a/modules/minify/minify.js b/modules/minify/minify.js new file mode 100644 index 0000000..f2bc9cb --- /dev/null +++ b/modules/minify/minify.js @@ -0,0 +1,116 @@ +/** + * Name: Minify + * Description: Provides url minifier functionality via one of a number of URL Minifier services +*/ + +var request = require('request'), + _ = require('underscore')._; + +var minify = function(dbot) { + + // This is where you provide support for new minifiers + // callback(miniURL, error); + // this.config contains only the configuration options for the given minifier + // example: if your minifier is "bitly" then this.config.myvalue = config.json entry "minifiers-bitly-myvalue" + this.minifiers = { + 'bitly': function(url, callback) { + request({ + 'url': this.config.url, + 'qs': { + 'access_token': this.config.access_token, + 'longUrl': encodeURI(url) + }, + 'json': true + }, function(error, response, body) { + if(!error && response.statusCode == 200) { + if (body.status_code == 200) { + callback(body.data.url); + } else { + callback(false, body.status_txt); + } + } else { + callback(false, error); + } + }); + } + }; + + + // API + // minify(url, minifier, callback(miniUrl, error)) + // calls 'callback' with a minified url string, or false. if 'false' the error parameter may be populated. + // if minifier is undefined then the default minifier is used + + this.api = { + 'minify': function(url, minifier, callback) { + if (typeof minifier === "function") { + callback = minifier; + minifier = this.config.defaultMinifier; + } + + if (!minifier) minifier = this.config.defaultMinifier; + + var mf = this.minifiers[minifier.trim()]; + if (!mf) { + // specified minifier does not exist + callback(false, "minifier_not_found"); + return; + } + + mf(url, callback) + } + }; + + + // Commands + // ~minify URL + // returns a minified URL using the default minifier + // ex: "~minify http://google.com" + // ~minify minifier URL + // returns a minified URL using the specified minifier + // ex: "~minify goo.gl http://google.com" + // ex: "~minify bit.ly http://google.com" + + + this.commands = { + '~minify': function(event) { + this.api.minify(event.input[2].trim(), event.input[1], function(mUrl, error) { + if (mUrl) { + event.reply(dbot.t('success', { 'miniurl': mUrl })); + } else { + if(error == "minifier_not_found") { + event.reply(dbot.t('fail-bad-minimizer')); + } else { + event.reply(dbot.t('fail', { 'reason': error })); + } + } + }); + } + }; + + this.commands['~minify'].regex = [/^minify ([\d\w-]+[\d\w\s-]*[ ])?(.+)$/, 3]; + + this.onLoad = function() { + this.minifiers = _.mapObject(this.minifiers, function(m, mName) { + var minifierConfig = _.pick(this.config, function(v,k) { return k.startsWith("minifier-" + mName + "-"); }); + minifierConfig = _.reduce(minifierConfig, function(r,v,k) { + var tlk = "minifier-" + mName + "-"; + var tlkl = tlk.length; + var nk = k.slice(tlkl); + r[nk] = v; + return r; + }, { }); + + var minifier = { + name: mName, + config: minifierConfig + } + + return m.bind(minifier); + }.bind(this)); + }.bind(this); +} + +exports.fetch = function(dbot) { + return new minify(dbot); +}; \ No newline at end of file diff --git a/modules/minify/strings.json b/modules/minify/strings.json new file mode 100644 index 0000000..758bebd --- /dev/null +++ b/modules/minify/strings.json @@ -0,0 +1,11 @@ +{ + "success": { + "en" : "Here you go! -> {miniurl}" + }, + "fail-bad-minimizer": { + "en" : "The specified minimizer is not supported." + }, + "fail" : { + "en" : "I couldn't. =( [{reason}]" + } +} \ No newline at end of file diff --git a/modules/spotify/spotify.js b/modules/spotify/spotify.js index 419c31b..3829326 100644 --- a/modules/spotify/spotify.js +++ b/modules/spotify/spotify.js @@ -91,6 +91,14 @@ var spotify = function(dbot) { } }); }.bind(this)); + }, + + 'getMinifiedSpotifyLink': function(link, callback) { + if(!dbot.modules.minify) { + callback(); + } else { + dbot.modules.minify.api.minify(link, "bitly", callback); + } } }; @@ -99,14 +107,16 @@ var spotify = function(dbot) { var query = event.input[1]; this.api.spotifySearch(query, function(body, t) { if(body) { - event.reply(dbot.t('found', { - 'artist': _.map(body.tracks.items[0].artists, function(a) { - return a.name }).join(', '), - 'album': body.tracks.items[0].album.name, - 'track': body.tracks.items[0].name, - 'url': t, - 'uri': body.tracks.items[0].uri - })); + this.api.getMinifiedSpotifyLink(t, function(mini) { + event.reply(dbot.t('found', { + 'artist': _.map(body.tracks.items[0].artists, function(a) { + return a.name }).join(', '), + 'album': body.tracks.items[0].album.name, + 'track': body.tracks.items[0].name, + 'url': mini || t, + 'uri': body.tracks.items[0].uri + })); + }); } else { event.reply(dbot.t('not-found')); } @@ -118,20 +128,22 @@ var spotify = function(dbot) { if(!_.isUndefined(event.params[1])) { lastLink = event.params[1]; } - + if(lastLink.match(this.youtubeRegex)) { dbot.api.link.getTitle(lastLink, function(title) { name = title.replace(' - YouTube', ''); this.api.spotifySearch(name, function(body, t) { if(body) { - event.reply(dbot.t('found', { - 'artist': _.map(body.tracks.items[0].artists, - function(a) { return a.name }).join(', '), - 'album': body.tracks.items[0].album.name, - 'track': body.tracks.items[0].name, - 'url': t, - 'uri': body.tracks.items[0].uri - })); + this.api.getMinifiedSpotifyLink(t, function(mini) { + event.reply(dbot.t('found', { + 'artist': _.map(body.tracks.items[0].artists, + function(a) { return a.name }).join(', '), + 'album': body.tracks.items[0].album.name, + 'track': body.tracks.items[0].name, + 'url': mini || t, + 'uri': body.tracks.items[0].uri + })); + }); } else { event.reply(dbot.t('not-found')); } From 43cfe3b637a718e34fc2c92979e2f67d1fecddc2 Mon Sep 17 00:00:00 2001 From: Scritches Date: Mon, 26 Mar 2018 16:23:03 -0400 Subject: [PATCH 2/2] Minify module basic framework for more human-readable error responses from the bitly minifier --- modules/minify/minify.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/modules/minify/minify.js b/modules/minify/minify.js index f2bc9cb..ee72094 100644 --- a/modules/minify/minify.js +++ b/modules/minify/minify.js @@ -26,7 +26,14 @@ var minify = function(dbot) { if (body.status_code == 200) { callback(body.data.url); } else { - callback(false, body.status_txt); + var emsg = function() { + switch(body.status_txt) { + case "INVALID_URI": return "Invalid URL was supplied"; + default: return body.status_txt; + } + } + + callback(false, emsg); } } else { callback(false, error);