2013-05-21 16:52:46 +02:00
|
|
|
/**
|
|
|
|
* Name: Spotify
|
|
|
|
* Description: Various Spotify functionality
|
|
|
|
*/
|
2013-02-12 20:14:27 +01:00
|
|
|
var request = require('request'),
|
2013-05-21 16:52:46 +02:00
|
|
|
_ = require('underscore')._;
|
2013-02-12 20:14:27 +01:00
|
|
|
|
|
|
|
var spotify = function(dbot) {
|
2013-05-21 16:52:46 +02:00
|
|
|
/* Examples:
|
|
|
|
* http://open.spotify.com/track/42SYMWISn7xUpTNPLw9V5E
|
|
|
|
* spotify:track:42SYMWISn7xUpTNPLw9V5E
|
|
|
|
* http://open.spotify.com/artist/3yY2gUcIsjMr8hjo51PoJ8
|
|
|
|
* spotify:artist:3yY2gUcIsjMr8hjo51PoJ8
|
|
|
|
* http://open.spotify.com/album/30g571JKoxs8AnsgAViV2J
|
|
|
|
* spotify:album:30g571JKoxs8AnsgAViV2J
|
|
|
|
*/
|
|
|
|
this.spotifyRegex = /(\b(https?:\/\/open.spotify.com\/(artist|track|album)\/\w*|spotify:(artist|track|album):\w*)\b)/ig;
|
|
|
|
this.spotifyLookup = 'http://ws.spotify.com/lookup/1/.json';
|
2016-12-05 21:04:37 +01:00
|
|
|
this.spotifySearch = 'https://api.spotify.com/v1/search';
|
2018-03-08 22:11:12 +01:00
|
|
|
this.youtubeRegex = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/;
|
2013-05-21 16:52:46 +02:00
|
|
|
this.spotifyText = '\u00039spotify\u000f';
|
2018-03-22 08:04:14 +01:00
|
|
|
this.spotifyAuthUrl = 'https://accounts.spotify.com/api/token';
|
|
|
|
|
2018-03-24 21:20:33 +01:00
|
|
|
this.auth = false;
|
|
|
|
|
2018-03-22 08:04:14 +01:00
|
|
|
this.authenticate = function(callback) {
|
2018-03-24 21:20:33 +01:00
|
|
|
this.auth = this.auth || new Buffer(this.config.api_key_clientid + ":" + this.config.api_key_clientsecret).toString("base64");
|
|
|
|
|
2018-03-22 08:04:14 +01:00
|
|
|
request({
|
|
|
|
url: this.spotifyAuthUrl,
|
|
|
|
method: "POST",
|
2018-03-24 21:20:33 +01:00
|
|
|
headers: { Authorization: "Basic " + this.auth },
|
2018-03-22 08:04:14 +01:00
|
|
|
form: { grant_type: "client_credentials" }
|
|
|
|
}, function(error, response, body) {
|
|
|
|
if (!error && response.statusCode == 200) {
|
|
|
|
body = JSON.parse(body);
|
|
|
|
var token = body.access_token;
|
|
|
|
callback(token);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2013-02-12 20:14:27 +01:00
|
|
|
|
2013-07-25 23:56:44 +02:00
|
|
|
this.lookup = function(link, callback) {
|
2018-03-22 08:04:14 +01:00
|
|
|
this.authenticate(function(token) {
|
|
|
|
request({
|
|
|
|
'url': this.spotifyLookup,
|
|
|
|
'qs': { 'uri': link },
|
|
|
|
'json': true,
|
|
|
|
'headers': { 'Authorization': "Bearer " + token }
|
|
|
|
}, function(error, response, body) {
|
|
|
|
if(!error && response.statusCode == 200) {
|
|
|
|
if(_.has(body, 'track')) {
|
|
|
|
callback(dbot.t('track', {
|
|
|
|
'artist': _.map(body.track.artists,
|
|
|
|
function(a) { return a.name }).join(', '),
|
|
|
|
'album': body.track.album.name,
|
|
|
|
'track': body.track.name
|
|
|
|
}));
|
|
|
|
} else if(_.has(body, 'album')) {
|
|
|
|
callback(dbot.t('album', {
|
|
|
|
'artist': body.album.artist,
|
|
|
|
'album': body.album.name
|
|
|
|
}));
|
|
|
|
} else if(_.has(body, 'artist')) {
|
|
|
|
callback(dbot.t('artist', {
|
|
|
|
'artist': body.artist.name
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}.bind(this));
|
2013-05-21 16:52:46 +02:00
|
|
|
};
|
2013-02-12 20:14:27 +01:00
|
|
|
|
2013-05-21 16:52:46 +02:00
|
|
|
this.api = {
|
|
|
|
'spotifySearch': function(query, callback) {
|
2018-03-08 22:11:12 +01:00
|
|
|
this.authenticate(function(token) {
|
2018-03-22 08:04:14 +01:00
|
|
|
request({
|
|
|
|
'url': this.spotifySearch,
|
|
|
|
'qs': { 'q': query, 'type': 'track' },
|
|
|
|
'json': true,
|
|
|
|
'headers': { 'Authorization': "Bearer " + token }
|
|
|
|
}, function(error, response, body) {
|
|
|
|
if(!error && response.statusCode == 200) {
|
|
|
|
if(_.has(body, 'tracks') && body.tracks.items[0] && _.has(body.tracks.items[0], 'href')) {
|
2018-03-24 21:20:33 +01:00
|
|
|
var url = body.tracks.items[0].href;
|
|
|
|
url = url.replace(/api.spotify.com\/v1\/tracks/, 'open.spotify.com/track');
|
|
|
|
callback(body, url, body.tracks.items[0].uri);
|
2018-03-22 08:04:14 +01:00
|
|
|
} else {
|
|
|
|
callback(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}.bind(this));
|
2018-03-26 19:52:06 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
'getMinifiedSpotifyLink': function(link, callback) {
|
|
|
|
if(!dbot.modules.minify) {
|
|
|
|
callback();
|
|
|
|
} else {
|
|
|
|
dbot.modules.minify.api.minify(link, "bitly", callback);
|
|
|
|
}
|
2013-05-20 17:05:00 +02:00
|
|
|
}
|
2013-05-21 16:52:46 +02:00
|
|
|
};
|
2013-05-20 17:05:00 +02:00
|
|
|
|
2013-05-21 16:52:46 +02:00
|
|
|
var commands = {
|
|
|
|
'~spotify': function(event) {
|
|
|
|
var query = event.input[1];
|
|
|
|
this.api.spotifySearch(query, function(body, t) {
|
|
|
|
if(body) {
|
2018-03-26 19:52:06 +02:00
|
|
|
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
|
|
|
|
}));
|
|
|
|
});
|
2013-05-21 16:52:46 +02:00
|
|
|
} else {
|
2013-05-21 19:13:19 +02:00
|
|
|
event.reply(dbot.t('not-found'));
|
2013-05-21 16:52:46 +02:00
|
|
|
}
|
2013-05-20 17:05:00 +02:00
|
|
|
}.bind(this));
|
2013-05-21 16:52:46 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
'~syt': function(event) {
|
|
|
|
var lastLink = dbot.modules.link.links[event.channel.name];
|
|
|
|
if(!_.isUndefined(event.params[1])) {
|
|
|
|
lastLink = event.params[1];
|
|
|
|
}
|
2018-03-26 19:52:06 +02:00
|
|
|
|
2018-03-08 22:11:12 +01:00
|
|
|
if(lastLink.match(this.youtubeRegex)) {
|
2013-05-21 16:52:46 +02:00
|
|
|
dbot.api.link.getTitle(lastLink, function(title) {
|
2018-03-08 22:11:12 +01:00
|
|
|
name = title.replace(' - YouTube', '');
|
2013-05-21 16:52:46 +02:00
|
|
|
this.api.spotifySearch(name, function(body, t) {
|
2018-03-22 08:04:14 +01:00
|
|
|
if(body) {
|
2018-03-26 19:52:06 +02:00
|
|
|
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
|
|
|
|
}));
|
|
|
|
});
|
2013-05-21 16:52:46 +02:00
|
|
|
} else {
|
2013-05-21 19:13:19 +02:00
|
|
|
event.reply(dbot.t('not-found'));
|
2013-05-21 16:52:46 +02:00
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
}.bind(this));
|
|
|
|
} else {
|
|
|
|
event.reply('That\'s not a YouTube link');
|
|
|
|
}
|
2013-05-20 17:05:00 +02:00
|
|
|
}
|
2013-05-21 16:52:46 +02:00
|
|
|
};
|
2013-12-29 19:38:24 +01:00
|
|
|
commands['~spotify'].regex = [/^spotify (.*)/, 2];
|
2013-05-21 16:52:46 +02:00
|
|
|
this.commands = commands;
|
2013-04-12 21:17:22 +02:00
|
|
|
|
2013-05-21 16:52:46 +02:00
|
|
|
this.onLoad = function() {
|
2013-07-25 23:56:44 +02:00
|
|
|
dbot.api.link.addHandler(this.name, this.spotifyRegex, function(matches, name, callback) {
|
|
|
|
this.lookup(matches[0], callback);
|
2013-05-21 16:52:46 +02:00
|
|
|
}.bind(this));
|
|
|
|
}.bind(this);
|
2013-02-12 20:14:27 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.fetch = function(dbot) {
|
2013-05-21 16:52:46 +02:00
|
|
|
return new spotify(dbot);
|
2013-02-12 20:14:27 +01:00
|
|
|
};
|