Improve matching and metadata extraction
This commit is contained in:
parent
7c1b8aa771
commit
e4a856e228
6 changed files with 74 additions and 36 deletions
|
@ -65,32 +65,46 @@ module.exports.lookupId = function(id, type) {
|
|||
}
|
||||
|
||||
module.exports.search = function(data) {
|
||||
var cleanParam = function(str) {
|
||||
var chopChars = ['&', '[', '('];
|
||||
chopChars.forEach(function(chr) {
|
||||
if (data.artist.name.indexOf('&') > 0) {
|
||||
str = str.substring(0, data.artist.name.indexOf(chr));
|
||||
}
|
||||
})
|
||||
return str.replace(/[\:\?]+/, "");
|
||||
}
|
||||
var query, album;
|
||||
var type = data.type;
|
||||
|
||||
if (type == "album") {
|
||||
query = "artist:" + data.artist.name.replace(":", "") + " album:" + data.name.replace(":", "");
|
||||
query = "artist:" + cleanParam(data.artist.name) + " album:" + cleanParam(data.name);
|
||||
album = data.name;
|
||||
} else if (type == "track") {
|
||||
query = "artist:" + data.artist.name.replace(":", "") + " track:" + data.name.replace(":", "") + ( data.album.name.length > 0 ? " album: " + data.album.name.replace(":", ""): "");
|
||||
query = "artist:" + cleanParam(data.artist.name) + " track:" + cleanParam(data.name) + ( cleanParam(data.album.name).length > 0 ? " album:" + cleanParam(data.album.name): "");
|
||||
album = data.album.name;
|
||||
}
|
||||
|
||||
return spotify.searchAsync({query: query, type: type}).then(function(results) {
|
||||
if (!results[type + "s"].items[0]) {
|
||||
var matches = album.match(/^[^\(\[]+/);
|
||||
if (matches && matches[0] && matches[0] != album) {
|
||||
var cleanedData = JSON.parse(JSON.stringify(data));
|
||||
if (type == "album") {
|
||||
cleanedData.name = matches[0].trim();
|
||||
} else if (type == "track") {
|
||||
cleanedData.album.name = matches[0].trim();
|
||||
return {service: "spotify"};
|
||||
} else {
|
||||
var found;
|
||||
var choppedAlbum = data.type == "album" ? cleanParam(data.name) : cleanParam(data.album.name);
|
||||
if (!choppedAlbum.length) {
|
||||
return module.exports.lookupId(results[type + "s"].items[0].id, type);
|
||||
}
|
||||
|
||||
results[type + "s"].items.forEach(function(item) {
|
||||
var albumName = data.type == "album" ? item.name : item.album.name;
|
||||
var matches = albumName.match(/^[^\(\[]+/);
|
||||
if(choppedAlbum.indexOf(matches[0]) >= 0) {
|
||||
found = item;
|
||||
}
|
||||
return module.exports.search(cleanedData);
|
||||
} else {
|
||||
});
|
||||
if (!found) {
|
||||
return {service: "spotify"};
|
||||
}
|
||||
} else {
|
||||
return module.exports.lookupId(results[type + "s"].items[0].id, type);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue