Merge pull request #354 from renatolond/fix_alternative_album_search

Use new way to transform playlist id into album id
This commit is contained in:
Jonathan Cremin 2021-06-17 16:52:29 +01:00 committed by GitHub
commit da1905850c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 26 deletions

View file

@ -214,35 +214,18 @@ async function lookupAlbum(id) {
} }
async function lookupPlaylist(id) { async function lookupPlaylist(id) {
let request_body = {enablePersistentPlaylistPanel: true, isAudioOnly: true, playlistId: id, ...standard_body} const endpoint = "https://music.youtube.com/playlist"
const { body } = await request.post("https://music.youtube.com/youtubei/v1/next") const response = await request.get(endpoint)
.set(standard_headers) .set(standard_headers)
.query(standard_params) .query({list: id})
.send(request_body) let match = response.text.match(/"MPRE[_a-zA-Z0-9]+/)
let albumId
// 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. if (match) {
// 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 albumId = match[0].substr(1)
// 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. } else {
const watchNextRenderer = body.contents.singleColumnMusicWatchNextResultsRenderer.tabbedRenderer.watchNextTabbedResultsRenderer debug("Couldn't match album id");
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!");
throw new Error(); 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) const possibleAlbum = await lookupAlbum(albumId)
if (possibleAlbum.playlistId = id) { if (possibleAlbum.playlistId = id) {
return possibleAlbum; return possibleAlbum;

View file

@ -72,6 +72,10 @@ describe('ytmusic', function(){
const result = await ytmusic.parseUrl('https://music.youtube.com/playlist?list=OLAK5uy_lx9K5RpiBEwd3E4C1GKqY7e06qTlwydvs'); const result = await ytmusic.parseUrl('https://music.youtube.com/playlist?list=OLAK5uy_lx9K5RpiBEwd3E4C1GKqY7e06qTlwydvs');
result.id.should.equal("MPREb_9C36yscfgmJ"); 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");
});
}); });
}); });
}); });