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;
|
||||
}
|
||||
|
||||
function formatResponse(res) {
|
||||
let result;
|
||||
if (res.body.Tracks) {
|
||||
result = res.body.Tracks.Items[0];
|
||||
} else {
|
||||
result = res.body.Albums.Items[0];
|
||||
}
|
||||
function formatResponse(match) {
|
||||
const item = {
|
||||
service: 'xbox',
|
||||
type: res.body.Tracks ? 'track' : 'album',
|
||||
id: result.Id,
|
||||
name: result.Name,
|
||||
streamUrl: result.Link,
|
||||
type: match.Album ? 'track' : 'album',
|
||||
id: match.Id,
|
||||
name: match.Name,
|
||||
streamUrl: match.Link,
|
||||
purchaseUrl: null,
|
||||
artwork: {
|
||||
small: result.ImageUrl.replace('http://', 'https://') + '&w=250&h=250',
|
||||
large: result.ImageUrl.replace('http://', 'https://') + '&w=500&h=500'
|
||||
small: match.ImageUrl.replace('http://', 'https://') + '&w=250&h=250',
|
||||
large: match.ImageUrl.replace('http://', 'https://') + '&w=500&h=500'
|
||||
},
|
||||
artist: {
|
||||
name: result.Artists[0].Artist.Name
|
||||
name: match.Artists[0].Artist.Name
|
||||
}
|
||||
};
|
||||
if (result.Album) {
|
||||
item.album = {name: result.Album.Name}
|
||||
if (match.Album) {
|
||||
item.album = {name: match.Album.Name}
|
||||
}
|
||||
return item;
|
||||
}
|
||||
|
@ -84,9 +78,10 @@ export function* parseUrl(url) {
|
|||
|
||||
export function* lookupId(id, type) {
|
||||
const path = '/' + id + '/lookup';
|
||||
const apiType = type.charAt(0).toUpperCase() + type.substr(1) + 's';
|
||||
try {
|
||||
const result = yield apiCall(path);
|
||||
return formatResponse(result);
|
||||
return formatResponse(result.body[apiType].Items[0]);
|
||||
} catch (e) {
|
||||
if (e.status !== 404) {
|
||||
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);
|
||||
album = data.albumName
|
||||
}
|
||||
|
||||
const name = data.name;
|
||||
|
||||
const path = '/music/search?q=' + encodeURIComponent(query) + '&filters=' + type + 's';
|
||||
try {
|
||||
const result = yield apiCall(path);
|
||||
return formatResponse(result);
|
||||
} catch (e) {
|
||||
if (e.status !== 404) {
|
||||
debug(e.body);
|
||||
}
|
||||
return {service: 'xbox'};
|
||||
const result = yield apiCall(path);
|
||||
|
||||
const apiType = type.charAt(0).toUpperCase() + type.substr(1) + 's';
|
||||
|
||||
let match = exactMatch(name, data.artist.name, result.body[apiType].Items, type);
|
||||
if (!match) {
|
||||
match = looseMatch(name, data.artist.name, result.body[apiType].Items, type);
|
||||
}
|
||||
|
||||
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'});
|
||||
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(){
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue