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

112 lines
3.1 KiB
JavaScript
Raw Normal View History

2015-08-20 23:22:57 +01:00
import { parse } from 'url';
import bluebird from 'bluebird';
import spotifyCB from 'spotify';
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";
2015-08-20 23:22:57 +01:00
export const match = urlMatch;
2015-08-20 23:22:57 +01:00
export function* parseUrl(url) {
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]);
}
}
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
}
2015-08-20 23:22:57 +01:00
export function* search(data) {
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(/[\:\?]+/, "");
}
var query, album;
var type = data.type;
if (type == "album") {
query = "artist:" + cleanParam(data.artist.name) + " album:" + cleanParam(data.name);
album = data.name;
} else if (type == "track") {
query = "artist:" + cleanParam(data.artist.name) + " track:" + cleanParam(data.name) + ( cleanParam(data.albumName).length > 0 ? " album:" + cleanParam(data.albumName): "");
album = data.albumName;
}
2015-08-20 23:22:57 +01:00
const results = yield spotify.searchAsync({query: query, type: type});
2017-05-07 20:36:20 +01:00
2015-08-20 23:22:57 +01:00
if (!results[type + "s"].items[0]) {
return {service: "spotify"};
} else {
let found;
const choppedAlbum = data.type == "album" ? cleanParam(data.name) : cleanParam(data.albumName);
2015-08-20 23:22:57 +01:00
if (!choppedAlbum.length) {
return yield lookupId(results[type + "s"].items[0].id, type);
2014-12-04 16:29:39 +00:00
}
2015-08-20 23:22:57 +01:00
results[type + "s"].items.forEach(function(item) {
const albumName = data.type == "album" ? item.name : item.album.name;
2017-05-07 20:36:20 +01:00
const matches = cleanParam(albumName).match(/^[^\(\[]+/);
2015-08-20 23:22:57 +01:00
if(choppedAlbum.indexOf(matches[0]) >= 0) {
found = item;
}
});
if (!found) {
return {service: "spotify"};
}
return yield lookupId(results[type + "s"].items[0].id, type);
}
2014-11-30 12:21:03 +00:00
}