Improve Groove music matching
This commit is contained in:
parent
147ad4f3d2
commit
bf8b6eea52
2 changed files with 53 additions and 25 deletions
|
@ -35,30 +35,24 @@ function* getAccessToken() {
|
||||||
return result.body.access_token;
|
return result.body.access_token;
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatResponse(res) {
|
function formatResponse(match) {
|
||||||
let result;
|
|
||||||
if (res.body.Tracks) {
|
|
||||||
result = res.body.Tracks.Items[0];
|
|
||||||
} else {
|
|
||||||
result = res.body.Albums.Items[0];
|
|
||||||
}
|
|
||||||
const item = {
|
const item = {
|
||||||
service: 'xbox',
|
service: 'xbox',
|
||||||
type: res.body.Tracks ? 'track' : 'album',
|
type: match.Album ? 'track' : 'album',
|
||||||
id: result.Id,
|
id: match.Id,
|
||||||
name: result.Name,
|
name: match.Name,
|
||||||
streamUrl: result.Link,
|
streamUrl: match.Link,
|
||||||
purchaseUrl: null,
|
purchaseUrl: null,
|
||||||
artwork: {
|
artwork: {
|
||||||
small: result.ImageUrl.replace('http://', 'https://') + '&w=250&h=250',
|
small: match.ImageUrl.replace('http://', 'https://') + '&w=250&h=250',
|
||||||
large: result.ImageUrl.replace('http://', 'https://') + '&w=500&h=500'
|
large: match.ImageUrl.replace('http://', 'https://') + '&w=500&h=500'
|
||||||
},
|
},
|
||||||
artist: {
|
artist: {
|
||||||
name: result.Artists[0].Artist.Name
|
name: match.Artists[0].Artist.Name
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
if (result.Album) {
|
if (match.Album) {
|
||||||
item.album = {name: result.Album.Name}
|
item.album = {name: match.Album.Name}
|
||||||
}
|
}
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
@ -84,9 +78,10 @@ export function* parseUrl(url) {
|
||||||
|
|
||||||
export function* lookupId(id, type) {
|
export function* lookupId(id, type) {
|
||||||
const path = '/' + id + '/lookup';
|
const path = '/' + id + '/lookup';
|
||||||
|
const apiType = type.charAt(0).toUpperCase() + type.substr(1) + 's';
|
||||||
try {
|
try {
|
||||||
const result = yield apiCall(path);
|
const result = yield apiCall(path);
|
||||||
return formatResponse(result);
|
return formatResponse(result.body[apiType].Items[0]);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e.status !== 404) {
|
if (e.status !== 404) {
|
||||||
debug(e.body);
|
debug(e.body);
|
||||||
|
@ -109,14 +104,41 @@ export function* search(data) {
|
||||||
query = cleanParam(data.artist.name.substring(0, data.artist.name.indexOf('&'))) + ' ' + cleanParam(data.name);
|
query = cleanParam(data.artist.name.substring(0, data.artist.name.indexOf('&'))) + ' ' + cleanParam(data.name);
|
||||||
album = data.albumName
|
album = data.albumName
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const name = data.name;
|
||||||
|
|
||||||
const path = '/music/search?q=' + encodeURIComponent(query) + '&filters=' + type + 's';
|
const path = '/music/search?q=' + encodeURIComponent(query) + '&filters=' + type + 's';
|
||||||
try {
|
const result = yield apiCall(path);
|
||||||
const result = yield apiCall(path);
|
|
||||||
return formatResponse(result);
|
const apiType = type.charAt(0).toUpperCase() + type.substr(1) + 's';
|
||||||
} catch (e) {
|
|
||||||
if (e.status !== 404) {
|
let match = exactMatch(name, data.artist.name, result.body[apiType].Items, type);
|
||||||
debug(e.body);
|
if (!match) {
|
||||||
}
|
match = looseMatch(name, data.artist.name, result.body[apiType].Items, type);
|
||||||
return {service: 'xbox'};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (match) {
|
||||||
|
return formatResponse(match);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {service: 'xbox'};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function exactMatch(item, artist, haystack, type) {
|
||||||
|
// try to find exact match
|
||||||
|
return haystack.find(function(entry) {
|
||||||
|
if (entry.Name === item && entry.Artists[0].Artist.Name === artist) {
|
||||||
|
return entry;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function looseMatch(item, artist, haystack, type) {
|
||||||
|
// try to find exact match
|
||||||
|
return haystack.find(function(entry) {
|
||||||
|
console.log(entry.Name, entry.Artists[0].Artist.Name)
|
||||||
|
if (entry.Name.indexOf(item) >= 0 && entry.Artists[0].Artist.Name.indexOf(artist) >= 0) {
|
||||||
|
return entry;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
|
@ -19,6 +19,12 @@ describe('Xbox Music', function(){
|
||||||
const result = yield xbox.search({type: 'album', artist: {name: 'Kyuss'}, name: 'Muchas Gracias: The Best Of Kyuss'});
|
const result = yield xbox.search({type: 'album', artist: {name: 'Kyuss'}, name: 'Muchas Gracias: The Best Of Kyuss'});
|
||||||
result.name.should.equal('Muchas Gracias: The Best Of Kyuss');
|
result.name.should.equal('Muchas Gracias: The Best Of Kyuss');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should find awkward album by search', function* (){
|
||||||
|
const result = yield xbox.search({type: 'album', artist: {name: 'Anavitória'}, name: 'Fica'});
|
||||||
|
result.name.should.equal('Fica');
|
||||||
|
result.artist.name.should.equal('Anavitória');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('lookupUrl', function(){
|
describe('lookupUrl', function(){
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue