Migrate beats library to bluebird
This commit is contained in:
parent
496434f710
commit
90c1385fb3
2 changed files with 23 additions and 28 deletions
|
@ -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 = "beats";
|
||||
|
||||
|
@ -20,32 +21,28 @@ var apiRoot = "https://partner.api.beatsmusic.com/v1/api";
|
|||
module.exports.match = require("./url").match;
|
||||
|
||||
module.exports.parseUrl = function(url) {
|
||||
var deferred = Q.defer();
|
||||
var matches = parse(url).path.match(/\/albums[\/]+([^\/]+)(\/tracks\/)?([^\/]+)?/);
|
||||
if (matches && matches[3]) {
|
||||
module.exports.lookupId(matches[3], "track").then(deferred.resolve);
|
||||
return module.exports.lookupId(matches[3], "track");
|
||||
} else if (matches && matches[1]) {
|
||||
module.exports.lookupId(matches[1], "album").then(deferred.resolve);
|
||||
return module.exports.lookupId(matches[1], "album");
|
||||
} else {
|
||||
deferred.reject();
|
||||
throw new Error("Url does not match");
|
||||
}
|
||||
return deferred.promise;
|
||||
}
|
||||
|
||||
module.exports.lookupId = function(id, type) {
|
||||
var deferred = Q.defer();
|
||||
|
||||
if (type == "album") {
|
||||
request.get(apiRoot + "/albums/" + id + "/images/default?size=medium&client_id=" + credentials.key).redirects(0).end(function(res) {
|
||||
return request.get(apiRoot + "/albums/" + id + "/images/default?size=medium&client_id=" + credentials.key).redirects(0).promise().then(function(res) {
|
||||
var artwork = res.headers.location;
|
||||
request.get(apiRoot + "/albums/" + id + "?client_id=" + credentials.key, function(res) {
|
||||
return request.get(apiRoot + "/albums/" + id + "?client_id=" + credentials.key).promise().then(function(res) {
|
||||
if (!res.body.data) {
|
||||
var error = new Error("Not Found");
|
||||
error.status = 404;
|
||||
return deferred.reject(error);
|
||||
throw error;
|
||||
}
|
||||
var result = res.body.data;
|
||||
deferred.resolve({
|
||||
return {
|
||||
service: "beats",
|
||||
type: "album",
|
||||
id: result.id,
|
||||
|
@ -56,20 +53,20 @@ module.exports.lookupId = function(id, type) {
|
|||
artist: {
|
||||
name: result.artist_display_name
|
||||
}
|
||||
});
|
||||
};
|
||||
});
|
||||
});
|
||||
} else if (type == "track") {
|
||||
request.get(apiRoot + "/tracks/" + id + "?client_id=" + credentials.key, function(res) {
|
||||
return request.get(apiRoot + "/tracks/" + id + "?client_id=" + credentials.key).promise().then(function(res) {
|
||||
if (!res.body.data) {
|
||||
var error = new Error("Not Found");
|
||||
error.status = 404;
|
||||
return deferred.reject(error);
|
||||
throw error;
|
||||
}
|
||||
var result = res.body.data;
|
||||
request.get(apiRoot + "/albums/" + result.refs.album.id + "/images/default?size=medium&client_id=" + credentials.key).redirects(0).end(function(res) {
|
||||
return request.get(apiRoot + "/albums/" + result.refs.album.id + "/images/default?size=medium&client_id=" + credentials.key).redirects(0).promise().then(function(res) {
|
||||
var artwork = res.headers.location;
|
||||
deferred.resolve({
|
||||
return {
|
||||
service: "beats",
|
||||
type: "track",
|
||||
id: result.id,
|
||||
|
@ -83,19 +80,17 @@ module.exports.lookupId = function(id, type) {
|
|||
album: {
|
||||
name: result.refs.album.display
|
||||
}
|
||||
});
|
||||
};
|
||||
});
|
||||
});
|
||||
} else {
|
||||
var error = new Error("Not Found");
|
||||
error.status = 404;
|
||||
deferred.reject(error);
|
||||
return error;
|
||||
}
|
||||
return deferred.promise;
|
||||
};
|
||||
|
||||
module.exports.search = function(data) {
|
||||
var deferred = Q.defer();
|
||||
var query, album;
|
||||
var type = data.type;
|
||||
|
||||
|
@ -108,7 +103,7 @@ module.exports.search = function(data) {
|
|||
}
|
||||
|
||||
var path = "/search?q=" + encodeURIComponent(query) + "&type=" + type + "&client_id=" + credentials.key;
|
||||
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) {
|
||||
|
@ -118,15 +113,13 @@ module.exports.search = function(data) {
|
|||
} 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: "beats"});
|
||||
return {service: "beats"};
|
||||
}
|
||||
} else {
|
||||
//insist on at least album or artist name being exactly right
|
||||
|
||||
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;
|
||||
};
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
"node": "0.10.x"
|
||||
},
|
||||
"dependencies": {
|
||||
"bluebird": "^2.3.11",
|
||||
"body-parser": "~1.8.1",
|
||||
"connect-flash": "^0.1.1",
|
||||
"cookie-parser": "~1.3.3",
|
||||
|
@ -25,7 +26,8 @@
|
|||
"rdio": "^1.5.2",
|
||||
"serve-favicon": "~2.1.3",
|
||||
"spotify": "^0.3.0",
|
||||
"superagent": "^0.21.0"
|
||||
"superagent": "^0.21.0",
|
||||
"superagent-bluebird-promise": "^0.5.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"should": "^4.3.0",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue