diff --git a/tests/mixins/test_search.py b/tests/mixins/test_search.py index cc60af6..72cdb67 100644 --- a/tests/mixins/test_search.py +++ b/tests/mixins/test_search.py @@ -112,13 +112,36 @@ class TestSearch: assert len(results) >= 3 assert all(item["resultType"] == "episode" for item in results) - def test_search_top_result(self, yt): + def test_search_episode_category(self, yt): + """Test resultType detection for episodes by searching for a podcast without a filter.""" + results = yt.search("Stanford Graduate School of Business") + episode = next( + item + for item in results + if item["category"] == "Episodes" and item["podcast"]["name"] == "Stanford GSB Podcasts" + ) + assert episode["resultType"] == "episode" + assert episode["podcast"]["id"] == "MPSPPLxq_lXOUlvQDUNyoBYLkN8aVt5yAwEtG9" + + def test_search_top_result_playlist(self, yt): results = yt.search("fdsfsfsd") # issue 524 assert results[0]["category"] == "Top result" assert results[0]["resultType"] == "playlist" assert results[0]["playlistId"].startswith("PL") assert len(results[0]["author"]) > 0 + def test_search_top_result_episode(self, yt): + results = yt.search( + "Stanford GSB Podcasts 124. Making Meetings Meaningful, Pt. 1: How to Structure and Organize More Effective Gatherings" + ) + assert results[0]["category"] == "Top result" + assert results[0]["resultType"] == "episode" + assert results[0]["videoId"] == "KNkyHCLOr1o" + assert results[0]["podcast"] == { + "id": "MPSPPLxq_lXOUlvQDUNyoBYLkN8aVt5yAwEtG9", + "name": "Stanford GSB Podcasts", + } + def test_search_uploads(self, config, yt, yt_oauth): with pytest.raises(Exception, match="No filter can be set when searching uploads"): yt.search( diff --git a/ytmusicapi/navigation.py b/ytmusicapi/navigation.py index 4c05da9..4304532 100644 --- a/ytmusicapi/navigation.py +++ b/ytmusicapi/navigation.py @@ -59,7 +59,8 @@ SUBTITLE3 = [*SUBTITLE_RUNS, 4, "text"] THUMBNAIL = ["thumbnail", "thumbnails"] THUMBNAILS = ["thumbnail", "musicThumbnailRenderer", *THUMBNAIL] THUMBNAIL_RENDERER = ["thumbnailRenderer", "musicThumbnailRenderer", *THUMBNAIL] -THUMBNAIL_OVERLAY = ["thumbnailOverlay", *OVERLAY_RENDERER, "playNavigationEndpoint", *WATCH_PID] +THUMBNAIL_OVERLAY_NAVIGATION = ["thumbnailOverlay", *OVERLAY_RENDERER, "playNavigationEndpoint"] +THUMBNAIL_OVERLAY = [*THUMBNAIL_OVERLAY_NAVIGATION, *WATCH_PID] THUMBNAIL_CROPPED = ["thumbnail", "croppedSquareThumbnailRenderer", *THUMBNAIL] FEEDBACK_TOKEN = ["feedbackEndpoint", "feedbackToken"] BADGE_PATH = [0, "musicInlineBadgeRenderer", "accessibilityData", "accessibilityData", "label"] diff --git a/ytmusicapi/parsers/search.py b/ytmusicapi/parsers/search.py index 0b960ab..088cf76 100644 --- a/ytmusicapi/parsers/search.py +++ b/ytmusicapi/parsers/search.py @@ -67,6 +67,13 @@ def parse_top_result(data: JsonDict, search_result_types: list[str]) -> JsonDict search_result["title"] = nav(data, TITLE_TEXT) search_result["author"] = parse_song_artists_runs(nav(data, ["subtitle", "runs"])[2:]) + if result_type in ["episode"]: + search_result["videoId"] = nav(data, [*THUMBNAIL_OVERLAY_NAVIGATION, *WATCH_VIDEO_ID]) + search_result["videoType"] = nav(data, [*THUMBNAIL_OVERLAY_NAVIGATION, *NAVIGATION_VIDEO_TYPE]) + runs = nav(data, SUBTITLE_RUNS)[2:] + search_result["date"] = runs[0]["text"] + search_result["podcast"] = parse_id_name(runs[2]) + search_result["thumbnails"] = nav(data, THUMBNAILS, True) return search_result @@ -96,7 +103,10 @@ def parse_search_result( iter(type for prefix, type in mapping.items() if browse_id.startswith(prefix)), None ) else: - result_type = "song" if video_type == "MUSIC_VIDEO_TYPE_ATV" else "video" + result_type = { + "MUSIC_VIDEO_TYPE_ATV": "song", + "MUSIC_VIDEO_TYPE_PODCAST_EPISODE": "episode", + }.get(video_type or "", "video") search_result["resultType"] = result_type @@ -191,12 +201,13 @@ def parse_search_result( if result_type in ["episode"]: flex_item = get_flex_column_item(data, 1) - has_date = int(len(nav(flex_item, TEXT_RUNS)) > 1) + runs = nav(flex_item, TEXT_RUNS)[default_offset:] + has_date = int(len(runs) > 1) search_result["live"] = bool(nav(data, ["badges", 0, "liveBadgeRenderer"], True)) if has_date: - search_result["date"] = nav(flex_item, TEXT_RUN_TEXT) + search_result["date"] = runs[0]["text"] - search_result["podcast"] = parse_id_name(nav(flex_item, ["text", "runs", has_date * 2])) + search_result["podcast"] = parse_id_name(runs[has_date * 2]) search_result["thumbnails"] = nav(data, THUMBNAILS, True)