Update the rest of the libraries for bluebird

This commit is contained in:
Jonathan Cremin 2014-12-13 00:00:49 +00:00
parent 90c1385fb3
commit 17de5e9b92
8 changed files with 591 additions and 144 deletions

View file

@ -1,7 +1,8 @@
"use strict";
var parse = require('url').parse;
var Promise = require('bluebird');
var request = require('superagent');
var Q = require('q');
require('superagent-bluebird-promise');
module.exports.id = "deezer";
@ -9,35 +10,31 @@ var apiRoot = "https://api.deezer.com";
module.exports.match = require('./url').match;
module.exports.parseUrl = function(url, next) {
var deferred = Q.defer();
module.exports.parseUrl = function(url) {
var matches = parse(url).path.match(/\/(album|track)[\/]+([^\/]+)/);
if (matches && matches[2]) {
module.exports.lookupId(matches[2], matches[1]).then(deferred.resolve);
return module.exports.lookupId(matches[2], matches[1]);
} else {
deferred.reject();
throw new Error();
}
return deferred.promise;
}
module.exports.lookupId = function(id, type) {
var deferred = Q.defer();
var path = "/" + type + "/" + id;
request.get(apiRoot + path, function(res) {
return request.get(apiRoot + path).promise().then(function(res) {
var result = res.body;
if (res.body.error) {
var error = new Error("Not Found");
error.status = 404;
deferred.reject(error);
return;
throw error;
}
var cover = result.cover || result.album.cover;
request.get(cover).redirects(0).end(function(res) {
return request.get(cover).redirects(0).promise().then(function(res) {
var artwork = res.headers.location.replace("120x120", "200x200");
if (type == "album") {
deferred.resolve({
return {
service: "deezer",
type: type,
id: result.id,
@ -48,9 +45,9 @@ module.exports.lookupId = function(id, type) {
artist: {
name: result.artist.name
},
});
};
} else if (type == "track") {
deferred.resolve({
return {
service: "deezer",
type: type,
id: result.id,
@ -64,15 +61,15 @@ module.exports.lookupId = function(id, type) {
album: {
name: result.album.title
}
});
};
};
} else {
throw new Error();
}
});
});
return deferred.promise;
};
module.exports.search = function(data, next) {
var deferred = Q.defer();
module.exports.search = function(data) {
var query, album;
var type = data.type;
@ -85,7 +82,7 @@ module.exports.search = function(data, next) {
}
var path = "/search/" + type + "?q=" + encodeURIComponent(query);
request.get(apiRoot + path, function(res) {
return request.get(apiRoot + path).promise().then(function(res) {
if (!res.body.data[0]) {
var matches = album.match(/^[^\(\[]+/);
if (matches[0] && matches[0] != album) {
@ -95,13 +92,12 @@ module.exports.search = function(data, next) {
} else if (type == "track") {
cleanedData.album.name = matches[0].trim();
}
module.exports.search(cleanedData).then(deferred.resolve);
return module.exports.search(cleanedData);
} else {
deferred.resolve({service: "deezer"});
return {service: "deezer"};
}
} else {
module.exports.lookupId(res.body.data[0].id, type).then(deferred.resolve);
return module.exports.lookupId(res.body.data[0].id, type);
}
});
return deferred.promise;
};