Move get_search_suggestions from browsing to search.

This commit is contained in:
Om Gupta 2023-04-05 11:21:05 +05:30 • committed by sigma67
commit 4fdad5be04
4 changed files with 115 additions and 112 deletions

View file

@ -5,7 +5,7 @@ from ytmusicapi.parsers.browsing import *
from ytmusicapi.parsers.albums import parse_album_header
from ytmusicapi.parsers.playlists import parse_playlist_items
from ytmusicapi.parsers.library import parse_albums
from typing import List, Dict, Union
from typing import List, Dict
class BrowsingMixin:
@ -814,88 +814,3 @@ class BrowsingMixin:
body = {'browseId': "FEmusic_home", "formData": formData}
self._send_request('browse', body)
def get_search_suggestions(self, query: str, detailed_runs=False) -> Union[List, List[Dict]]:
"""
Get Search Suggestions
:param query: Query string, i.e. 'faded'
:param detailed_runs: Whether to return detailed runs of each suggestion.
If True, it returns the query that the user typed and the remaining
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.
:return: List of search suggestion results depending on detailed_runs param.
Examples:
1.
Params:
query = 'fade'
detailed_runs = False
Response:
[
'faded',
'faded alan walker lyrics',
'faded alan walker',
'faded remix',
'faded song',
'faded lyrics',
'faded instrumental'
]
2.
Params:
query = 'fade'
detailed_runs = True
Response:
Runs with bold: True show the text typed by user & other runs contain the remaining suggestion.
[
{
'text': 'faded',
'runs': [
{
'text': 'fade',
'bold': True
},
{
'text': 'd'
}
]
},
{
'text': 'faded alan walker lyrics',
'runs': [
{
'text': 'fade',
'bold': True
},
{
'text': 'd alan walker lyrics'
}
]
},
{
'text': 'faded alan walker',
'runs': [
{
'text': 'fade',
'bold': True
},
{
'text': 'd alan walker'
}
]
},
...
]
"""
body = {'input': query}
endpoint = 'music/get_search_suggestions'
response = self._send_request(endpoint, body)
search_suggestions = parse_search_suggestions(response, detailed_runs)
return search_suggestions

View file

@ -1,4 +1,4 @@
from typing import List, Dict
from typing import List, Dict, Union
from ytmusicapi.continuations import get_continuations
from ytmusicapi.parsers.search import *
@ -203,12 +203,100 @@ class SearchMixin:
category))
if filter: # if filter is set, there are continuations
request_func = lambda additionalParams: self._send_request(
endpoint, body, additionalParams)
parse_func = lambda contents: parse_search_results(contents, type, category)
def request_func(additionalParams):
return self._send_request(endpoint, body, additionalParams)
def parse_func(contents):
return parse_search_results(contents, type, category)
search_results.extend(
get_continuations(res['musicShelfRenderer'], 'musicShelfContinuation',
limit - len(search_results), request_func, parse_func))
return search_results
def get_search_suggestions(self, query: str, detailed_runs=False) -> Union[List, List[Dict]]:
"""
Get Search Suggestions
:param query: Query string, i.e. 'faded'
:param detailed_runs: Whether to return detailed runs of each suggestion.
If True, it returns the query that the user typed and the remaining
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.
:return: List of search suggestion results depending on detailed_runs param.
Examples:
1.
Params:
query = 'fade'
detailed_runs = False
Response:
[
'faded',
'faded alan walker lyrics',
'faded alan walker',
'faded remix',
'faded song',
'faded lyrics',
'faded instrumental'
]
2.
Params:
query = 'fade'
detailed_runs = True
Response:
Runs with bold: True show the text typed by user & other runs contain the remaining suggestion.
[
{
'text': 'faded',
'runs': [
{
'text': 'fade',
'bold': True
},
{
'text': 'd'
}
]
},
{
'text': 'faded alan walker lyrics',
'runs': [
{
'text': 'fade',
'bold': True
},
{
'text': 'd alan walker lyrics'
}
]
},
{
'text': 'faded alan walker',
'runs': [
{
'text': 'fade',
'bold': True
},
{
'text': 'd alan walker'
}
]
},
...
]
"""
body = {'input': query}
endpoint = 'music/get_search_suggestions'
response = self._send_request(endpoint, body)
search_suggestions = parse_search_suggestions(response, detailed_runs)
return search_suggestions

View file

@ -147,25 +147,3 @@ def parse_watch_playlist(data):
'playlistId': nav(data, NAVIGATION_WATCH_PLAYLIST_ID),
'thumbnails': nav(data, THUMBNAIL_RENDERER),
}
def parse_search_suggestions(results, detailed_runs):
if not results.get('contents', [{}])[0].get('searchSuggestionsSectionRenderer', {}).get(
'contents', []):
return []
raw_suggestions = results['contents'][0]['searchSuggestionsSectionRenderer']['contents']
suggestions = []
for raw_suggestion in raw_suggestions:
suggestion_content = raw_suggestion['searchSuggestionRenderer']
text = suggestion_content['navigationEndpoint']['searchEndpoint']['query']
runs = suggestion_content['suggestion']['runs']
if detailed_runs:
suggestions.append({'text': text, 'runs': runs})
else:
suggestions.append(text)
return suggestions

View file

@ -198,3 +198,25 @@ def get_search_params(filter, scope, ignore_spelling):
def _get_param2(filter):
filter_params = {'songs': 'I', 'videos': 'Q', 'albums': 'Y', 'artists': 'g', 'playlists': 'o'}
return filter_params[filter]
def parse_search_suggestions(results, detailed_runs):
if not results.get('contents', [{}])[0].get('searchSuggestionsSectionRenderer', {}).get(
'contents', []):
return []
raw_suggestions = results['contents'][0]['searchSuggestionsSectionRenderer']['contents']
suggestions = []
for raw_suggestion in raw_suggestions:
suggestion_content = raw_suggestion['searchSuggestionRenderer']
text = suggestion_content['navigationEndpoint']['searchEndpoint']['query']
runs = suggestion_content['suggestion']['runs']
if detailed_runs:
suggestions.append({'text': text, 'runs': runs})
else:
suggestions.append(text)
return suggestions