Do a second search without trailing brackets

This commit is contained in:
Jonathan Cremin 2014-12-05 14:13:47 +00:00
parent f7b1fcd53f
commit 8802ffe959
6 changed files with 113 additions and 39 deletions

View file

@ -51,15 +51,16 @@ module.exports.lookupId = function(id, type) {
module.exports.search = function(data) {
var deferred = Q.defer();
var query = "";
var entity = "";
var query, album, entity;
var type = data.type;
if (type == "album") {
query = data.artist.name + " " + data.name;
album = data.name;
entity = "album";
} else if (type == "track") {
query = data.artist.name + " " + data.album.name + " " + data.name;
album = data.album.name;
entity = "musicTrack";
}
@ -68,7 +69,18 @@ module.exports.search = function(data) {
var data = JSON.parse(res.text);
if (!data.results[0]) {
deferred.resolve({service: "itunes"});
var matches = album.match(/^[^\(\[]+/);
if (matches[0]) {
var cleanedData = JSON.parse(JSON.stringify(data));
if (type == "album") {
cleanedData.name = matches[0].trim();
} else if (type == "track") {
cleanedData.album = matches[0].trim();
}
module.exports.search(cleanedData).then(deferred.resolve);
} else {
deferred.resolve({service: "itunes"});
}
} else {
var result = data.results[0];