Fix track matching, upgrade GPM heuristics

This commit is contained in:
Jonathan Cremin 2017-05-07 19:59:25 +01:00
parent 03c50b550a
commit d6352b6250
9 changed files with 68 additions and 29 deletions

View file

@ -86,8 +86,8 @@ export function* search(data) {
query = cleanParam(data.artist.name) + ' ' + cleanParam(data.name);
album = data.name;
} else if (type === 'track') {
query = cleanParam(data.artist.name) + ' ' + cleanParam(data.album.name) + ' ' + cleanParam(data.name);
album = data.album.name;
query = cleanParam(data.artist.name) + ' ' + cleanParam(data.albumName) + ' ' + cleanParam(data.name);
album = data.albumName;
}
var path = '/search/' + type + '?q=' + encodeURIComponent(query);
@ -101,7 +101,7 @@ export function* search(data) {
if (type === 'album') {
cleanedData.name = matches[0].trim();
} else if (type === 'track') {
cleanedData.album.name = matches[0].trim();
cleanedData.albumName = matches[0].trim();
}
return yield module.exports.search(cleanedData);
} else {

View file

@ -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];
}
});
}

View file

@ -84,8 +84,8 @@ export function* search(data) {
album = data.name;
entity = 'album';
} 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;
entity = 'musicTrack';
}
@ -100,7 +100,7 @@ export function* search(data) {
if (type === 'album') {
cleanedData.name = matches[0].trim();
} else if (type === 'track') {
cleanedData.album.name = matches[0].trim();
cleanedData.albumName = matches[0].trim();
}
return yield search(cleanedData);
} else {

View file

@ -81,8 +81,8 @@ export function* search(data) {
query = "artist:" + cleanParam(data.artist.name) + " album:" + cleanParam(data.name);
album = data.name;
} else if (type == "track") {
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;
query = "artist:" + cleanParam(data.artist.name) + " track:" + cleanParam(data.name) + ( cleanParam(data.albumName).length > 0 ? " album:" + cleanParam(data.albumName): "");
album = data.albumName;
}
const results = yield spotify.searchAsync({query: query, type: type});
@ -90,7 +90,7 @@ export function* search(data) {
return {service: "spotify"};
} else {
let found;
const choppedAlbum = data.type == "album" ? cleanParam(data.name) : cleanParam(data.album.name);
const choppedAlbum = data.type == "album" ? cleanParam(data.name) : cleanParam(data.albumName);
if (!choppedAlbum.length) {
return yield lookupId(results[type + "s"].items[0].id, type);
}

View file

@ -107,7 +107,7 @@ export function* search(data) {
album = data.name;
} else if (type == 'track') {
query = cleanParam(data.artist.name.substring(0, data.artist.name.indexOf('&'))) + ' ' + cleanParam(data.name);
album = data.album.name
album = data.albumName
}
const path = '/music/search?q=' + encodeURIComponent(query) + '&filters=' + type + 's';
try {

View file

@ -80,7 +80,7 @@ export function* lookupId(id, type) {
match.name = topic.property['/type/object/name'].values[0].text;
if (topic.property['/music/recording/releases']) {
match.type = 'album';
match.album.name = topic.property['/music/recording/releases'].values[0].text;
match.albumName = topic.property['/music/recording/releases'].values[0].text;
}
} else if (musicalAlbum) {
match.name = topic.property['/type/object/name'].values[0].text;
@ -103,7 +103,7 @@ export function* search(data) {
album = data.name;
} else if (type == 'track') {
query = data.artist.name + ' ' + data.name;
album = data.album.name
album = data.albumName
}
const path = '/search?part=snippet&q=' + encodeURIComponent(query) + '&type=video&videoCaption=any&videoCategoryId=10&key=' + credentials.key;

View file

@ -21,7 +21,7 @@ describe('Deezer', function(){
});
it('should find track by search', function* (){
const result = yield deezer.search({type: 'track', artist: {name: 'Deftones'}, album: {name: 'Deftones'}, name: 'Hexagram'});
const result = yield deezer.search({type: 'track', artist: {name: 'Deftones'}, albumName: 'Deftones', name: 'Hexagram'});
result.name.should.equal('Hexagram');
});
});

View file

@ -19,6 +19,16 @@ describe('Google Play Music', function(){
const result = yield google.search({type: 'album', artist: {name: 'Jamie xx'}, name: 'In Colour'});
result.name.should.equal('In Colour');
});
it('should find track by search', function* (){
const result = yield google.search({type: 'track', artist: {name: 'Jamie xx'}, albumName: 'In Colour', name: 'Loud Places'});
result.name.should.equal('Loud Places');
});
it('should find awkward track by search', function* (){
const result = yield google.search({type: 'track', artist: {name: 'Jamie xx'}, albumName: 'In Colour (Remixes)', name: 'Loud Places [Tessela Remix]'});
result.name.should.equal('Loud Places [Tessela Remix]');
});
});
describe('lookupUrl', function(){

View file

@ -21,7 +21,7 @@ describe('iTunes Music', function(){
});
it('should find track by search', function* (){
const result = yield itunes.search({type: 'track', artist: {name: 'Deftones'}, album: {name: 'Deftones'}, name: 'Hexagram'});
const result = yield itunes.search({type: 'track', artist: {name: 'Deftones'}, albumName: 'Deftones', name: 'Hexagram'});
result.name.should.equal('Hexagram');
});
});