Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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