fixed tabs/spaces and added spotify uri for desktop app linkability

This commit is contained in:
Scritches 2018-03-22 03:04:14 -04:00
parent 217e65df96
commit 10c642c94f
2 changed files with 76 additions and 75 deletions

View File

@ -19,81 +19,81 @@ var spotify = function(dbot) {
this.spotifySearch = 'https://api.spotify.com/v1/search'; this.spotifySearch = 'https://api.spotify.com/v1/search';
this.youtubeRegex = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/; this.youtubeRegex = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/;
this.spotifyText = '\u00039spotify\u000f'; this.spotifyText = '\u00039spotify\u000f';
this.spotifyAuthUrl = 'https://accounts.spotify.com/api/token'; this.spotifyAuthUrl = 'https://accounts.spotify.com/api/token';
// ClientID and ClientSecret come from the spotify developer center; you will need to supply your own. // ClientID and ClientSecret come from the spotify developer center; you will need to supply your own.
this.spotifyClientID = 'e2491c50879a4d7f900dcefcc74b7c90'; this.spotifyClientID = 'e2491c50879a4d7f900dcefcc74b7c90';
this.spotifyClientSecret = 'b29da299612e4e659099ab3367ffa3f4'; this.spotifyClientSecret = 'b29da299612e4e659099ab3367ffa3f4';
this.spotifyAuth = new Buffer(this.spotifyClientID + ":" + this.spotifyClientSecret).toString("base64"); this.spotifyAuth = new Buffer(this.spotifyClientID + ":" + this.spotifyClientSecret).toString("base64");
this.authenticate = function(callback) { this.authenticate = function(callback) {
request({ request({
url: this.spotifyAuthUrl, url: this.spotifyAuthUrl,
method: "POST", method: "POST",
headers: { Authorization: "Basic " + this.spotifyAuth }, headers: { Authorization: "Basic " + this.spotifyAuth },
form: { grant_type: "client_credentials" } form: { grant_type: "client_credentials" }
}, function(error, response, body) { }, function(error, response, body) {
if (!error && response.statusCode == 200) { if (!error && response.statusCode == 200) {
body = JSON.parse(body); body = JSON.parse(body);
var token = body.access_token; var token = body.access_token;
callback(token); callback(token);
} }
}); });
}; };
this.lookup = function(link, callback) { this.lookup = function(link, callback) {
this.authenticate(function(token) { this.authenticate(function(token) {
request({ request({
'url': this.spotifyLookup, 'url': this.spotifyLookup,
'qs': { 'uri': link }, 'qs': { 'uri': link },
'json': true, 'json': true,
'headers': { 'Authorization': "Bearer " + token } 'headers': { 'Authorization': "Bearer " + token }
}, function(error, response, body) { }, function(error, response, body) {
if(!error && response.statusCode == 200) { if(!error && response.statusCode == 200) {
if(_.has(body, 'track')) { if(_.has(body, 'track')) {
callback(dbot.t('track', { callback(dbot.t('track', {
'artist': _.map(body.track.artists, 'artist': _.map(body.track.artists,
function(a) { return a.name }).join(', '), function(a) { return a.name }).join(', '),
'album': body.track.album.name, 'album': body.track.album.name,
'track': body.track.name 'track': body.track.name
})); }));
} else if(_.has(body, 'album')) { } else if(_.has(body, 'album')) {
callback(dbot.t('album', { callback(dbot.t('album', {
'artist': body.album.artist, 'artist': body.album.artist,
'album': body.album.name 'album': body.album.name
})); }));
} else if(_.has(body, 'artist')) { } else if(_.has(body, 'artist')) {
callback(dbot.t('artist', { callback(dbot.t('artist', {
'artist': body.artist.name 'artist': body.artist.name
})); }));
} }
} }
}); });
}.bind(this)); }.bind(this));
}; };
this.api = { this.api = {
'spotifySearch': function(query, callback) { 'spotifySearch': function(query, callback) {
this.authenticate(function(token) { this.authenticate(function(token) {
request({ request({
'url': this.spotifySearch, 'url': this.spotifySearch,
'qs': { 'q': query, 'type': 'track' }, 'qs': { 'q': query, 'type': 'track' },
'json': true, 'json': true,
'headers': { 'Authorization': "Bearer " + token } 'headers': { 'Authorization': "Bearer " + token }
}, function(error, response, body) { }, function(error, response, body) {
if(!error && response.statusCode == 200) { if(!error && response.statusCode == 200) {
if(_.has(body, 'tracks') && body.tracks.items[0] && _.has(body.tracks.items[0], 'href')) { if(_.has(body, 'tracks') && body.tracks.items[0] && _.has(body.tracks.items[0], 'href')) {
var t = body.tracks.items[0].href; var t = body.tracks.items[0].href;
///*t = t.replace(/:/g, '/'); ///*t = t.replace(/:/g, '/');
t = t.replace(/api.spotify.com\/v1\/tracks/, t = t.replace(/api.spotify.com\/v1\/tracks/,
'open.spotify.com/track'); 'open.spotify.com/track');
callback(body, t); callback(body, t);
} else { } else {
callback(false); callback(false);
} }
} }
}); });
}.bind(this)); }.bind(this));
} }
}; };
@ -107,7 +107,8 @@ var spotify = function(dbot) {
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': t,
'uri': body.tracks.items[0].uri
})); }));
} else { } else {
event.reply(dbot.t('not-found')); event.reply(dbot.t('not-found'));
@ -125,8 +126,8 @@ var spotify = function(dbot) {
dbot.api.link.getTitle(lastLink, function(title) { dbot.api.link.getTitle(lastLink, function(title) {
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) {
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,

View File

@ -21,11 +21,11 @@
"it": "[Track] {artist} {track} (dall' album {album})" "it": "[Track] {artist} {track} (dall' album {album})"
}, },
"found": { "found": {
"en": "[{url}] {artist} {track} (from {album})", "en": "[{url} - {uri}] {artist} {track} (from {album})",
"cy": "[{url}] {artist} {track} (o'r albwm: {album})", "cy": "[{url} - {uri}] {artist} {track} (o'r albwm: {album})",
"de": "[{url}] {artist} {track} (aus dem Album {album})", "de": "[{url} - {uri}] {artist} {track} (aus dem Album {album})",
"fr": "[{url}] {artist} {track} (tiré de {album})", "fr": "[{url} - {uri}] {artist} {track} (tiré de {album})",
"it": "[{url}] {artist} {track} (dall' album {album})" "it": "[{url} - {uri}] {artist} {track} (dall' album {album})"
}, },
"not-found": { "not-found": {
"en": "No results.", "en": "No results.",