diff --git a/tests/mixins/test_search.py b/tests/mixins/test_search.py index ab8745da..4d5c1808 100644 --- a/tests/mixins/test_search.py +++ b/tests/mixins/test_search.py @@ -13,17 +13,12 @@ def test_search_exceptions(self, yt_auth): yt_auth.search(query, scope="upload") @pytest.mark.parametrize("query", ["Monekes", "llwlwl", "heun"]) - def test_search_queries(self, yt, yt_brand, query: str) -> None: - results = yt_brand.search(query) - assert ["resultType" in r for r in results] == [True] * len(results) - assert len(results) >= 5 - assert not any( - artist["name"].lower() in ALL_RESULT_TYPES - for result in results - if "artists" in result - for artist in result["artists"] - ) + @pytest.mark.parametrize("yt_instance", ["yt", "yt_brand"]) + def test_search_queries(self, query: str, yt_instance: str, request: pytest.FixtureRequest) -> None: + yt: YTMusic = request.getfixturevalue(yt_instance) results = yt.search(query) + assert all(album["playlistId"] is not None for album in results if album["resultType"] == "album") + assert ["resultType" in r for r in results] == [True] * len(results) assert len(results) >= 5 assert not any( artist["name"].lower() in ALL_RESULT_TYPES diff --git a/ytmusicapi/parsers/search.py b/ytmusicapi/parsers/search.py index fcd34f3e..a8b87078 100644 --- a/ytmusicapi/parsers/search.py +++ b/ytmusicapi/parsers/search.py @@ -47,7 +47,8 @@ def parse_top_result(data, search_result_types): if result_type in ["album"]: search_result["browseId"] = nav(data, TITLE + NAVIGATION_BROWSE_ID, True) - search_result["playlistId"] = nav(data, ["buttons", 0, "buttonRenderer", "command", *WATCH_PID], True) + button_command = nav(data, ["buttons", 0, "buttonRenderer", "command"], True) + search_result["playlistId"] = parse_album_playlistid_if_exists(button_command) if result_type in ["playlist"]: search_result["playlistId"] = nav(data, MENU_PLAYLIST_ID) @@ -94,7 +95,8 @@ def parse_search_result(data, search_result_types, result_type, category): elif result_type == "album": search_result["type"] = get_item_text(data, 1) - search_result["playlistId"] = nav(data, [*PLAY_BUTTON, "playNavigationEndpoint", *WATCH_PID], True) + play_navigation = nav(data, [*PLAY_BUTTON, "playNavigationEndpoint"], True) + search_result["playlistId"] = parse_album_playlistid_if_exists(play_navigation) elif result_type == "playlist": flex_item = get_flex_column_item(data, 1)["text"]["runs"] @@ -179,6 +181,11 @@ def parse_search_result(data, search_result_types, result_type, category): return search_result +def parse_album_playlistid_if_exists(data: dict[str, Any]) -> Optional[str]: + """the content of the data changes based on whether the user is authenticated or not""" + return nav(data, WATCH_PID, True) or nav(data, WATCH_PLAYLIST_ID, True) if data else None + + def parse_search_results(results, search_result_types, resultType=None, category=None): return [ parse_search_result(result[MRLIR], search_result_types, resultType, category) for result in results