Implement get_search_suggestions method
This commit is contained in:
parent
28ae6b6c7a
commit
b0faaf9fee
2 changed files with 113 additions and 4 deletions
|
|
@ -5,7 +5,7 @@ from ytmusicapi.parsers.browsing import *
|
||||||
from ytmusicapi.parsers.albums import parse_album_header
|
from ytmusicapi.parsers.albums import parse_album_header
|
||||||
from ytmusicapi.parsers.playlists import parse_playlist_items
|
from ytmusicapi.parsers.playlists import parse_playlist_items
|
||||||
from ytmusicapi.parsers.library import parse_albums
|
from ytmusicapi.parsers.library import parse_albums
|
||||||
from typing import List, Dict
|
from typing import List, Dict, Union
|
||||||
|
|
||||||
|
|
||||||
class BrowsingMixin:
|
class BrowsingMixin:
|
||||||
|
|
@ -105,10 +105,12 @@ class BrowsingMixin:
|
||||||
|
|
||||||
section_list = nav(response, SINGLE_COLUMN_TAB + ['sectionListRenderer'])
|
section_list = nav(response, SINGLE_COLUMN_TAB + ['sectionListRenderer'])
|
||||||
if 'continuations' in section_list:
|
if 'continuations' in section_list:
|
||||||
request_func = lambda additionalParams: self._send_request(
|
|
||||||
endpoint, body, additionalParams)
|
|
||||||
|
|
||||||
parse_func = lambda contents: parse_mixed_content(contents)
|
def request_func(additionalParams):
|
||||||
|
return self._send_request(endpoint, body, additionalParams)
|
||||||
|
|
||||||
|
def parse_func(contents):
|
||||||
|
return parse_mixed_content(contents)
|
||||||
|
|
||||||
home.extend(
|
home.extend(
|
||||||
get_continuations(section_list, 'sectionListContinuation', limit - len(home),
|
get_continuations(section_list, 'sectionListContinuation', limit - len(home),
|
||||||
|
|
@ -812,3 +814,88 @@ class BrowsingMixin:
|
||||||
|
|
||||||
body = {'browseId': "FEmusic_home", "formData": formData}
|
body = {'browseId': "FEmusic_home", "formData": formData}
|
||||||
self._send_request('browse', body)
|
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
|
||||||
|
|
|
||||||
|
|
@ -147,3 +147,25 @@ def parse_watch_playlist(data):
|
||||||
'playlistId': nav(data, NAVIGATION_WATCH_PLAYLIST_ID),
|
'playlistId': nav(data, NAVIGATION_WATCH_PLAYLIST_ID),
|
||||||
'thumbnails': nav(data, THUMBNAIL_RENDERER),
|
'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
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue