Skip to content

Commit

Permalink
search: fix missing playlistId for album results while authenticated (#…
Browse files Browse the repository at this point in the history
…659)

* search: fix missing playlistId for album results while authenticated

* deduplicate test
  • Loading branch information
sigma67 authored Oct 7, 2024
1 parent e995ae5 commit 2c6c6b6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
15 changes: 5 additions & 10 deletions tests/mixins/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions ytmusicapi/parsers/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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"]
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2c6c6b6

Please sign in to comment.