From 216940a2b83de695526f6f2f9f81ae4868e62336 Mon Sep 17 00:00:00 2001 From: "Renato \"Lond\" Cerqueira" Date: Sun, 17 Jan 2021 16:40:16 +0100 Subject: [PATCH] Add extra logic to match albums and tracks to improve match --- lib/services/ytmusic/index.js | 11 +++++++++-- test/services/ytmusic.js | 6 ++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/services/ytmusic/index.js b/lib/services/ytmusic/index.js index 24b4161..9e16713 100644 --- a/lib/services/ytmusic/index.js +++ b/lib/services/ytmusic/index.js @@ -76,8 +76,15 @@ export async function search(data, original = {}) { } const matches = parse_result_content(result.musicShelfRenderer.contents, data.type) - if (matches[0]) { - return await lookupId(matches[0], data.type) + // This could probably be done without extra lookups, but it would involve parsing deeply the response. + // If there's some kind of rate limit on ytmusic's side, this is a good play to start refactoring + for (const match of matches) { + const possibleMatch = await lookupId(match, data.type) + const nameMatch = possibleMatch.name == data.name; + const artistMatch = data.artist.name == "" ? possibleMatch.artist.name === 'Various Artists' : data.artist.name == possibleMatch.artist.name; + if (nameMatch && artistMatch) { + return possibleMatch + } } } debug("Finished looking up, no results") diff --git a/test/services/ytmusic.js b/test/services/ytmusic.js index e89ac79..ad10834 100644 --- a/test/services/ytmusic.js +++ b/test/services/ytmusic.js @@ -27,6 +27,12 @@ describe('ytmusic', function(){ result.id.should.equal('MPREb_iZt1VjORlv7'); }); + it('should find album and make sure it makes sense by search', async function(){ + const result = await ytmusic.search({type: 'album', artist: {name: 'The Beatles'}, name: 'The Beatles'}); + result.name.should.equal('The Beatles'); + result.id.should.equal('MPREb_S5TiUIYvI78'); + }); + it('should find track by search', async function (){ const result = await ytmusic.search({type: 'track', artist: {name: 'Oasis'}, albumName: 'Stop The Clocks', name: 'Wonderwall'}); result.name.should.equal('Wonderwall');