3
0
mirror of https://github.com/reality/dbot.git synced 2024-11-27 06:19:24 +01:00

Merge pull request #670 from Scritches/master

New minify module
This commit is contained in:
Luke Slater 2018-03-28 21:52:55 +01:00 committed by GitHub
commit 48728c5801
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 179 additions and 18 deletions

View File

@ -297,7 +297,13 @@ var lastfm = function(dbot) {
spotify: function(cb) { spotify: function(cb) {
dbot.api.spotify.spotifySearch(term, function(body, url, uri) { dbot.api.spotify.spotifySearch(term, function(body, url, uri) {
if(body) { 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 { } else {
cb(null, undefined); 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"
}

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

@ -0,0 +1,123 @@
/**
* 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 {
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);
}
});
}
};
// 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)); }.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]; var query = event.input[1];
this.api.spotifySearch(query, function(body, t) { this.api.spotifySearch(query, function(body, t) {
if(body) { if(body) {
this.api.getMinifiedSpotifyLink(t, function(mini) {
event.reply(dbot.t('found', { event.reply(dbot.t('found', {
'artist': _.map(body.tracks.items[0].artists, function(a) { 'artist': _.map(body.tracks.items[0].artists, function(a) {
return a.name }).join(', '), return a.name }).join(', '),
'album': body.tracks.items[0].album.name, 'album': body.tracks.items[0].album.name,
'track': body.tracks.items[0].name, 'track': body.tracks.items[0].name,
'url': t, 'url': mini || t,
'uri': body.tracks.items[0].uri 'uri': body.tracks.items[0].uri
})); }));
});
} else { } else {
event.reply(dbot.t('not-found')); event.reply(dbot.t('not-found'));
} }
@ -124,14 +134,16 @@ var spotify = function(dbot) {
name = title.replace(' - YouTube', ''); name = title.replace(' - YouTube', '');
this.api.spotifySearch(name, function(body, t) { this.api.spotifySearch(name, function(body, t) {
if(body) { if(body) {
this.api.getMinifiedSpotifyLink(t, function(mini) {
event.reply(dbot.t('found', { event.reply(dbot.t('found', {
'artist': _.map(body.tracks.items[0].artists, 'artist': _.map(body.tracks.items[0].artists,
function(a) { return a.name }).join(', '), function(a) { return a.name }).join(', '),
'album': body.tracks.items[0].album.name, 'album': body.tracks.items[0].album.name,
'track': body.tracks.items[0].name, 'track': body.tracks.items[0].name,
'url': t, 'url': mini || t,
'uri': body.tracks.items[0].uri 'uri': body.tracks.items[0].uri
})); }));
});
} else { } else {
event.reply(dbot.t('not-found')); event.reply(dbot.t('not-found'));
} }