diff --git a/tests/mixins/test_search.py b/tests/mixins/test_search.py index c145323..4e4a645 100644 --- a/tests/mixins/test_search.py +++ b/tests/mixins/test_search.py @@ -1,5 +1,8 @@ import pytest +from ytmusicapi import YTMusic +from ytmusicapi.parsers.search import ALL_RESULT_TYPES + class TestSearch: def test_search_exceptions(self, yt_auth): @@ -14,13 +17,30 @@ class TestSearch: results = yt_brand.search(query) assert ["resultType" in r for r in results] == [True] * len(results) assert len(results) >= 10 + assert not any( + artist["name"].lower() in ALL_RESULT_TYPES + for result in results + if "artists" in result + for artist in result["artists"] + ) results = yt.search(query) assert len(results) >= 10 + assert not any( + artist["name"].lower() in ALL_RESULT_TYPES + for result in results + if "artists" in result + for artist in result["artists"] + ) def test_search_ignore_spelling(self, yt_auth): results = yt_auth.search("Martin Stig Andersen - Deteriation", ignore_spelling=True) assert len(results) > 0 + def test_search_localized(self): + yt_local = YTMusic(language="it") + results = yt_local.search("ABBA") + assert all(result["resultType"] in ALL_RESULT_TYPES for result in results) + def test_search_filters(self, yt_auth): query = "hip hop playlist" results = yt_auth.search(query, filter="songs") diff --git a/ytmusicapi/mixins/search.py b/ytmusicapi/mixins/search.py index cc01712..8113e4d 100644 --- a/ytmusicapi/mixins/search.py +++ b/ytmusicapi/mixins/search.py @@ -192,10 +192,10 @@ class SearchMixin(MixinProtocol): else: results = response["contents"] - results = nav(results, SECTION_LIST) + section_list = nav(results, SECTION_LIST) # no results - if len(results) == 1 and "itemSectionRenderer" in results: + if len(section_list) == 1 and "itemSectionRenderer" in section_list: return search_results # set filter for parser @@ -204,27 +204,23 @@ class SearchMixin(MixinProtocol): elif scope == scopes[1]: filter = scopes[1] - for res in results: + for res in section_list: if "musicCardShelfRenderer" in res: top_result = parse_top_result( res["musicCardShelfRenderer"], self.parser.get_search_result_types() ) search_results.append(top_result) - if results := nav(res, ["musicCardShelfRenderer", "contents"], True): - category = None - # category "more from youtube" is missing sometimes - if "messageRenderer" in results[0]: - category = nav(results.pop(0), ["messageRenderer", *TEXT_RUN_TEXT]) - type = None - else: + if not (shelf_contents := nav(res, ["musicCardShelfRenderer", "contents"], True)): continue + type = category = None + # if "more from youtube" is present, remove it - it's not parseable + if "messageRenderer" in shelf_contents[0]: + category = nav(shelf_contents.pop(0), ["messageRenderer", *TEXT_RUN_TEXT]) elif "musicShelfRenderer" in res: - results = res["musicShelfRenderer"]["contents"] - type_filter = filter + shelf_contents = res["musicShelfRenderer"]["contents"] category = nav(res, MUSIC_SHELF + TITLE_TEXT, True) - if not type_filter and scope == scopes[0]: - type_filter = category + type_filter = filter or category type = type_filter[:-1].lower() if type_filter else None @@ -232,7 +228,7 @@ class SearchMixin(MixinProtocol): continue search_result_types = self.parser.get_search_result_types() - search_results.extend(parse_search_results(results, search_result_types, type, category)) + search_results.extend(parse_search_results(shelf_contents, search_result_types, type, category)) if filter: # if filter is set, there are continuations diff --git a/ytmusicapi/parsers/search.py b/ytmusicapi/parsers/search.py index 2ed6e08..4088662 100644 --- a/ytmusicapi/parsers/search.py +++ b/ytmusicapi/parsers/search.py @@ -1,17 +1,19 @@ from ._utils import * from .songs import * +UNIQUE_RESULT_TYPES = ["artist", "playlist", "song", "video", "station", "profile", "podcast", "episode"] +ALL_RESULT_TYPES = ["album", *UNIQUE_RESULT_TYPES] + def get_search_result_type(result_type_local, result_types_local): if not result_type_local: return None - result_types = ["artist", "playlist", "song", "video", "station", "profile", "podcast", "episode"] result_type_local = result_type_local.lower() # default to album since it's labeled with multiple values ('Single', 'EP', etc.) if result_type_local not in result_types_local: result_type = "album" else: - result_type = result_types[result_types_local.index(result_type_local)] + result_type = UNIQUE_RESULT_TYPES[result_types_local.index(result_type_local)] return result_type @@ -39,7 +41,7 @@ def parse_top_result(data, search_result_types): search_result["title"] = nav(data, TITLE_TEXT) runs = nav(data, ["subtitle", "runs"]) - song_info = parse_song_runs(runs) + song_info = parse_song_runs(runs[2:]) search_result.update(song_info) if result_type in ["album"]: @@ -58,14 +60,29 @@ def parse_search_result(data, search_result_types, result_type, category): default_offset = (not result_type or result_type == "album") * 2 search_result = {"category": category} video_type = nav(data, [*PLAY_BUTTON, "playNavigationEndpoint", *NAVIGATION_VIDEO_TYPE], True) - if not result_type and video_type: - result_type = "song" if video_type == "MUSIC_VIDEO_TYPE_ATV" else "video" - result_type = ( - get_search_result_type(get_item_text(data, 1), search_result_types) - if not result_type - else result_type - ) + # try to determine the result type based on the first run + if result_type not in ALL_RESULT_TYPES: # i.e. localized result_type + result_type = get_search_result_type(get_item_text(data, 1), search_result_types) + + # determine result type based on browseId + # if there was no category title (i.e. for extra results in Top Result) + if not result_type: + if browse_id := nav(data, NAVIGATION_BROWSE_ID, True): + mapping = { + "VMPL": "playlist", + "RD": "playlist", + "MPLA": "artist", + "MPRE": "album", + "MPSP": "podcast", + "MPED": "episode", + } + result_type = next( + 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" + search_result["resultType"] = result_type if result_type != "artist": @@ -134,7 +151,8 @@ def parse_search_result(data, search_result_types, result_type, category): search_result["year"] = None flex_item = get_flex_column_item(data, 1) runs = flex_item["text"]["runs"] - song_info = parse_song_runs(runs) + runs_offset = (len(runs[0]) == 1) * 2 # ignore the first run if it is a type specifier (like "Song") + song_info = parse_song_runs(runs[runs_offset:]) search_result.update(song_info) if result_type in ["artist", "album", "playlist", "profile", "podcast"]: