From 33098ba69eac6c3e5fe88ed502cd3da000996536 Mon Sep 17 00:00:00 2001 From: sigma67 <16363825+sigma67@users.noreply.github.com> Date: Mon, 27 May 2024 21:16:39 +0200 Subject: [PATCH] =?UTF-8?q?get=5Fhome:=20remove=20"year"=20attribute=20fro?= =?UTF-8?q?m=20album=20results,=20parse=20podcasts=20=E2=80=A6=20(#591)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * get_home: remove "year" attribute from album results, parse podcasts and episodes * search: make itemCount int cast error proof * fix None case for parse_album year * fix lint --- ytmusicapi/mixins/browsing.py | 1 - ytmusicapi/parsers/browsing.py | 20 ++++++++++++++------ ytmusicapi/parsers/podcasts.py | 2 +- ytmusicapi/parsers/search.py | 6 +++--- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/ytmusicapi/mixins/browsing.py b/ytmusicapi/mixins/browsing.py index 1c50bb8..f447ce6 100644 --- a/ytmusicapi/mixins/browsing.py +++ b/ytmusicapi/mixins/browsing.py @@ -35,7 +35,6 @@ def get_home(self, limit=3) -> List[Dict]: "contents": [ { //album result "title": "Sentiment", - "year": "Said The Sky", "browseId": "MPREb_QtqXtd2xZMR", "thumbnails": [...] }, diff --git a/ytmusicapi/parsers/browsing.py b/ytmusicapi/parsers/browsing.py index 3ed32ec..945c0ca 100644 --- a/ytmusicapi/parsers/browsing.py +++ b/ytmusicapi/parsers/browsing.py @@ -1,3 +1,4 @@ +from .podcasts import parse_episode, parse_podcast from .songs import * @@ -30,11 +31,14 @@ def parse_mixed_content(rows): content = parse_related_artist(data) elif page_type == "MUSIC_PAGE_TYPE_PLAYLIST": content = parse_playlist(data) - else: - data = nav(result, [MRLIR], True) - if not data: - continue + elif page_type == "MUSIC_PAGE_TYPE_PODCAST_SHOW_DETAIL_PAGE": + content = parse_podcast(data) + elif data := nav(result, [MRLIR], True): content = parse_song_flat(data) + elif data := nav(result, [MMRIR], True): + content = parse_episode(data) + else: + continue contents.append(content) @@ -51,10 +55,9 @@ def parse_content_list(results, parse_func, key=MTRIR): def parse_album(result): - return { + album = { "title": nav(result, TITLE_TEXT), "type": nav(result, SUBTITLE), - "year": nav(result, SUBTITLE2, True), "artists": [parse_id_name(x) for x in nav(result, ["subtitle", "runs"]) if "navigationEndpoint" in x], "browseId": nav(result, TITLE + NAVIGATION_BROWSE_ID), "audioPlaylistId": nav(result, THUMBNAIL_OVERLAY, True), @@ -62,6 +65,11 @@ def parse_album(result): "isExplicit": nav(result, SUBTITLE_BADGE_LABEL, True) is not None, } + if (year := nav(result, SUBTITLE2, True)) and year.isnumeric(): + album["year"] = year + + return album + def parse_single(result): return { diff --git a/ytmusicapi/parsers/podcasts.py b/ytmusicapi/parsers/podcasts.py index ab48d1c..b782b80 100644 --- a/ytmusicapi/parsers/podcasts.py +++ b/ytmusicapi/parsers/podcasts.py @@ -129,7 +129,7 @@ def parse_podcast(data): """Parses a single podcast under "Podcasts" on a channel page""" return { "title": nav(data, TITLE_TEXT), - "channel": parse_id_name(nav(data, [*SUBTITLE_RUNS, 0])), + "channel": parse_id_name(nav(data, [*SUBTITLE_RUNS, 0], True)), "browseId": nav(data, TITLE + NAVIGATION_BROWSE_ID), "podcastId": nav(data, THUMBNAIL_OVERLAY, True), "thumbnails": nav(data, THUMBNAIL_RENDERER), diff --git a/ytmusicapi/parsers/search.py b/ytmusicapi/parsers/search.py index c2484d2..8b96bea 100644 --- a/ytmusicapi/parsers/search.py +++ b/ytmusicapi/parsers/search.py @@ -98,9 +98,9 @@ def parse_search_result(data, search_result_types, result_type, category): elif result_type == "playlist": flex_item = get_flex_column_item(data, 1)["text"]["runs"] has_author = len(flex_item) == default_offset + 3 - search_result["itemCount"] = to_int( - get_item_text(data, 1, default_offset + has_author * 2).split(" ")[0] - ) + search_result["itemCount"] = get_item_text(data, 1, default_offset + has_author * 2).split(" ")[0] + if search_result["itemCount"] and search_result["itemCount"].isnumeric(): + search_result["itemCount"] = to_int(search_result["itemCount"]) search_result["author"] = None if not has_author else get_item_text(data, 1, default_offset) elif result_type == "station":