Update the rest of the libraries for bluebird
This commit is contained in:
parent
90c1385fb3
commit
17de5e9b92
8 changed files with 591 additions and 144 deletions
|
@ -1,8 +1,9 @@
|
|||
"use strict";
|
||||
var parse = require('url').parse;
|
||||
var parse = require("url").parse;
|
||||
var Promise = require('bluebird');
|
||||
var querystring = require('querystring');
|
||||
var request = require('superagent');
|
||||
var Q = require('q');
|
||||
require('superagent-bluebird-promise');
|
||||
|
||||
module.exports.id = "itunes";
|
||||
|
||||
|
@ -11,7 +12,6 @@ var apiRoot = "https://itunes.apple.com";
|
|||
module.exports.match = require('./url').match;
|
||||
|
||||
module.exports.parseUrl = function(url) {
|
||||
var deferred = Q.defer();
|
||||
var parsed = parse(url);
|
||||
var matches = parsed.path.match(/[\/]?([\/]?[a-z]{2}?)?[\/]+album[\/]+([^\/]+)[\/]+([^\?]+)/);
|
||||
var query = querystring.parse(parsed.query);
|
||||
|
@ -23,16 +23,13 @@ module.exports.parseUrl = function(url) {
|
|||
type = "track";
|
||||
id = query.i;
|
||||
}
|
||||
module.exports.lookupId(id, type, matches[1] || "us").then(deferred.resolve, deferred.reject);
|
||||
return module.exports.lookupId(id, type, matches[1] || "us");
|
||||
} else {
|
||||
deferred.reject();
|
||||
throw new Error();
|
||||
}
|
||||
return deferred.promise;
|
||||
};
|
||||
|
||||
module.exports.lookupId = function(id, type, cc) {
|
||||
var deferred = Q.defer();
|
||||
|
||||
if (id.match(/^[a-z]{2}/)) {
|
||||
cc = id.substr(0,2);
|
||||
id = id.substr(2);
|
||||
|
@ -43,13 +40,13 @@ module.exports.lookupId = function(id, type, cc) {
|
|||
path = "/" + cc + path;
|
||||
}
|
||||
|
||||
request.get(apiRoot + path, function(res) {
|
||||
return request.get(apiRoot + path).promise().then(function(res) {
|
||||
var data = JSON.parse(res.text);
|
||||
|
||||
if (!data.results || data.resultCount == 0 || !data.results[0].collectionId) {
|
||||
var error = new Error("Not Found");
|
||||
error.status = 404;
|
||||
deferred.reject(error);
|
||||
throw error;
|
||||
} else {
|
||||
var result = data.results[0];
|
||||
|
||||
|
@ -72,14 +69,12 @@ module.exports.lookupId = function(id, type, cc) {
|
|||
};
|
||||
}
|
||||
|
||||
deferred.resolve(item);
|
||||
return item;
|
||||
}
|
||||
});
|
||||
return deferred.promise;
|
||||
};
|
||||
|
||||
module.exports.search = function(data) {
|
||||
var deferred = Q.defer();
|
||||
var query, album, entity;
|
||||
var type = data.type;
|
||||
|
||||
|
@ -94,7 +89,7 @@ module.exports.search = function(data) {
|
|||
}
|
||||
|
||||
var path = "/search?term=" + encodeURIComponent(query) + "&media=music&entity=" + entity;
|
||||
request.get(apiRoot + path, function(res) {
|
||||
return request.get(apiRoot + path).promise().then(function(res) {
|
||||
var result = JSON.parse(res.text);
|
||||
|
||||
if (!result.results[0]) {
|
||||
|
@ -106,9 +101,9 @@ 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: "itunes"});
|
||||
return {service: "itunes"};
|
||||
}
|
||||
} else {
|
||||
var result = result.results[0];
|
||||
|
@ -131,9 +126,7 @@ module.exports.search = function(data) {
|
|||
name: result.collectionName
|
||||
};
|
||||
}
|
||||
deferred.resolve(item);
|
||||
return item;
|
||||
}
|
||||
});
|
||||
|
||||
return deferred.promise;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue