3
0
mirror of https://github.com/reality/dbot.git synced 2024-12-28 05:32:50 +01:00

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)
This commit is contained in:
Scritches 2018-03-26 13:52:06 -04:00
parent ff7be1f31d
commit 19cc4c474d
5 changed files with 172 additions and 18 deletions

View File

@ -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);
}

View File

@ -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"
}

116
modules/minify/minify.js Normal file
View File

@ -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);
};

View File

@ -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}]"
}
}

View File

@ -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'));
}