From 1b58d95551dbe08194d6888eaec416ca2f179ee9 Mon Sep 17 00:00:00 2001 From: "Renato \"Lond\" Cerqueira" Date: Tue, 8 Jun 2021 19:06:36 +0200 Subject: [PATCH] Use new way to transform playlist id into album id Following changes in https://github.com/sigma67/ytmusicapi/issues/186 --- lib/services/ytmusic/index.js | 35 +++++++++-------------------------- test/services/ytmusic.js | 4 ++++ 2 files changed, 13 insertions(+), 26 deletions(-) diff --git a/lib/services/ytmusic/index.js b/lib/services/ytmusic/index.js index 9e16713..2fbb71b 100644 --- a/lib/services/ytmusic/index.js +++ b/lib/services/ytmusic/index.js @@ -202,35 +202,18 @@ async function lookupAlbum(id) { } async function lookupPlaylist(id) { - let request_body = {enablePersistentPlaylistPanel: true, isAudioOnly: true, playlistId: id, ...standard_body} - const { body } = await request.post("https://music.youtube.com/youtubei/v1/next") + const endpoint = "https://music.youtube.com/playlist" + const response = await request.get(endpoint) .set(standard_headers) - .query(standard_params) - .send(request_body) - - // The playlist object is rather complex, but here's what I'm doing: I'll parse the very minimum to get to the first track. - // At that point, I'm going to check the id of the album in that track. And make a lookup on it. If the album looked up - // has the same playlist id as the one I'm looking up, it means it's an album and not a playlist and we're good. - const watchNextRenderer = body.contents.singleColumnMusicWatchNextResultsRenderer.tabbedRenderer.watchNextTabbedResultsRenderer - const firstTrack = watchNextRenderer.tabs[0].tabRenderer.content.musicQueueRenderer.content.playlistPanelRenderer.contents[0] - const runs = firstTrack.playlistPanelVideoRenderer.longBylineText.runs - const reverse_last_artist_idx = runs.reverse().findIndex((entry) => { - if (entry.navigationEndpoint === undefined) { - return false - } - return entry.navigationEndpoint.browseEndpoint.browseId.startsWith("UC") || - entry.navigationEndpoint.browseEndpoint.browseId.startsWith("FEmusic_library_privately_owned_artist") - }); - if (reverse_last_artist_idx == -1) { - debug("Could not find an artist. Implement extra logic from ytmusicapi!"); + .query({list: id}) + let match = response.text.match(/"MPRE[_a-zA-Z0-9]+/) + let albumId + if (match) { + albumId = match[0].substr(1) + } else { + debug("Couldn't match album id"); throw new Error(); } - const last_artist_idx = runs.length - reverse_last_artist_idx - 1; - if (runs.length - last_artist_idx != 5) { - debug("No album found, can't find this."); - throw new Error(); - } - const albumId = runs[last_artist_idx + 2].navigationEndpoint.browseEndpoint.browseId const possibleAlbum = await lookupAlbum(albumId) if (possibleAlbum.playlistId = id) { return possibleAlbum; diff --git a/test/services/ytmusic.js b/test/services/ytmusic.js index ad10834..fb390de 100644 --- a/test/services/ytmusic.js +++ b/test/services/ytmusic.js @@ -55,6 +55,10 @@ describe('ytmusic', function(){ const result = await ytmusic.parseUrl('https://music.youtube.com/playlist?list=OLAK5uy_lx9K5RpiBEwd3E4C1GKqY7e06qTlwydvs'); result.id.should.equal("MPREb_9C36yscfgmJ"); }); + it('should parse alternative album url into ID, regression', async function (){ + const result = await ytmusic.parseUrl('https://music.youtube.com/playlist?list=OLAK5uy_kxepMtCUKFek54-bgWICIsmglK86HD0TM'); + result.id.should.equal("MPREb_XmlDLpyWvMt"); + }); }); }); });