From bf8b6eea52067a880dda6443fb733c5c6a5b8ab4 Mon Sep 17 00:00:00 2001 From: Jonathan Cremin Date: Sun, 7 May 2017 22:47:06 +0100 Subject: [PATCH] Improve Groove music matching --- lib/services/xbox/index.js | 72 +++++++++++++++++++++++++------------- test/services/xbox.js | 6 ++++ 2 files changed, 53 insertions(+), 25 deletions(-) diff --git a/lib/services/xbox/index.js b/lib/services/xbox/index.js index 491880b..5d6c8c6 100644 --- a/lib/services/xbox/index.js +++ b/lib/services/xbox/index.js @@ -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; + } + }); +} diff --git a/test/services/xbox.js b/test/services/xbox.js index c9b230e..dd60951 100644 --- a/test/services/xbox.js +++ b/test/services/xbox.js @@ -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(){