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

154 lines
4.2 KiB
JavaScript
Raw Normal View History

2015-08-20 23:22:57 +01:00
import { parse } from 'url';
2015-06-03 21:45:54 -07:00
import request from 'superagent';
import 'superagent-bluebird-promise';
2015-08-20 23:22:57 +01:00
import { match as urlMatch } from './url';
2015-08-20 23:22:57 +01:00
export let id = 'deezer';
2015-06-03 21:45:54 -07:00
const apiRoot = 'https://api.deezer.com';
2015-08-20 23:22:57 +01:00
export const match = urlMatch;
2015-08-20 23:22:57 +01:00
export function parseUrl(url) {
2015-06-03 21:45:54 -07:00
let matches = parse(url).path.match(/\/(album|track)[\/]+([^\/]+)/);
if (matches && matches[2]) {
return module.exports.lookupId(matches[2], matches[1]);
} else {
throw new Error();
}
2015-06-03 21:45:54 -07:00
};
2015-08-20 23:22:57 +01:00
export function* lookupId(id, type) {
2015-06-03 21:45:54 -07:00
let path = '/' + type + '/' + id;
2015-06-03 21:45:54 -07:00
let {body} = yield request.get(apiRoot + path).promise();
if (!body || body.error) {
let error = new Error('Not Found');
error.status = 404;
return Promise.reject(error);
}
let item = body;
let coverUrl = item.cover || item.album.cover;
let cover = 'test';
// nasty hacks for superagent-bluebird-promise
try {
cover = yield request.get(coverUrl).redirects(0);
} catch(err) {
2015-10-13 15:58:13 -07:00
cover = err.originalError.response;
2015-06-03 21:45:54 -07:00
}
let artwork = {
small: cover.headers.location.replace('120x120', '200x200'),
large: cover.headers.location.replace('120x120', '800x800')
};
if (type === 'album') {
return Promise.resolve({
service: 'deezer',
type: type,
id: item.id,
name: item.title,
streamUrl: item.link,
purchaseUrl: null,
artwork: artwork,
artist: {
name: item.artist.name
}
});
} else if (type === 'track') {
return Promise.resolve({
service: 'deezer',
type: type,
id: item.id,
name: item.title,
streamUrl: item.album.link,
purchaseUrl: null,
artwork: artwork,
artist: {
name: item.artist.name
},
album: {
name: item.album.title
}
2014-12-04 20:02:27 +00:00
});
2015-06-03 21:45:54 -07:00
} else {
return Promise.reject(new Error());
}
};
2017-05-07 20:57:12 +01:00
export function* search(data, original={}) {
2015-06-03 21:45:54 -07:00
let cleanParam = function(str) {
return str.replace(/[\:\?\&]+/, '');
};
let query, album;
let {type} = data;
2017-05-07 20:57:12 +01:00
const various = data.artist.name === 'Various Artists' || data.artist.name === 'Various';
2015-06-03 21:45:54 -07:00
if (type === 'album') {
2017-05-07 20:57:12 +01:00
// Deezer is shitty about artists with these names, strip them instead
if (various) {
query = cleanParam(data.name);
} else {
query = cleanParam(data.artist.name) + ' ' + cleanParam(data.name);
}
album = data.name;
2015-06-03 21:45:54 -07:00
} else if (type === 'track') {
query = cleanParam(data.artist.name) + ' ' + cleanParam(data.albumName) + ' ' + cleanParam(data.name);
album = data.albumName;
}
2015-06-03 21:45:54 -07:00
var path = '/search/' + type + '?q=' + encodeURIComponent(query);
2017-05-07 20:57:12 +01:00
2015-06-03 21:45:54 -07:00
let response = yield request.get(apiRoot + path);
2017-05-07 20:57:12 +01:00
const name = original.name || data.name;
if (response.body.data.length > 0) {
let match;
if (!(match = exactMatch(name, response.body.data, data.type, various))) {
match = looseMatch(name, response.body.data, data.type, various);
}
2015-06-03 21:45:54 -07:00
return yield module.exports.lookupId(response.body.data[0].id, type);
} else {
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') {
cleanedData.albumName = matches[0].trim();
}
2017-05-07 20:57:12 +01:00
return yield module.exports.search(cleanedData, data);
} else {
2015-06-03 21:45:54 -07:00
return Promise.resolve({service: 'deezer'});
}
2015-06-03 21:45:54 -07:00
}
};
2017-05-07 20:57:12 +01:00
function exactMatch(needle, haystack, type, various) {
// try to find exact match
return haystack.find(function(entry) {
if (!entry[type] || (various && (entry.artist.name !== 'Various' || entry.artist.name !== 'Various Artists'))) {
return false;
}
entry = entry[type];
if (entry.title === needle) {
return entry;
}
});
}
function looseMatch(needle, haystack, type, various) {
// try to find exact match
return haystack.find(function(entry) {
if (!entry[type] || (various && (entry.artist.name !== 'Various' || entry.artist.name !== 'Various Artists'))) {
return false;
}
const name = entry[type].title || entry[type].name;
if (name.indexOf(needle) >= 0) {
return entry[type];
}
});
}