Improve matching and metadata extraction

This commit is contained in:
Jonathan Cremin 2015-01-27 21:32:28 +00:00
parent 7c1b8aa771
commit e4a856e228
6 changed files with 74 additions and 36 deletions

View file

@ -97,14 +97,17 @@ module.exports.lookupId = function(id, type) {
};
module.exports.search = function(data) {
var cleanParam = function(str) {
return str.replace(/[\:\?\&]+/, "");
}
var query, album;
var type = data.type;
if (type == "album") {
query = '"' + data.artist.name + '" "' + data.name + '"';
query = '"' + cleanParam(data.artist.name) + '" "' + cleanParam(data.name) + '"';
album = data.name;
} else if (type == "track") {
query = '"' + data.artist.name + '" "' + data.name + '"';
query = '"' + cleanParam(data.artist.name) + '" "' + cleanParam(data.name) + '"';
album = data.album.name
}
@ -112,21 +115,33 @@ module.exports.search = function(data) {
return request.get(apiRoot + path).promise().then(function(res) {
if (!res.body.data[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: "beats"};
} else {
var found;
var choppedAlbum = data.type == "album" ? cleanParam(data.name) : cleanParam(data.album.name);
var choppedArtist = cleanParam(data.artist.name);
res.body.data.forEach(function(item) {
var matches = item.detail.match(/^[^\(\[]+/);
if(choppedArtist.indexOf(matches[0]) >= 0) {
found = item;
}
return module.exports.search(cleanedData);
} else {
});
if (!found && !choppedAlbum.length) {
return module.exports.lookupId(res.body.data[0].id, type);
}
res.body.data.forEach(function(item) {
var matches = item.related.display.match(/^[^\(\[]+/);
if(choppedAlbum.indexOf(matches[0]) >= 0) {
found = item;
}
});
if (!found) {
return {service: "beats"};
}
} else {
//insist on at least album or artist name being exactly right
return module.exports.lookupId(res.body.data[0].id, type);
return module.exports.lookupId(found.id, type);
}
});
};