search: fix missing playlistId for album results while authenticated (#659)

* search: fix missing playlistId for album results while authenticated

* deduplicate test
This commit is contained in:
sigma67 2024-10-07 20:28:56 +02:00 • committed by GitHub
commit 2c6c6b6504
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 14 additions and 12 deletions

View file

@ -13,17 +13,12 @@ class TestSearch:
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

View file

@ -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