combine.fm/lib/services/spotify.js

108 lines
3.1 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');
var Q = require('q');
2014-11-30 12:21:03 +00:00
module.exports.id = "spotify";
module.exports.match = function(url, type) {
var parsed = parse(url);
return parsed.host.match(/spotify\.com$/);
};
module.exports.lookupId = function(id, type) {
var deferred = Q.defer();
2014-11-30 12:21:03 +00:00
spotify.lookup({id: id, type: type}, function(err, data) {
2014-12-05 18:03:45 +00:00
if ( err || data.error) {
var error = new Error("Not Found");
error.status = 404;
deferred.reject(error);
2014-11-30 12:21:03 +00:00
return;
}
var artist = data.artists[0];
if (type == "album") {
deferred.resolve({
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,
artwork: data.images ? data.images[1].url.replace("http:", "https:") : data.album.images[1].url.replace("http:", "https:"),
artist: {
name: artist.name
}
});
} else if (type == "track") {
deferred.resolve({
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,
artwork: data.images ? data.images[1].url.replace("http:", "https:") : data.album.images[1].url.replace("http:", "https:"),
artist: {
name: artist.name
},
album: {
name: data.album.name
}
})
}
2014-11-30 12:21:03 +00:00
});
return deferred.promise;
2014-11-30 12:21:03 +00:00
}
module.exports.search = function(data) {
var deferred = Q.defer();
var query, album;
var type = data.type;
if (type == "album") {
2014-12-03 23:32:49 +00:00
query = "artist:" + data.artist.name.replace(":", "") + " album:" + data.name.replace(":", "");
album = data.name;
} else if (type == "track") {
2014-12-03 23:32:49 +00:00
query = "artist:" + data.artist.name.replace(":", "") + " album:" + data.album.name.replace(":", "") + " track:" + data.name.replace(":", "");
album = data.album.name;
}
2014-12-06 21:39:15 +00:00
spotify.search({query: query, type: type}, function(err, results) {
2014-11-30 12:21:03 +00:00
if ( err ) {
deferred.resolve({service: "spotify"});
2014-11-30 12:21:03 +00:00
return;
}
2014-12-06 21:39:15 +00:00
if (!results[type + "s"].items[0]) {
var matches = album.match(/^[^\(\[]+/);
2014-12-05 22:20:17 +00:00
if (matches[0] && matches[0] != album) {
var cleanedData = JSON.parse(JSON.stringify(data));
if (type == "album") {
cleanedData.name = matches[0].trim();
} else if (type == "track") {
2014-12-05 22:26:12 +00:00
cleanedData.album.name = matches[0].trim();
}
module.exports.search(cleanedData).then(deferred.resolve);
} else {
deferred.resolve({service: "spotify"});
}
} else {
module.exports.lookupId(data[type + "s"].items[0].id, type).then(deferred.resolve);
2014-12-04 16:29:39 +00:00
}
2014-11-30 12:21:03 +00:00
});
return deferred.promise;
2014-11-30 12:21:03 +00:00
}
module.exports.parseUrl = function(url) {
var deferred = Q.defer();
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]).then(deferred.resolve);
2014-11-30 12:21:03 +00:00
}
return deferred.promise;
2014-11-30 12:21:03 +00:00
}