Fix track matching, upgrade GPM heuristics
This commit is contained in:
parent
03c50b550a
commit
d6352b6250
9 changed files with 68 additions and 29 deletions
lib/services/google
|
@ -88,7 +88,7 @@ export function* lookupId(id, type) {
|
|||
}
|
||||
};
|
||||
|
||||
export function* search(data) {
|
||||
export function* search(data, original={}) {
|
||||
yield ready;
|
||||
let query, album;
|
||||
const type = data.type;
|
||||
|
@ -97,39 +97,68 @@ export function* search(data) {
|
|||
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;
|
||||
query = data.artist.name + ' ' + data.albumName + ' ' + data.name;
|
||||
album = data.albumName;
|
||||
}
|
||||
|
||||
let result = yield pm.searchAsync(query, 5)
|
||||
|
||||
if (!result.entries) {
|
||||
const matches = album.match(/^[^\(\[]+/);
|
||||
if (matches && matches[0] && matches[0] !== album) {
|
||||
if (matches && matches[0]) {
|
||||
const cleanedData = JSON.parse(JSON.stringify(data));
|
||||
if (type === 'album') {
|
||||
cleanedData.name = matches[0].trim();
|
||||
cleanedData.name = data.name.match(/^[^\(\[]+/)[0].trim();
|
||||
} else if (type === 'track') {
|
||||
cleanedData.album.name = matches[0].trim();
|
||||
cleanedData.albumName = data.albumName.match(/^[^\(\[]+/)[0].trim();
|
||||
cleanedData.name = data.name.match(/^[^\(\[]+/)[0].trim();
|
||||
}
|
||||
return yield search(cleanedData);
|
||||
return yield search(cleanedData, data);
|
||||
} else {
|
||||
return {service: 'google'};
|
||||
}
|
||||
}
|
||||
result = result.entries.filter(function(entry) {
|
||||
return entry[type];
|
||||
}).sort(function(a, b) { // sort by match score
|
||||
return a.score < b.score;
|
||||
}).shift();
|
||||
|
||||
if (!result) {
|
||||
const name = original.name || data.name;
|
||||
|
||||
let match;
|
||||
if (!(match = exactMatch(name, result.entries, data.type))) {
|
||||
match = looseMatch(name, result.entries, data.type);
|
||||
}
|
||||
|
||||
if (!match) {
|
||||
return {service: 'google'};
|
||||
} else {
|
||||
if (type === 'album') {
|
||||
return yield lookupId(result.album.albumId, type);
|
||||
return yield lookupId(match.album.albumId, type);
|
||||
} else if (type === 'track') {
|
||||
return yield lookupId(result.track.nid, type);
|
||||
return yield lookupId(match.track.storeId, type);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
function exactMatch(needle, haystack, type) {
|
||||
// try to find exact match
|
||||
return haystack.find(function(entry) {
|
||||
if (!entry[type]) {
|
||||
return false;
|
||||
}
|
||||
entry = entry[type];
|
||||
if (entry.title === needle) {
|
||||
return entry;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function looseMatch(needle, haystack, type) {
|
||||
// try to find exact match
|
||||
return haystack.find(function(entry) {
|
||||
if (!entry[type]) {
|
||||
return false;
|
||||
}
|
||||
const name = entry[type].title || entry[type].name;
|
||||
if (name.indexOf(needle) >= 0) {
|
||||
return entry[type];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue