2015-08-20 23:22:57 +01:00
|
|
|
import { parse } from 'url';
|
|
|
|
import bluebird from 'bluebird';
|
|
|
|
import spotifyCB from 'spotify';
|
2017-05-07 22:16:43 +01:00
|
|
|
import request from 'superagent';
|
|
|
|
import 'superagent-bluebird-promise';
|
2015-08-20 23:22:57 +01:00
|
|
|
const spotify = bluebird.promisifyAll(spotifyCB);
|
|
|
|
import { match as urlMatch } from './url';
|
2014-11-30 12:21:03 +00:00
|
|
|
|
2015-08-20 23:22:57 +01:00
|
|
|
export let id = "spotify";
|
2014-12-04 19:17:41 +00:00
|
|
|
|
2015-08-20 23:22:57 +01:00
|
|
|
export const match = urlMatch;
|
2014-12-11 19:53:12 +00:00
|
|
|
|
2015-08-20 23:22:57 +01:00
|
|
|
export function* parseUrl(url) {
|
2014-12-11 19:53:12 +00:00
|
|
|
var matches = parse(url).path.match(/\/(album|track)[\/]+([^\/]+)/);
|
|
|
|
|
|
|
|
if (matches && matches[2]) {
|
2015-08-20 23:22:57 +01:00
|
|
|
return yield lookupId(matches[2], matches[1]);
|
2014-12-11 19:53:12 +00:00
|
|
|
}
|
|
|
|
}
|
2014-12-04 19:17:41 +00:00
|
|
|
|
2015-08-20 23:22:57 +01:00
|
|
|
export function* lookupId(id, type) {
|
|
|
|
const data = yield spotify.lookupAsync({id: id, type: type});
|
|
|
|
if (data.error) {
|
|
|
|
var error = new Error("Not Found");
|
|
|
|
error.status = 404;
|
|
|
|
throw error;
|
|
|
|
}
|
2014-11-30 12:21:03 +00:00
|
|
|
|
2015-08-20 23:22:57 +01:00
|
|
|
var artist = data.artists[0];
|
2014-11-30 12:21:03 +00:00
|
|
|
|
2015-08-20 23:22:57 +01:00
|
|
|
if (type == "album") {
|
|
|
|
return {
|
|
|
|
service: "spotify",
|
|
|
|
type: type,
|
|
|
|
id: data.id,
|
|
|
|
name: data.name,
|
|
|
|
streamUrl: "https://play.spotify.com/" + type + "/" + data.id,
|
|
|
|
purchaseUrl: null,
|
|
|
|
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,
|
|
|
|
streamUrl: "https://play.spotify.com/" + type + "/" + data.id,
|
|
|
|
purchaseUrl: null,
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-05-07 22:16:43 +01:00
|
|
|
export function* search(data, original={}) {
|
|
|
|
const markets = ['US', 'GB', 'JP', 'BR', 'DE', 'ES'];
|
|
|
|
const cleanParam = function(str) {
|
2015-01-27 21:32:28 +00:00
|
|
|
var chopChars = ['&', '[', '('];
|
|
|
|
chopChars.forEach(function(chr) {
|
|
|
|
if (data.artist.name.indexOf('&') > 0) {
|
|
|
|
str = str.substring(0, data.artist.name.indexOf(chr));
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return str.replace(/[\:\?]+/, "");
|
|
|
|
}
|
2017-05-07 22:16:43 +01:00
|
|
|
let query, album;
|
|
|
|
const type = data.type;
|
2014-12-03 23:03:34 +00:00
|
|
|
|
|
|
|
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") {
|
2017-05-07 19:59:25 +01:00
|
|
|
query = "artist:" + cleanParam(data.artist.name) + " track:" + cleanParam(data.name) + ( cleanParam(data.albumName).length > 0 ? " album:" + cleanParam(data.albumName): "");
|
|
|
|
album = data.albumName;
|
2014-12-03 23:03:34 +00:00
|
|
|
}
|
|
|
|
|
2017-05-07 22:16:43 +01:00
|
|
|
for (let market of markets) {
|
|
|
|
const response = yield request.get('https://api.spotify.com/v1/search?type=' + type + '&q=' + encodeURI(query) + '&market=' + market);
|
|
|
|
const items = response.body[type + 's'].items;
|
2017-05-07 20:36:20 +01:00
|
|
|
|
2017-05-07 22:16:43 +01:00
|
|
|
const name = original.name || data.name;
|
|
|
|
|
|
|
|
let match;
|
|
|
|
if (!(match = exactMatch(name, items, type))) {
|
|
|
|
match = looseMatch(name, items, type);
|
2014-12-04 16:29:39 +00:00
|
|
|
}
|
|
|
|
|
2017-05-07 22:16:43 +01:00
|
|
|
if (match) {
|
|
|
|
if (type === 'album') {
|
|
|
|
return yield lookupId(match.id, type);
|
|
|
|
} else if (type === 'track') {
|
|
|
|
return yield lookupId(match.id, type);
|
2015-08-20 23:22:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-07 22:16:43 +01:00
|
|
|
return {service: "spotify"};
|
|
|
|
}
|
|
|
|
|
|
|
|
function exactMatch(needle, haystack, type) {
|
|
|
|
// try to find exact match
|
|
|
|
return haystack.find(function(entry) {
|
|
|
|
if (entry.type !== type) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry.name === needle) {
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function looseMatch(needle, haystack, type) {
|
|
|
|
// try to find exact match
|
|
|
|
return haystack.find(function(entry) {
|
|
|
|
if (entry.type !== type) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry.name.indexOf(needle) >= 0) {
|
|
|
|
return entry
|
|
|
|
}
|
|
|
|
});
|
2014-11-30 12:21:03 +00:00
|
|
|
}
|