forked from GitHub/dbot
Added necessary authentication for spotify API calls and fixed youtube link regex.
This commit is contained in:
parent
03067f97d6
commit
217e65df96
@ -17,15 +17,37 @@ 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.youtubeMiniRegex = /^https?:\/\/youtu\.be\/\w+$/;
|
|
||||||
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) {
|
||||||
|
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 }
|
||||||
}, 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')) {
|
||||||
@ -47,14 +69,17 @@ var spotify = function(dbot) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}.bind(this));
|
||||||
};
|
};
|
||||||
|
|
||||||
this.api = {
|
this.api = {
|
||||||
'spotifySearch': function(query, callback) {
|
'spotifySearch': function(query, callback) {
|
||||||
|
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 }
|
||||||
}, 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')) {
|
||||||
@ -68,6 +93,7 @@ var spotify = function(dbot) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}.bind(this));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -95,16 +121,16 @@ var spotify = function(dbot) {
|
|||||||
lastLink = event.params[1];
|
lastLink = event.params[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
if(lastLink.match(this.youtubeRegex) | lastLink.match(this.youtubeMiniRegex)) {
|
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 {
|
||||||
|
Loading…
Reference in New Issue
Block a user