2014-12-02 11:46:39 +00:00
|
|
|
"use strict";
|
2014-11-30 12:21:03 +00:00
|
|
|
var parse = require('url').parse;
|
2014-12-13 00:00:49 +00:00
|
|
|
var Promise = require('bluebird');
|
|
|
|
var spotify = Promise.promisifyAll(require('spotify'));
|
2014-11-30 12:21:03 +00:00
|
|
|
|
2014-12-04 19:17:41 +00:00
|
|
|
module.exports.id = "spotify";
|
|
|
|
|
2014-12-11 19:53:12 +00:00
|
|
|
module.exports.match = require('./url').match;
|
|
|
|
|
|
|
|
module.exports.parseUrl = function(url) {
|
|
|
|
var matches = parse(url).path.match(/\/(album|track)[\/]+([^\/]+)/);
|
|
|
|
|
|
|
|
if (matches && matches[2]) {
|
2014-12-13 00:00:49 +00:00
|
|
|
return module.exports.lookupId(matches[2], matches[1]);
|
2014-12-11 19:53:12 +00:00
|
|
|
}
|
|
|
|
}
|
2014-12-04 19:17:41 +00:00
|
|
|
|
|
|
|
module.exports.lookupId = function(id, type) {
|
2014-12-13 00:00:49 +00:00
|
|
|
return spotify.lookupAsync({id: id, type: type}).then(function(data) {
|
|
|
|
if (data.error) {
|
2014-12-05 18:03:45 +00:00
|
|
|
var error = new Error("Not Found");
|
|
|
|
error.status = 404;
|
2014-12-13 00:00:49 +00:00
|
|
|
throw error;
|
2014-11-30 12:21:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var artist = data.artists[0];
|
|
|
|
|
2014-12-03 23:03:34 +00:00
|
|
|
if (type == "album") {
|
2014-12-13 00:00:49 +00:00
|
|
|
return {
|
2014-12-03 23:03:34 +00:00
|
|
|
service: "spotify",
|
|
|
|
type: type,
|
|
|
|
id: data.id,
|
|
|
|
name: data.name,
|
2014-12-04 21:11:55 +00:00
|
|
|
streamUrl: "https://play.spotify.com/" + type + "/" + data.id,
|
|
|
|
purchaseUrl: null,
|
2014-12-13 12:05:47 +00:00
|
|
|
artwork: {
|
|
|
|
small: data.images[1].url.replace("http:", "https:"),
|
|
|
|
large: data.images[0].url.replace("http:", "https:"),
|
|
|
|
},
|
2014-12-03 23:03:34 +00:00
|
|
|
artist: {
|
|
|
|
name: artist.name
|
|
|
|
}
|
2014-12-13 00:00:49 +00:00
|
|
|
};
|
2014-12-03 23:03:34 +00:00
|
|
|
} else if (type == "track") {
|
2014-12-13 00:00:49 +00:00
|
|
|
return {
|
2014-12-03 23:03:34 +00:00
|
|
|
service: "spotify",
|
|
|
|
type: type,
|
|
|
|
id: data.id,
|
|
|
|
name: data.name,
|
2014-12-04 21:11:55 +00:00
|
|
|
streamUrl: "https://play.spotify.com/" + type + "/" + data.id,
|
|
|
|
purchaseUrl: null,
|
2014-12-13 12:05:47 +00:00
|
|
|
artwork: {
|
|
|
|
small: data.album.images[1].url.replace("http:", "https:"),
|
|
|
|
large: data.album.images[0].url.replace("http:", "https:"),
|
|
|
|
},
|
2014-12-03 23:03:34 +00:00
|
|
|
artist: {
|
|
|
|
name: artist.name
|
|
|
|
},
|
|
|
|
album: {
|
|
|
|
name: data.album.name
|
|
|
|
}
|
2014-12-13 00:00:49 +00:00
|
|
|
};
|
2014-12-03 23:03:34 +00:00
|
|
|
}
|
2014-11-30 12:21:03 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-12-04 19:17:41 +00:00
|
|
|
module.exports.search = function(data) {
|
2015-01-27 21:32:28 +00:00
|
|
|
var cleanParam = function(str) {
|
|
|
|
var chopChars = ['&', '[', '('];
|
|
|
|
chopChars.forEach(function(chr) {
|
|
|
|
if (data.artist.name.indexOf('&') > 0) {
|
|
|
|
str = str.substring(0, data.artist.name.indexOf(chr));
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return str.replace(/[\:\?]+/, "");
|
|
|
|
}
|
2014-12-05 14:13:47 +00:00
|
|
|
var query, album;
|
2014-12-03 23:03:34 +00:00
|
|
|
var type = data.type;
|
|
|
|
|
|
|
|
if (type == "album") {
|
2015-01-27 21:32:28 +00:00
|
|
|
query = "artist:" + cleanParam(data.artist.name) + " album:" + cleanParam(data.name);
|
2014-12-05 14:13:47 +00:00
|
|
|
album = data.name;
|
2014-12-03 23:03:34 +00:00
|
|
|
} else if (type == "track") {
|
2015-01-27 21:32:28 +00:00
|
|
|
query = "artist:" + cleanParam(data.artist.name) + " track:" + cleanParam(data.name) + ( cleanParam(data.album.name).length > 0 ? " album:" + cleanParam(data.album.name): "");
|
2014-12-05 14:13:47 +00:00
|
|
|
album = data.album.name;
|
2014-12-03 23:03:34 +00:00
|
|
|
}
|
|
|
|
|
2014-12-13 00:00:49 +00:00
|
|
|
return spotify.searchAsync({query: query, type: type}).then(function(results) {
|
2014-12-06 21:39:15 +00:00
|
|
|
if (!results[type + "s"].items[0]) {
|
2015-01-27 21:32:28 +00:00
|
|
|
return {service: "spotify"};
|
|
|
|
} else {
|
|
|
|
var found;
|
|
|
|
var choppedAlbum = data.type == "album" ? cleanParam(data.name) : cleanParam(data.album.name);
|
|
|
|
if (!choppedAlbum.length) {
|
|
|
|
return module.exports.lookupId(results[type + "s"].items[0].id, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
results[type + "s"].items.forEach(function(item) {
|
|
|
|
var albumName = data.type == "album" ? item.name : item.album.name;
|
|
|
|
var matches = albumName.match(/^[^\(\[]+/);
|
|
|
|
if(choppedAlbum.indexOf(matches[0]) >= 0) {
|
|
|
|
found = item;
|
2014-12-05 14:13:47 +00:00
|
|
|
}
|
2015-01-27 21:32:28 +00:00
|
|
|
});
|
|
|
|
if (!found) {
|
2014-12-13 00:00:49 +00:00
|
|
|
return {service: "spotify"};
|
2014-12-05 14:13:47 +00:00
|
|
|
}
|
2014-12-13 00:00:49 +00:00
|
|
|
return module.exports.lookupId(results[type + "s"].items[0].id, type);
|
2014-12-04 16:29:39 +00:00
|
|
|
}
|
|
|
|
|
2014-11-30 12:21:03 +00:00
|
|
|
});
|
|
|
|
}
|