2014-12-04 19:17:41 +00:00
|
|
|
"use strict";
|
|
|
|
var parse = require('url').parse;
|
2014-12-13 00:00:49 +00:00
|
|
|
var Promise = require('bluebird');
|
2014-12-04 19:17:41 +00:00
|
|
|
var request = require('superagent');
|
2014-12-13 00:00:49 +00:00
|
|
|
require('superagent-bluebird-promise');
|
2014-12-04 19:17:41 +00:00
|
|
|
|
|
|
|
module.exports.id = "deezer";
|
|
|
|
|
|
|
|
var apiRoot = "https://api.deezer.com";
|
|
|
|
|
2014-12-11 19:53:12 +00:00
|
|
|
module.exports.match = require('./url').match;
|
|
|
|
|
2014-12-13 00:00:49 +00:00
|
|
|
module.exports.parseUrl = function(url) {
|
2014-12-11 19:53:12 +00:00
|
|
|
var matches = parse(url).path.match(/\/(album|track)[\/]+([^\/]+)/);
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2014-12-04 19:17:41 +00:00
|
|
|
|
|
|
|
module.exports.lookupId = function(id, type) {
|
|
|
|
var path = "/" + type + "/" + id;
|
|
|
|
|
2014-12-13 00:00:49 +00:00
|
|
|
return request.get(apiRoot + path).promise().then(function(res) {
|
2014-12-04 19:17:41 +00:00
|
|
|
var result = res.body;
|
2014-12-05 18:08:40 +00:00
|
|
|
if (res.body.error) {
|
|
|
|
var error = new Error("Not Found");
|
|
|
|
error.status = 404;
|
2014-12-13 00:00:49 +00:00
|
|
|
throw error;
|
2014-12-05 18:08:40 +00:00
|
|
|
}
|
2014-12-04 20:02:27 +00:00
|
|
|
var cover = result.cover || result.album.cover;
|
2014-12-13 00:00:49 +00:00
|
|
|
return request.get(cover).redirects(0).promise().then(function(res) {
|
2014-12-04 20:02:27 +00:00
|
|
|
var artwork = res.headers.location.replace("120x120", "200x200");
|
|
|
|
if (type == "album") {
|
2014-12-13 00:00:49 +00:00
|
|
|
return {
|
2014-12-04 20:02:27 +00:00
|
|
|
service: "deezer",
|
|
|
|
type: type,
|
|
|
|
id: result.id,
|
|
|
|
name: result.title,
|
2014-12-04 21:11:55 +00:00
|
|
|
streamUrl: result.link,
|
|
|
|
purchaseUrl: null,
|
2014-12-04 20:02:27 +00:00
|
|
|
artwork: artwork,
|
|
|
|
artist: {
|
|
|
|
name: result.artist.name
|
|
|
|
},
|
2014-12-13 00:00:49 +00:00
|
|
|
};
|
2014-12-04 20:02:27 +00:00
|
|
|
} else if (type == "track") {
|
2014-12-13 00:00:49 +00:00
|
|
|
return {
|
2014-12-04 20:02:27 +00:00
|
|
|
service: "deezer",
|
|
|
|
type: type,
|
|
|
|
id: result.id,
|
|
|
|
name: result.title,
|
2014-12-05 13:44:42 +00:00
|
|
|
streamUrl: result.album.link,
|
|
|
|
purchaseUrl: null,
|
2014-12-04 20:02:27 +00:00
|
|
|
artwork: artwork,
|
|
|
|
artist: {
|
|
|
|
name: result.artist.name
|
|
|
|
},
|
|
|
|
album: {
|
|
|
|
name: result.album.title
|
|
|
|
}
|
2014-12-13 00:00:49 +00:00
|
|
|
};
|
|
|
|
} else {
|
|
|
|
throw new Error();
|
|
|
|
}
|
2014-12-04 20:02:27 +00:00
|
|
|
});
|
2014-12-04 19:17:41 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-12-13 00:00:49 +00:00
|
|
|
module.exports.search = function(data) {
|
2014-12-05 14:13:47 +00:00
|
|
|
var query, album;
|
2014-12-04 19:17:41 +00:00
|
|
|
var type = data.type;
|
|
|
|
|
|
|
|
if (type == "album") {
|
|
|
|
query = data.artist.name + " " + data.name;
|
2014-12-05 14:13:47 +00:00
|
|
|
album = data.name;
|
2014-12-04 19:17:41 +00:00
|
|
|
} else if (type == "track") {
|
|
|
|
query = data.artist.name + " " + data.album.name + " " + data.name;
|
2014-12-05 14:13:47 +00:00
|
|
|
album = data.album.name;
|
2014-12-04 19:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var path = "/search/" + type + "?q=" + encodeURIComponent(query);
|
2014-12-13 00:00:49 +00:00
|
|
|
return request.get(apiRoot + path).promise().then(function(res) {
|
2014-12-04 19:17:41 +00:00
|
|
|
if (!res.body.data[0]) {
|
2014-12-05 14:13:47 +00:00
|
|
|
var matches = album.match(/^[^\(\[]+/);
|
2014-12-05 22:20:17 +00:00
|
|
|
if (matches[0] && matches[0] != album) {
|
2014-12-05 14:13:47 +00:00
|
|
|
var cleanedData = JSON.parse(JSON.stringify(data));
|
|
|
|
if (type == "album") {
|
|
|
|
cleanedData.name = matches[0].trim();
|
|
|
|
} else if (type == "track") {
|
2014-12-05 22:26:12 +00:00
|
|
|
cleanedData.album.name = matches[0].trim();
|
2014-12-05 14:13:47 +00:00
|
|
|
}
|
2014-12-13 00:00:49 +00:00
|
|
|
return module.exports.search(cleanedData);
|
2014-12-05 14:13:47 +00:00
|
|
|
} else {
|
2014-12-13 00:00:49 +00:00
|
|
|
return {service: "deezer"};
|
2014-12-05 14:13:47 +00:00
|
|
|
}
|
2014-12-04 19:17:41 +00:00
|
|
|
} else {
|
2014-12-13 00:00:49 +00:00
|
|
|
return module.exports.lookupId(res.body.data[0].id, type);
|
2014-12-04 19:17:41 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|