From d143480ae402b398633273af415fb9e46552daac Mon Sep 17 00:00:00 2001 From: fischcode <82546407+fischcode@users.noreply.github.com> Date: Tue, 27 Feb 2024 09:23:09 +0100 Subject: [PATCH] Fix parsing of album and views in parse_playlist_items (#552) * Fix parsing of album and views in parse_playlist_items Parse album and views columns as a function of flex column count and is_album property to cater for varying layouts with three and four flex columns * Add "own_playlist" attribute to playlists To allow for an easy distinction between owned / editable and community / non-editable playlists,in applications incorporating ytmusicapi, a boolean "own_playlist" dictionary entry is added to playlists based on existing code already identifying this property. * Add thumbnails to episodes Add thumbnails to episodes for visual rendering in Kodi etc. * Add unit test for playlist "own_playlist" attribut Add unit test for playlist "own_playlist" attribut * Improvements as requested - rename attribute for own playlists to playlist("owned") - filter out episode thumbnails with an non-functional URL * Fix linter in test_playlists.py Fix test for playlist["owned"] * Auto-Formatting on playlists.py * format parsers/podcasts.py * Remove filtering of podcast thumbnails No idea why I had seen useless images for those thumbnails before - maybe Google fixed something to make them work? --- tests/mixins/test_playlists.py | 2 ++ ytmusicapi/mixins/playlists.py | 1 + ytmusicapi/parsers/playlists.py | 10 ++++------ ytmusicapi/parsers/podcasts.py | 2 ++ 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/tests/mixins/test_playlists.py b/tests/mixins/test_playlists.py index 3ce433f..f0851ec 100644 --- a/tests/mixins/test_playlists.py +++ b/tests/mixins/test_playlists.py @@ -11,6 +11,7 @@ class TestPlaylists: assert len(playlist["duration"]) > 5 assert len(playlist["tracks"]) > 200 assert "suggestions" not in playlist + assert playlist["owned"] is False yt.get_playlist("RDATgXd-") assert len(playlist["tracks"]) >= 100 @@ -24,6 +25,7 @@ class TestPlaylists: assert len(playlist["tracks"]) < 100 assert len(playlist["suggestions"]) == 21 assert len(playlist["related"]) == 10 + assert playlist["owned"] is True def test_edit_playlist(self, config, yt_brand): playlist = yt_brand.get_playlist(config["playlists"]["own"]) diff --git a/ytmusicapi/mixins/playlists.py b/ytmusicapi/mixins/playlists.py index d38436b..b59b46f 100644 --- a/ytmusicapi/mixins/playlists.py +++ b/ytmusicapi/mixins/playlists.py @@ -116,6 +116,7 @@ class PlaylistsMixin(MixinProtocol): header = response["header"]["musicEditablePlaylistDetailHeaderRenderer"] playlist["privacy"] = header["editHeader"]["musicPlaylistEditHeaderRenderer"]["privacy"] header = header["header"]["musicDetailHeaderRenderer"] + playlist["owned"] = own_playlist playlist["title"] = nav(header, TITLE_TEXT) playlist["thumbnails"] = nav(header, THUMBNAIL_CROPPED) diff --git a/ytmusicapi/parsers/playlists.py b/ytmusicapi/parsers/playlists.py index bf0182c..ebbb7b9 100644 --- a/ytmusicapi/parsers/playlists.py +++ b/ytmusicapi/parsers/playlists.py @@ -44,15 +44,13 @@ def parse_playlist_items(results, menu_entries: Optional[List[List]] = None, is_ if title == "Song deleted": continue + flex_column_count = len(data["flexColumns"]) + artists = parse_song_artists(data, 1) - album = parse_song_album(data, 2) + album = parse_song_album(data, flex_column_count - 1) if not is_album else None - views = None - if album and album["id"] is None: - # views currently only present on albums and formatting is localization-dependent -> no parsing - if (views := (get_item_text(data, 2))) is not None: - album = None + views = get_item_text(data, 2) if flex_column_count == 4 or is_album else None duration = None if "fixedColumns" in data: diff --git a/ytmusicapi/parsers/podcasts.py b/ytmusicapi/parsers/podcasts.py index 388b87d..1883642 100644 --- a/ytmusicapi/parsers/podcasts.py +++ b/ytmusicapi/parsers/podcasts.py @@ -103,6 +103,7 @@ def parse_episodes(results) -> List[Dict]: episodes = [] for result in results: data = nav(result, ["musicMultiRowListItemRenderer"]) + thumbnails = nav(data, THUMBNAILS) if len(nav(data, SUBTITLE_RUNS)) == 1: duration = nav(data, SUBTITLE) else: @@ -124,6 +125,7 @@ def parse_episodes(results) -> List[Dict]: "browseId": browseId, "videoType": videoType, "date": date, + "thumbnails": thumbnails, } )