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';
|
2014-12-04 19:17:41 +00:00
|
|
|
|
2015-08-20 23:22:57 +01:00
|
|
|
export let id = 'deezer';
|
2014-12-04 19:17:41 +00:00
|
|
|
|
2015-06-03 21:45:54 -07:00
|
|
|
const apiRoot = 'https://api.deezer.com';
|
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) {
|
2015-06-03 21:45:54 -07:00
|
|
|
let matches = parse(url).path.match(/\/(album|track)[\/]+([^\/]+)/);
|
2014-12-11 19:53:12 +00:00
|
|
|
|
|
|
|
if (matches && matches[2]) {
|
2014-12-13 00:00:49 +00:00
|
|
|
return module.exports.lookupId(matches[2], matches[1]);
|
2014-12-11 19:53:12 +00:00
|
|
|
} else {
|
2014-12-13 00:00:49 +00:00
|
|
|
throw new Error();
|
2014-12-11 19:53:12 +00:00
|
|
|
}
|
2015-06-03 21:45:54 -07:00
|
|
|
};
|
2014-12-04 19:17:41 +00: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;
|
2014-12-04 19:17:41 +00:00
|
|
|
|
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) {
|
|
|
|
cover = err.message.response.res;
|
|
|
|
}
|
|
|
|
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-13 00:00:49 +00:00
|
|
|
}
|
2014-12-04 20:02:27 +00:00
|
|
|
});
|
2015-06-03 21:45:54 -07:00
|
|
|
} else {
|
|
|
|
return Promise.reject(new Error());
|
|
|
|
}
|
2014-12-04 19:17:41 +00:00
|
|
|
};
|
|
|
|
|
2015-08-20 23:22:57 +01:00
|
|
|
export function* search(data) {
|
2015-06-03 21:45:54 -07:00
|
|
|
let cleanParam = function(str) {
|
|
|
|
return str.replace(/[\:\?\&]+/, '');
|
|
|
|
};
|
|
|
|
let query, album;
|
|
|
|
let {type} = data;
|
2014-12-04 19:17:41 +00:00
|
|
|
|
2015-06-03 21:45:54 -07:00
|
|
|
if (type === 'album') {
|
|
|
|
query = cleanParam(data.artist.name) + ' ' + cleanParam(data.name);
|
2014-12-05 14:13:47 +00:00
|
|
|
album = data.name;
|
2015-06-03 21:45:54 -07:00
|
|
|
} else if (type === 'track') {
|
|
|
|
query = cleanParam(data.artist.name) + ' ' + cleanParam(data.album.name) + ' ' + cleanParam(data.name);
|
2014-12-05 14:13:47 +00:00
|
|
|
album = data.album.name;
|
2014-12-04 19:17:41 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 21:45:54 -07:00
|
|
|
var path = '/search/' + type + '?q=' + encodeURIComponent(query);
|
|
|
|
let response = yield request.get(apiRoot + path);
|
|
|
|
if (response.body.data[0]) {
|
|
|
|
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.album.name = matches[0].trim();
|
2014-12-05 14:13:47 +00:00
|
|
|
}
|
2015-06-03 21:45:54 -07:00
|
|
|
return yield module.exports.search(cleanedData);
|
2014-12-04 19:17:41 +00:00
|
|
|
} else {
|
2015-06-03 21:45:54 -07:00
|
|
|
return Promise.resolve({service: 'deezer'});
|
2014-12-04 19:17:41 +00:00
|
|
|
}
|
2015-06-03 21:45:54 -07:00
|
|
|
}
|
2014-12-04 19:17:41 +00:00
|
|
|
};
|