Skip to content

Commit

Permalink
get_playlist: support audio playlists
Browse files Browse the repository at this point in the history
  • Loading branch information
sgvictorino committed Dec 18, 2024
1 parent b86654f commit 7318787
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
2 changes: 2 additions & 0 deletions tests/data/2024_12_get_playlist_audio.json

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions tests/data/expected_output/2024_12_get_playlist_audio.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"id": "OLAK5uy_n0x1TMX8DL2eli2g_LysCSg-6Nq5YQa1g",
"title": "Revival",
"owned": false,
"trackCount": 19
}
1 change: 1 addition & 0 deletions tests/mixins/test_playlists.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class TestPlaylists:
"2024_03_get_playlist.json",
"2024_03_get_playlist_public.json",
"2024_07_get_playlist_collaborative.json",
"2024_12_get_playlist_audio.json",
],
)
def test_get_playlist(self, yt, test_file):
Expand Down
35 changes: 23 additions & 12 deletions ytmusicapi/mixins/playlists.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,35 +109,45 @@ def get_playlist(
endpoint = "browse"
response = self._send_request(endpoint, body)

header_data = nav(response, [*TWO_COLUMN_RENDERER, *TAB_CONTENT, *SECTION_LIST_ITEM])
header_data = nav(response, [*TWO_COLUMN_RENDERER, *TAB_CONTENT, *SECTION_LIST_ITEM], True) or {}
section_list = nav(response, [*TWO_COLUMN_RENDERER, "secondaryContents", *SECTION])
playlist: dict = {}
playlist["owned"] = EDITABLE_PLAYLIST_DETAIL_HEADER[0] in header_data
if not playlist["owned"]:

content_data = nav(section_list, [*CONTENT, "musicPlaylistShelfRenderer"])

header = None
if header_data and not playlist["owned"]:
header = nav(header_data, RESPONSIVE_HEADER)
playlist["id"] = nav(
header,
["buttons", 1, "musicPlayButtonRenderer", "playNavigationEndpoint", *WATCH_PLAYLIST_ID],
True,
)
playlist["privacy"] = "PUBLIC"
else:
elif header_data:
playlist["id"] = nav(header_data, [*EDITABLE_PLAYLIST_DETAIL_HEADER, *PLAYLIST_ID])
header = nav(header_data, [*EDITABLE_PLAYLIST_DETAIL_HEADER, *HEADER, *RESPONSIVE_HEADER])
playlist["privacy"] = header_data[EDITABLE_PLAYLIST_DETAIL_HEADER[0]]["editHeader"][
"musicPlaylistEditHeaderRenderer"
]["privacy"]

description_shelf = nav(header, ["description", *DESCRIPTION_SHELF], True)
playlist["description"] = (
"".join([run["text"] for run in description_shelf["description"]["runs"]])
if description_shelf
else None
)
if header:
description_shelf = nav(header, ["description", *DESCRIPTION_SHELF], True)
playlist["description"] = (
"".join([run["text"] for run in description_shelf["description"]["runs"]])
if description_shelf
else None
)

playlist.update(parse_playlist_header_meta(header))
playlist.update(parse_playlist_header_meta(header))

playlist.update(parse_song_runs(nav(header, SUBTITLE_RUNS)[2 + playlist["owned"] * 2 :]))
playlist.update(parse_song_runs(nav(header, SUBTITLE_RUNS)[2 + playlist["owned"] * 2 :]))
else:
playlist["id"] = nav(
content_data, [*CONTENT, MRLIR, *PLAY_BUTTON, "playNavigationEndpoint", *WATCH_PLAYLIST_ID]
)
playlist["trackCount"] = nav(content_data, ["collapsedItemCount"])

request_func = lambda additionalParams: self._send_request(endpoint, body, additionalParams)

Expand Down Expand Up @@ -175,7 +185,6 @@ def get_playlist(
)

playlist["tracks"] = []
content_data = nav(section_list, [*CONTENT, "musicPlaylistShelfRenderer"])
if "contents" in content_data:
playlist["tracks"] = parse_playlist_items(content_data["contents"])

Expand All @@ -187,6 +196,8 @@ def get_playlist(
)
)

playlist["title"] = playlist.get("title") or playlist["tracks"][0]["album"]["name"]

playlist["duration_seconds"] = sum_total_duration(playlist)
return playlist

Expand Down

0 comments on commit 7318787

Please sign in to comment.