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

107 lines
2.9 KiB
JavaScript
Raw Normal View History

"use strict";
var parse = require('url').parse;
var Promise = require('bluebird');
var request = require('superagent');
require('superagent-bluebird-promise');
module.exports.id = "deezer";
var apiRoot = "https://api.deezer.com";
module.exports.match = require('./url').match;
module.exports.parseUrl = function(url) {
var matches = parse(url).path.match(/\/(album|track)[\/]+([^\/]+)/);
if (matches && matches[2]) {
return module.exports.lookupId(matches[2], matches[1]);
} else {
throw new Error();
}
}
module.exports.lookupId = function(id, type) {
var path = "/" + type + "/" + id;
return request.get(apiRoot + path).promise().then(function(res) {
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;
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;
return request.get(cover).redirects(0).promise().then(function(res) {
2014-12-13 12:05:47 +00:00
var artwork = {
small: res.headers.location.replace("120x120", "200x200"),
large: res.headers.location.replace("120x120", "800x800")
};
2014-12-04 20:02:27 +00:00
if (type == "album") {
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-04 20:02:27 +00:00
} else if (type == "track") {
return {
2014-12-04 20:02:27 +00:00
service: "deezer",
type: type,
id: result.id,
name: result.title,
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
}
};
} else {
throw new Error();
}
2014-12-04 20:02:27 +00:00
});
});
};
module.exports.search = function(data) {
var query, album;
var type = data.type;
if (type == "album") {
query = data.artist.name + " " + data.name;
album = data.name;
} else if (type == "track") {
query = data.artist.name + " " + data.album.name + " " + data.name;
album = data.album.name;
}
var path = "/search/" + type + "?q=" + encodeURIComponent(query);
return request.get(apiRoot + path).promise().then(function(res) {
if (!res.body.data[0]) {
var matches = album.match(/^[^\(\[]+/);
2014-12-05 22:20:17 +00:00
if (matches[0] && matches[0] != album) {
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();
}
return module.exports.search(cleanedData);
} else {
return {service: "deezer"};
}
} else {
return module.exports.lookupId(res.body.data[0].id, type);
}
});
};