From b0faaf9fee878af5320d1c94dcd5977fd117162b Mon Sep 17 00:00:00 2001 From: Om Gupta Date: Wed, 5 Apr 2023 02:49:12 +0530 Subject: [PATCH] Implement get_search_suggestions method --- ytmusicapi/mixins/browsing.py | 95 ++++++++++++++++++++++++++++++++-- ytmusicapi/parsers/browsing.py | 22 ++++++++ 2 files changed, 113 insertions(+), 4 deletions(-) diff --git a/ytmusicapi/mixins/browsing.py b/ytmusicapi/mixins/browsing.py index 9934f31..ff5e9b2 100644 --- a/ytmusicapi/mixins/browsing.py +++ b/ytmusicapi/mixins/browsing.py @@ -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 +from typing import List, Dict, Union class BrowsingMixin: @@ -105,10 +105,12 @@ class BrowsingMixin: section_list = nav(response, SINGLE_COLUMN_TAB + ['sectionListRenderer']) 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( get_continuations(section_list, 'sectionListContinuation', limit - len(home), @@ -812,3 +814,88 @@ 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 diff --git a/ytmusicapi/parsers/browsing.py b/ytmusicapi/parsers/browsing.py index c221cd9..308f859 100644 --- a/ytmusicapi/parsers/browsing.py +++ b/ytmusicapi/parsers/browsing.py @@ -147,3 +147,25 @@ 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