combine.fm/lib/spotify.js

76 lines
1.9 KiB
JavaScript
Raw Normal View History

2014-12-02 11:46:39 +00:00
"use strict";
2014-11-30 12:21:03 +00:00
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];
if (type == "album") {
next({
service: "spotify",
type: type,
id: data.id,
name: data.name,
url: "https://play.spotify.com/" + type + "/" + data.id,
artwork: data.images ? data.images[0].url.replace("http:", "") : data.album.images[0].url.replace("http:", ""),
artist: {
name: artist.name
}
});
} else if (type == "track") {
next({
service: "spotify",
type: type,
id: data.id,
name: data.name,
url: "https://play.spotify.com/" + type + "/" + data.id,
artwork: data.images ? data.images[0].url.replace("http:", "") : data.album.images[0].url.replace("http:", ""),
artist: {
name: artist.name
},
album: {
name: data.album.name
}
})
}
2014-11-30 12:21:03 +00:00
});
}
module.exports.search = function(data, next) {
var query = "";
var type = data.type;
if (type == "album") {
query = data.artist.name + " " + data.name;
} else if (type == "track") {
query = data.artist.name + " " + data.album.name + " " + data.name;
}
query = query.replace(":", "");
2014-11-30 12:21:03 +00:00
spotify.search({query: query, type: type}, function(err, data) {
if ( err ) {
console.log('Error occurred: ' + err);
return;
}
2014-12-02 01:35:10 +00:00
var item = data[type + "s"].items[0];
2014-11-30 12:21:03 +00:00
2014-12-02 01:35:10 +00:00
module.exports.lookupId(item.id, type, next);
2014-11-30 12:21:03 +00:00
});
}
module.exports.parseUrl = function(url, next) {
2014-12-02 01:35:10 +00:00
var matches = parse(url).path.match(/\/(album|track)[\/]+([^\/]+)/);
2014-11-30 12:21:03 +00:00
2014-12-02 01:35:10 +00:00
if (matches && matches[2]) {
module.exports.lookupId(matches[2], matches[1], next);
2014-11-30 12:21:03 +00:00
}
}