From 1dc46a2520173041607c768f06fb725e0ef750f5 Mon Sep 17 00:00:00 2001 From: jcbirdwell Date: Thu, 28 Dec 2023 19:46:31 -0600 Subject: [PATCH 1/3] search_suggestions: fix handle history when authenticated --- tests/test.py | 8 ++++++++ ytmusicapi/mixins/search.py | 7 +++++-- ytmusicapi/parsers/search.py | 15 ++++++++++++--- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/tests/test.py b/tests/test.py index 9aff93df..c5f2ec6c 100644 --- a/tests/test.py +++ b/tests/test.py @@ -270,6 +270,14 @@ def test_get_search_suggestions(self): result = self.yt.get_search_suggestions("fade", detailed_runs=True) self.assertGreaterEqual(len(result), 0) + def test_auth_search_suggestions(self): + # add search term to history + first_pass = self.yt_auth.search('b') + self.assertGreater(len(first_pass), 0) + # get results + results = self.yt_auth.get_search_suggestions('b', detailed_runs=True) + self.assertGreater(len(results), 0) + ################ # EXPLORE ################ diff --git a/ytmusicapi/mixins/search.py b/ytmusicapi/mixins/search.py index 0eeac063..351e1f95 100644 --- a/ytmusicapi/mixins/search.py +++ b/ytmusicapi/mixins/search.py @@ -233,7 +233,8 @@ def parse_func(contents): def get_search_suggestions(self, query: str, - detailed_runs=False) -> Union[List[str], List[Dict]]: + detailed_runs=False, + include_history=False) -> Union[List[str], List[Dict]]: """ Get Search Suggestions @@ -243,6 +244,8 @@ def get_search_suggestions(self, suggestion along with the complete text (like many search services usually bold the text typed by the user). Default: False, returns the list of search suggestions in plain text. + :param include_history: Whether to return suggestions based on historical/previously + searched items. Only relevant to detailed_runs. :return: List of search suggestion results depending on ``detailed_runs`` param. Example response when ``query`` is 'fade' and ``detailed_runs`` is set to ``False``:: @@ -304,6 +307,6 @@ def get_search_suggestions(self, endpoint = 'music/get_search_suggestions' response = self._send_request(endpoint, body) - search_suggestions = parse_search_suggestions(response, detailed_runs) + search_suggestions = parse_search_suggestions(response, detailed_runs, include_history) return search_suggestions diff --git a/ytmusicapi/parsers/search.py b/ytmusicapi/parsers/search.py index 96d724b0..ba804e15 100644 --- a/ytmusicapi/parsers/search.py +++ b/ytmusicapi/parsers/search.py @@ -5,7 +5,9 @@ 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_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: @@ -214,7 +216,7 @@ def _get_param2(filter): return filter_params[filter] -def parse_search_suggestions(results, detailed_runs): +def parse_search_suggestions(results, detailed_runs, history): if not results.get('contents', [{}])[0].get('searchSuggestionsSectionRenderer', {}).get( 'contents', []): return [] @@ -223,7 +225,14 @@ def parse_search_suggestions(results, detailed_runs): suggestions = [] for raw_suggestion in raw_suggestions: - suggestion_content = raw_suggestion['searchSuggestionRenderer'] + + if 'historySuggestionRenderer' in raw_suggestion: + if history: + suggestion_content = raw_suggestion['historySuggestionRenderer'] + else: + continue + else: + suggestion_content = raw_suggestion['searchSuggestionRenderer'] text = suggestion_content['navigationEndpoint']['searchEndpoint']['query'] runs = suggestion_content['suggestion']['runs'] From 7999df9c4107208444efabf174866c919e64789f Mon Sep 17 00:00:00 2001 From: jcbirdwell Date: Thu, 28 Dec 2023 19:55:42 -0600 Subject: [PATCH 2/3] search_suggestions: test historical option does something --- tests/test.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/test.py b/tests/test.py index c5f2ec6c..db983f4e 100644 --- a/tests/test.py +++ b/tests/test.py @@ -277,6 +277,11 @@ def test_auth_search_suggestions(self): # get results results = self.yt_auth.get_search_suggestions('b', detailed_runs=True) self.assertGreater(len(results), 0) + # get more results + historical = self.yt_auth.get_search_suggestions('b', + detailed_runs=True, + include_history=True) + self.assertGreater(len(historical), len(results)) ################ # EXPLORE From 89f2ee91c5e0ec9abdab1a26e0f99b7fd108857b Mon Sep 17 00:00:00 2001 From: sigma67 Date: Sun, 31 Dec 2023 13:15:53 +0100 Subject: [PATCH 3/3] remove optional param --- tests/test.py | 6 ++---- ytmusicapi/mixins/search.py | 8 ++------ ytmusicapi/parsers/search.py | 11 +++++------ 3 files changed, 9 insertions(+), 16 deletions(-) diff --git a/tests/test.py b/tests/test.py index 3a6d468d..e9a7f4b1 100644 --- a/tests/test.py +++ b/tests/test.py @@ -322,16 +322,14 @@ def test_get_search_suggestions(self): result = self.yt.get_search_suggestions("fade", detailed_runs=True) self.assertGreaterEqual(len(result), 0) - def test_auth_search_suggestions(self): # add search term to history first_pass = self.yt_auth.search("b") self.assertGreater(len(first_pass), 0) # get results results = self.yt_auth.get_search_suggestions("b", detailed_runs=True) self.assertGreater(len(results), 0) - # get more results - historical = self.yt_auth.get_search_suggestions("b", detailed_runs=True, include_history=True) - self.assertGreater(len(historical), len(results)) + self.assertTrue(any(item["fromHistory"] for item in results)) + self.assertTrue(any(not item["fromHistory"] for item in results)) ################ # EXPLORE diff --git a/ytmusicapi/mixins/search.py b/ytmusicapi/mixins/search.py index 8480754f..783a48b3 100644 --- a/ytmusicapi/mixins/search.py +++ b/ytmusicapi/mixins/search.py @@ -253,9 +253,7 @@ def parse_func(contents): return search_results - def get_search_suggestions( - self, query: str, detailed_runs=False, include_history=False - ) -> Union[List[str], List[Dict]]: + def get_search_suggestions(self, query: str, detailed_runs=False) -> Union[List[str], List[Dict]]: """ Get Search Suggestions @@ -265,8 +263,6 @@ def get_search_suggestions( suggestion along with the complete text (like many search services usually bold the text typed by the user). Default: False, returns the list of search suggestions in plain text. - :param include_history: Whether to return suggestions based on historical/previously - searched items. Only relevant to detailed_runs. :return: List of search suggestion results depending on ``detailed_runs`` param. Example response when ``query`` is 'fade' and ``detailed_runs`` is set to ``False``:: @@ -328,6 +324,6 @@ def get_search_suggestions( endpoint = "music/get_search_suggestions" response = self._send_request(endpoint, body) - search_suggestions = parse_search_suggestions(response, detailed_runs, include_history) + search_suggestions = parse_search_suggestions(response, detailed_runs) return search_suggestions diff --git a/ytmusicapi/parsers/search.py b/ytmusicapi/parsers/search.py index af6d10e4..05bde37c 100644 --- a/ytmusicapi/parsers/search.py +++ b/ytmusicapi/parsers/search.py @@ -211,7 +211,7 @@ def _get_param2(filter): return filter_params[filter] -def parse_search_suggestions(results, detailed_runs, history): +def parse_search_suggestions(results, detailed_runs): if not results.get("contents", [{}])[0].get("searchSuggestionsSectionRenderer", {}).get("contents", []): return [] @@ -220,18 +220,17 @@ def parse_search_suggestions(results, detailed_runs, history): for raw_suggestion in raw_suggestions: if "historySuggestionRenderer" in raw_suggestion: - if history: - suggestion_content = raw_suggestion["historySuggestionRenderer"] - else: - continue + suggestion_content = raw_suggestion["historySuggestionRenderer"] + from_history = True else: suggestion_content = raw_suggestion["searchSuggestionRenderer"] + from_history = False text = suggestion_content["navigationEndpoint"]["searchEndpoint"]["query"] runs = suggestion_content["suggestion"]["runs"] if detailed_runs: - suggestions.append({"text": text, "runs": runs}) + suggestions.append({"text": text, "runs": runs, "fromHistory": from_history}) else: suggestions.append(text)