7
7
8
8
9
9
class SearchMixin (MixinProtocol ):
10
- def __init__ (self ):
11
- self ._latest_suggestions = None
12
- self ._latest_feedback_tokens = None
13
-
14
10
def search (
15
11
self ,
16
12
query : str ,
@@ -262,7 +258,7 @@ def parse_func(contents):
262
258
263
259
return search_results
264
260
265
- def get_search_suggestions (self , query : str , detailed_runs = False ) -> Union [list [str ], list [dict ]]:
261
+ def get_search_suggestions (self , query : str , detailed_runs = False ) -> tuple [ Union [list [str ], list [dict ]], dict [ int , str ]]:
266
262
"""
267
263
Get Search Suggestions
268
264
@@ -272,7 +268,10 @@ def get_search_suggestions(self, query: str, detailed_runs=False) -> Union[list[
272
268
suggestion along with the complete text (like many search services
273
269
usually bold the text typed by the user).
274
270
Default: False, returns the list of search suggestions in plain text.
275
- :return: List of search suggestion results depending on ``detailed_runs`` param.
271
+ :return: A tuple containing:
272
+ - A list of search suggestions. If ``detailed_runs`` is False, it returns plain text suggestions.
273
+ If ``detailed_runs`` is True, it returns a list of dictionaries with detailed information.
274
+ - A dictionary of feedback tokens that can be used to remove suggestions later.
276
275
277
276
Example response when ``query`` is 'fade' and ``detailed_runs`` is set to ``False``::
278
277
@@ -300,7 +299,7 @@ def get_search_suggestions(self, query: str, detailed_runs=False) -> Union[list[
300
299
"text": "d"
301
300
}
302
301
],
303
- "number ": 1
302
+ "index ": 0
304
303
},
305
304
{
306
305
"text": "faded alan walker lyrics",
@@ -313,7 +312,7 @@ def get_search_suggestions(self, query: str, detailed_runs=False) -> Union[list[
313
312
"text": "d alan walker lyrics"
314
313
}
315
314
],
316
- "number ": 2
315
+ "index ": 1
317
316
},
318
317
{
319
318
"text": "faded alan walker",
@@ -326,7 +325,7 @@ def get_search_suggestions(self, query: str, detailed_runs=False) -> Union[list[
326
325
"text": "d alan walker"
327
326
}
328
327
],
329
- "number ": 3
328
+ "index ": 2
330
329
},
331
330
...
332
331
]
@@ -340,36 +339,33 @@ def get_search_suggestions(self, query: str, detailed_runs=False) -> Union[list[
340
339
feedback_tokens : dict [int , str ] = {}
341
340
search_suggestions = parse_search_suggestions (response , detailed_runs , feedback_tokens )
342
341
343
- # Store the suggestions and feedback tokens for later use
344
- self ._latest_suggestions = search_suggestions
345
- self ._latest_feedback_tokens = feedback_tokens
346
-
347
- return search_suggestions
342
+ return search_suggestions , feedback_tokens
348
343
349
- def remove_search_suggestion (self , number : int ) -> bool :
344
+ def remove_search_suggestion (self , index : int , feedback_tokens : dict [ int , str ] ) -> bool :
350
345
"""
351
346
Remove a search suggestion from the user search history based on the number displayed next to it.
352
347
353
- :param number : The number of the suggestion to be removed.
348
+ :param index : The number of the suggestion to be removed.
354
349
This number is displayed when the `detailed_runs` and `display_numbers` parameters are set to True
355
350
in the `get_search_suggestions` method.
351
+ :param feedback_tokens: A dictionary containing feedback tokens for each suggestion.
352
+ This dictionary is obtained from the `get_search_suggestions` method.
356
353
:return: True if the operation was successful, False otherwise.
357
354
358
- Example usage:
355
+ Example usage::
359
356
360
- # Assuming you want to remove suggestion number 1
361
- success = ytmusic.remove_search_suggestion(number=1)
357
+ # Assuming you want to remove suggestion number 0
358
+ suggestions, feedback_tokens = ytmusic.get_search_suggestions(query="fade", detailed_runs=True)
359
+ success = ytmusic.remove_search_suggestion(index=0, feedback_tokens=feedback_tokens)
362
360
if success:
363
361
print("Suggestion removed successfully")
364
362
else:
365
363
print("Failed to remove suggestion")
366
364
"""
367
- if self ._latest_suggestions is None or self ._latest_feedback_tokens is None :
368
- raise ValueError (
369
- "No suggestions available. Please run get_search_suggestions first to retrieve suggestions."
370
- )
371
-
372
- feedback_token = self ._latest_feedback_tokens .get (number )
365
+ if not feedback_tokens :
366
+ raise YTMusicUserError ("No feedback tokens provided. Please run get_search_suggestions first to retrieve suggestions." )
367
+
368
+ feedback_token = feedback_tokens .get (index )
373
369
374
370
if not feedback_token :
375
371
return False
@@ -379,7 +375,4 @@ def remove_search_suggestion(self, number: int) -> bool:
379
375
380
376
response = self ._send_request (endpoint , body )
381
377
382
- if "feedbackResponses" in response and response ["feedbackResponses" ][0 ].get ("isProcessed" , False ):
383
- return True
384
-
385
- return False
378
+ return bool (nav (response , ["feedbackResponses" , 0 , "isProcessed" ], none_if_absent = True ))
0 commit comments