Merge pull request #665 from Scritches/master

Added support for the minified youtube links returned by the ~yt command to the ~syt command
This commit is contained in:
Luke Slater 2018-03-08 21:25:03 +00:00 committed by GitHub
commit dde0200032
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -17,56 +17,83 @@ var spotify = function(dbot) {
this.spotifyRegex = /(\b(https?:\/\/open.spotify.com\/(artist|track|album)\/\w*|spotify:(artist|track|album):\w*)\b)/ig; 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'; this.spotifyLookup = 'http://ws.spotify.com/lookup/1/.json';
this.spotifySearch = 'https://api.spotify.com/v1/search'; this.spotifySearch = 'https://api.spotify.com/v1/search';
this.youtubeRegex = /^http:\/\/(?:www\.)?youtube.com\/watch\?v=\w+(&\S*)?$/ 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';
// ClientID and ClientSecret come from the spotify developer center; you will need to supply your own.
this.spotifyClientID = 'e2491c50879a4d7f900dcefcc74b7c90';
this.spotifyClientSecret = 'b29da299612e4e659099ab3367ffa3f4';
this.spotifyAuth = new Buffer(this.spotifyClientID + ":" + this.spotifyClientSecret).toString("base64");
this.authenticate = function(callback) {
request({
url: this.spotifyAuthUrl,
method: "POST",
headers: { Authorization: "Basic " + this.spotifyAuth },
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);
}
});
};
this.lookup = function(link, callback) { this.lookup = function(link, callback) {
request({ this.authenticate(function(token) {
'url': this.spotifyLookup, request({
'qs': { 'uri': link }, 'url': this.spotifyLookup,
'json': true 'qs': { 'uri': link },
}, function(error, response, body) { 'json': true,
if(!error && response.statusCode == 200) { 'headers': { 'Authorization': "Bearer " + token }
if(_.has(body, 'track')) { }, function(error, response, body) {
callback(dbot.t('track', { if(!error && response.statusCode == 200) {
'artist': _.map(body.track.artists, if(_.has(body, 'track')) {
function(a) { return a.name }).join(', '), callback(dbot.t('track', {
'album': body.track.album.name, 'artist': _.map(body.track.artists,
'track': body.track.name function(a) { return a.name }).join(', '),
})); 'album': body.track.album.name,
} else if(_.has(body, 'album')) { 'track': body.track.name
callback(dbot.t('album', { }));
'artist': body.album.artist, } else if(_.has(body, 'album')) {
'album': body.album.name callback(dbot.t('album', {
})); 'artist': body.album.artist,
} else if(_.has(body, 'artist')) { 'album': body.album.name
callback(dbot.t('artist', { }));
'artist': body.artist.name } else if(_.has(body, 'artist')) {
})); callback(dbot.t('artist', {
} 'artist': body.artist.name
} }));
}); }
}
});
}.bind(this));
}; };
this.api = { this.api = {
'spotifySearch': function(query, callback) { 'spotifySearch': function(query, callback) {
request({ this.authenticate(function(token) {
'url': this.spotifySearch, request({
'qs': { 'q': query, 'type': 'track' }, 'url': this.spotifySearch,
'json': true 'qs': { 'q': query, 'type': 'track' },
}, function(error, response, body) { 'json': true,
if(!error && response.statusCode == 200) { 'headers': { 'Authorization': "Bearer " + token }
if(_.has(body, 'tracks') && body.tracks.items[0] && _.has(body.tracks.items[0], 'href')) { }, function(error, response, body) {
var t = body.tracks.items[0].href; if(!error && response.statusCode == 200) {
///*t = t.replace(/:/g, '/'); if(_.has(body, 'tracks') && body.tracks.items[0] && _.has(body.tracks.items[0], 'href')) {
t = t.replace(/api.spotify.com\/v1\/tracks/, var t = body.tracks.items[0].href;
'open.spotify.com/track'); ///*t = t.replace(/:/g, '/');
callback(body, t); t = t.replace(/api.spotify.com\/v1\/tracks/,
} else { 'open.spotify.com/track');
callback(false); callback(body, t);
} } else {
} callback(false);
}); }
}
});
}.bind(this));
} }
}; };
@ -96,14 +123,14 @@ var spotify = function(dbot) {
if(lastLink.match(this.youtubeRegex)) { if(lastLink.match(this.youtubeRegex)) {
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[0].artists, 'artist': _.map(body.tracks.items[0].artists,
function(a) { return a.name }).join(', '), function(a) { return a.name }).join(', '),
'album': body.tracks[0].album.name, 'album': body.tracks.items[0].album.name,
'track': body.tracks[0].name, 'track': body.tracks.items[0].name,
'url': t 'url': t
})); }));
} else { } else {