combine.fm/lib/services/spotify/index.js

99 lines
2.8 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 Promise = require('bluebird');
var spotify = Promise.promisifyAll(require('spotify'));
2014-11-30 12:21:03 +00:00
module.exports.id = "spotify";
module.exports.match = require('./url').match;
module.exports.parseUrl = function(url) {
var matches = parse(url).path.match(/\/(album|track)[\/]+([^\/]+)/);
if (matches && matches[2]) {
return module.exports.lookupId(matches[2], matches[1]);
}
}
module.exports.lookupId = function(id, type) {
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;
throw error;
2014-11-30 12:21:03 +00:00
}
var artist = data.artists[0];
if (type == "album") {
return {
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:"),
},
artist: {
name: artist.name
}
};
} else if (type == "track") {
return {
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:"),
},
artist: {
name: artist.name
},
album: {
name: data.album.name
}
};
}
2014-11-30 12:21:03 +00:00
});
}
module.exports.search = function(data) {
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") {
query = "artist:" + data.artist.name.replace(":", "") + " track:" + data.name.replace(":", "") + ( data.album.name.length > 0 ? " album: " + data.album.name.replace(":", ""): "");
album = data.album.name;
}
return spotify.searchAsync({query: query, type: type}).then(function(results) {
2014-12-06 21:39:15 +00:00
if (!results[type + "s"].items[0]) {
var matches = album.match(/^[^\(\[]+/);
if (matches && 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();
}
return module.exports.search(cleanedData);
} else {
return {service: "spotify"};
}
} else {
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
});
}