Skip to content

Commit

Permalink
make indices optional
Browse files Browse the repository at this point in the history
  • Loading branch information
sigma67 committed Dec 17, 2024
1 parent aa3a41f commit 4801d3a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
5 changes: 2 additions & 3 deletions tests/mixins/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,8 @@ def test_remove_search_suggestions_valid(self, yt_auth):
assert len(results) > 0, "No search suggestions returned"
assert any(item.get("fromHistory") for item in results), "No suggestions from history found"

suggestion_to_remove = [0]
response = yt_auth.remove_search_suggestions(results, suggestion_to_remove)
assert response is True, "Failed to remove search suggestion"
response = yt_auth.remove_search_suggestions(results)
assert response is True, "Failed to remove search suggestions"

def test_remove_search_suggestions_errors(self, yt_auth, yt):
first_pass = yt_auth.search("a")
Expand Down
17 changes: 11 additions & 6 deletions ytmusicapi/mixins/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ def get_search_suggestions(self, query: str, detailed_runs=False) -> Union[list[
"text": "d alan walker lyrics"
}
],
"fromHistory": true,
"fromHistory": false,
"feedbackToken": None
},
{
Expand All @@ -338,20 +338,22 @@ def get_search_suggestions(self, query: str, detailed_runs=False) -> Union[list[

return parse_search_suggestions(response, detailed_runs)

def remove_search_suggestions(self, suggestions: list[dict[str, Any]], indices: list[int]) -> bool:
def remove_search_suggestions(
self, suggestions: list[dict[str, Any]], indices: Optional[list[int]] = None
) -> bool:
"""
Remove a search suggestion from the user search history based on the number displayed next to it.
Remove search suggestion from the user search history.
:param suggestions: The dictionary obtained from the :py:func:`get_search_suggestions`
(with detailed_runs=True)`
:param indices: The indices of the suggestions to be removed.
:param indices: Optional. The indices of the suggestions to be removed. Default: remove all suggestions.
:return: True if the operation was successful, False otherwise.
Example usage::
# Assuming you want to remove suggestion number 0
# Removing suggestion number 0
suggestions = ytmusic.get_search_suggestions(query="fade", detailed_runs=True)
success = ytmusic.remove_search_suggestion(suggestions=suggestions, index=0)
success = ytmusic.remove_search_suggestions(suggestions=suggestions, indices=[0])
if success:
print("Suggestion removed successfully")
else:
Expand All @@ -364,6 +366,9 @@ def remove_search_suggestions(self, suggestions: list[dict[str, Any]], indices:
"Ensure that you have searched a similar term before."
)

if indices is None:
indices = list(range(len(suggestions)))

if any(index >= len(suggestions) for index in indices):
raise YTMusicUserError("Index out of range. Index must be smaller than the length of suggestions")

Expand Down

0 comments on commit 4801d3a

Please sign in to comment.