combine.fm/lib/spotify.js
2014-11-30 16:58:25 +00:00

49 lines
1.2 KiB
JavaScript

var parse = require('url').parse;
var spotify = require('spotify');
module.exports.lookupId = function(id, type, next) {
spotify.lookup({id: id, type: type}, function(err, data) {
if ( err ) {
console.log('Error occurred: ' + err);
return;
}
var artist = data.artists[0];
next({
id: data.id,
name: data.name,
url: "https://play.spotify.com/album/" + data.id,
artwork: data.images[0].url.replace("http:", ""),
artist: {
name: artist.name
}
})
});
}
module.exports.search = function(query, type, next) {
spotify.search({query: query, type: type}, function(err, data) {
if ( err ) {
console.log('Error occurred: ' + err);
return;
}
album = data.albums.items[0];
module.exports.lookupId(album.id, "album", next);
});
}
module.exports.parseUrl = function(url, next) {
// https://play.spotify.com/album/3W3ENDBQMJ9bD2qmxWI2f0
// https://play.spotify.com/track/3W3ENDBQMJ9bD2qmxWI2f0
// https://open.spotify.com/album/3W3ENDBQMJ9bD2qmxWI2f0
// https://open.spotify.com/track/3W3ENDBQMJ9bD2qmxWI2f0
var matches = parse(url).path.match(/\/album[\/]+([^\/]+)/);
if (matches && matches[1]) {
return next({id:matches[1], type: "album"})
}
}