Do a second search without trailing brackets
This commit is contained in:
parent
f7b1fcd53f
commit
8802ffe959
6 changed files with 113 additions and 39 deletions
|
@ -71,19 +71,32 @@ module.exports.lookupId = function(id) {
|
|||
|
||||
module.exports.search = function(data) {
|
||||
var deferred = Q.defer();
|
||||
var query;
|
||||
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?q=" + encodeURIComponent(query) + "&type=" + type + "&client_id=" + credentials.key;
|
||||
request.get(apiRoot + path, function(res) {
|
||||
if (!res.body.data[0]) {
|
||||
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: "beats"});
|
||||
}
|
||||
} else {
|
||||
module.exports.lookupId(res.body.data[0].id).then(deferred.resolve);
|
||||
}
|
||||
|
|
|
@ -58,19 +58,32 @@ module.exports.lookupId = function(id, type) {
|
|||
|
||||
module.exports.search = function(data, next) {
|
||||
var deferred = Q.defer();
|
||||
var query;
|
||||
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);
|
||||
request.get(apiRoot + path, function(res) {
|
||||
if (!res.body.data[0]) {
|
||||
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: "deezer"});
|
||||
}
|
||||
} else {
|
||||
module.exports.lookupId(res.body.data[0].id, type).then(deferred.resolve);
|
||||
}
|
||||
|
|
|
@ -65,8 +65,10 @@ module.exports.search = function(data) {
|
|||
|
||||
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;
|
||||
}
|
||||
|
||||
pm.search(query, 5, function(data) {
|
||||
|
@ -80,10 +82,20 @@ module.exports.search = function(data) {
|
|||
return a.score < b.score;
|
||||
}).shift();
|
||||
|
||||
if (!result.album && !result.track) {
|
||||
if (!result) {
|
||||
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: "googleplaymusic"});
|
||||
}
|
||||
|
||||
} else {
|
||||
var id;
|
||||
if (type == "album") {
|
||||
id = result.album.albumId;
|
||||
|
@ -92,6 +104,7 @@ module.exports.search = function(data) {
|
|||
}
|
||||
|
||||
module.exports.lookupId(id, type).then(deferred.resolve);
|
||||
}
|
||||
});
|
||||
return deferred.promise;
|
||||
}
|
||||
|
|
|
@ -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]) {
|
||||
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];
|
||||
|
||||
|
|
|
@ -115,8 +115,19 @@ module.exports.search = function(data) {
|
|||
}).shift();
|
||||
|
||||
if (!result) {
|
||||
return deferred.resolve({service: "rdio"});
|
||||
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: "rdio"});
|
||||
}
|
||||
} else {
|
||||
var parsed = parse(result.shortUrl)
|
||||
var id = parsed.path.replace("/x/", "").replace("/", "");
|
||||
deferred.resolve({
|
||||
|
@ -131,6 +142,7 @@ module.exports.search = function(data) {
|
|||
name: result.artist
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
return deferred.promise;
|
||||
};
|
||||
|
|
|
@ -56,29 +56,40 @@ module.exports.lookupId = function(id, type) {
|
|||
|
||||
module.exports.search = function(data) {
|
||||
var deferred = Q.defer();
|
||||
var query = "";
|
||||
var query, album;
|
||||
var type = data.type;
|
||||
|
||||
if (type == "album") {
|
||||
query = "artist:" + data.artist.name.replace(":", "") + " album:" + data.name.replace(":", "");
|
||||
album = data.name;
|
||||
} else if (type == "track") {
|
||||
query = "artist:" + data.artist.name.replace(":", "") + " album:" + data.album.name.replace(":", "") + " track:" + data.name.replace(":", "");
|
||||
album = data.album.name;
|
||||
}
|
||||
|
||||
spotify.search({query: query, type: type}, function(err, data) {
|
||||
if ( err ) {
|
||||
console.log('Error occurred: ' + err);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!data[type + "s"].items[0]) {
|
||||
deferred.resolve({service: "spotify"});
|
||||
return;
|
||||
}
|
||||
|
||||
var item = data[type + "s"].items[0];
|
||||
if (!data[type + "s"].items[0]) {
|
||||
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: "spotify"});
|
||||
}
|
||||
} else {
|
||||
module.exports.lookupId(data[type + "s"].items[0].id, type).then(deferred.resolve);
|
||||
}
|
||||
|
||||
module.exports.lookupId(item.id, type).then(deferred.resolve);
|
||||
});
|
||||
return deferred.promise;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue