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

145 lines
3.8 KiB
JavaScript
Raw Normal View History

2015-08-20 23:22:57 +01:00
import { parse } from 'url';
2017-07-20 14:31:07 +01:00
import SpotifyWebApi from 'spotify-web-api-node';
import urlMatch from './url';
2014-11-30 12:21:03 +00:00
2017-07-20 14:31:07 +01:00
const spotify = new SpotifyWebApi({
clientId: process.env.SPOTIFY_CLIENT_ID,
clientSecret: process.env.SPOTIFY_CLIENT_SECRET,
2017-10-23 17:58:23 +01:00
redirectUri: 'https://combine.fm',
2017-07-20 14:31:07 +01:00
});
2017-07-20 14:31:07 +01:00
function exactMatch(needle, haystack, type) {
// try to find exact match
return haystack.find((entry) => {
if (entry.type !== type) {
return false;
}
2017-07-20 14:31:07 +01:00
if (entry.name === needle) {
return entry;
}
return false;
});
}
function looseMatch(needle, haystack, type) {
// try to find exact match
return haystack.find((entry) => {
if (entry.type !== type) {
return false;
}
if (entry.name.indexOf(needle) >= 0) {
return entry;
}
return false;
});
}
2015-08-20 23:22:57 +01:00
export function* lookupId(id, type) {
2017-07-20 14:31:07 +01:00
const token = yield spotify.clientCredentialsGrant();
spotify.setAccessToken(token.body['access_token']);
let data = yield spotify[`get${type.charAt(0).toUpperCase()}${type.slice(1)}s`]([id]);
data = data.body[`${type}s`][0];
2014-11-30 12:21:03 +00:00
2017-07-20 14:31:07 +01:00
const artist = data.artists[0];
2014-11-30 12:21:03 +00:00
2017-07-20 14:31:07 +01:00
if (type === 'album') {
2015-08-20 23:22:57 +01:00
return {
2017-07-20 14:31:07 +01:00
service: 'spotify',
type,
2015-08-20 23:22:57 +01:00
id: data.id,
name: data.name,
2017-07-20 14:31:07 +01:00
streamUrl: `https://play.spotify.com/${type}/${data.id}`,
2015-08-20 23:22:57 +01:00
purchaseUrl: null,
artwork: {
2017-07-20 14:31:07 +01:00
small: data.images[1].url.replace('http:', 'https:'),
large: data.images[0].url.replace('http:', 'https:'),
2015-08-20 23:22:57 +01:00
},
artist: {
2017-07-20 14:31:07 +01:00
name: artist.name,
},
2015-08-20 23:22:57 +01:00
};
2017-07-20 14:31:07 +01:00
} else if (type === 'track') {
2015-08-20 23:22:57 +01:00
return {
2017-07-20 14:31:07 +01:00
service: 'spotify',
type,
2015-08-20 23:22:57 +01:00
id: data.id,
name: data.name,
2017-07-20 14:31:07 +01:00
streamUrl: `https://play.spotify.com/${type}/${data.id}`,
2015-08-20 23:22:57 +01:00
purchaseUrl: null,
artwork: {
2017-07-20 14:31:07 +01:00
small: data.album.images[1].url.replace('http:', 'https:'),
large: data.album.images[0].url.replace('http:', 'https:'),
2015-08-20 23:22:57 +01:00
},
artist: {
2017-07-20 14:31:07 +01:00
name: artist.name,
2015-08-20 23:22:57 +01:00
},
album: {
2017-07-20 14:31:07 +01:00
name: data.album.name,
},
2015-08-20 23:22:57 +01:00
};
}
2017-07-20 14:31:07 +01:00
return { service: 'spotify' };
2014-11-30 12:21:03 +00:00
}
2017-07-20 14:31:07 +01:00
export function* search(data, original = {}) {
const token = yield spotify.clientCredentialsGrant();
spotify.setAccessToken(token.body['access_token']);
const markets = ['US', 'GB', 'JP', 'BR', 'DE', 'ES'];
2017-07-20 14:31:07 +01:00
function cleanParam(str) {
const chopChars = ['&', '[', '('];
chopChars.forEach((chr) => {
if (data.artist.name.indexOf('&') > 0) {
2017-07-20 14:31:07 +01:00
str = str.substring(0, data.artist.name.indexOf(chr)); // eslint-disable-line no-param-reassign,max-len
}
2017-07-20 14:31:07 +01:00
});
return str.replace(/[:?]+/, '');
}
2017-07-20 14:31:07 +01:00
let query;
const type = data.type;
2017-07-20 14:31:07 +01:00
if (type === 'album') {
query = `artist:${cleanParam(data.artist.name)} album:${cleanParam(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)}` : ''}`;
}
2017-07-20 14:31:07 +01:00
for (const market of markets) { // eslint-disable-line
const response = yield spotify[`search${type.charAt(0).toUpperCase()}${type.slice(1)}s`](query, { market });
const items = response.body[`${type}s`].items;
2017-05-07 20:36:20 +01:00
const name = original.name || data.name;
2017-07-20 14:31:07 +01:00
let match = exactMatch(name, items, type);
if (!match) {
match = looseMatch(name, items, type);
2014-12-04 16:29:39 +00: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-07-20 14:31:07 +01:00
return { service: 'spotify' };
}
2017-07-20 14:31:07 +01:00
export function* parseUrl(url) {
const matches = parse(url).path.match(/\/(album|track)[/]+([^/]+)/);
2017-07-20 14:31:07 +01:00
if (matches && matches[2]) {
return yield lookupId(matches[2], matches[1]);
}
throw new Error();
}
2017-07-20 14:31:07 +01:00
export const id = 'spotify';
export const match = urlMatch;