mirror of
https://github.com/reality/dbot.git
synced 2024-11-23 12:29:26 +01:00
commit
48728c5801
@ -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);
|
||||
}
|
||||
|
9
modules/minify/config.json
Normal file
9
modules/minify/config.json
Normal 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
123
modules/minify/minify.js
Normal 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);
|
||||
};
|
11
modules/minify/strings.json
Normal file
11
modules/minify/strings.json
Normal 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}]"
|
||||
}
|
||||
}
|
@ -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'));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user