From 4fdad5be04950a09a424358023bda9c2797f567d Mon Sep 17 00:00:00 2001 From: Om Gupta Date: Wed, 5 Apr 2023 11:21:05 +0530 Subject: [PATCH] Move get_search_suggestions from browsing to search. --- ytmusicapi/mixins/browsing.py | 87 +----------------------------- ytmusicapi/mixins/search.py | 96 ++++++++++++++++++++++++++++++++-- ytmusicapi/parsers/browsing.py | 22 -------- ytmusicapi/parsers/search.py | 22 ++++++++ 4 files changed, 115 insertions(+), 112 deletions(-) diff --git a/ytmusicapi/mixins/browsing.py b/ytmusicapi/mixins/browsing.py index ff5e9b2..e439e0b 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, 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 diff --git a/ytmusicapi/mixins/search.py b/ytmusicapi/mixins/search.py index 9b734d2..2d72969 100644 --- a/ytmusicapi/mixins/search.py +++ b/ytmusicapi/mixins/search.py @@ -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 diff --git a/ytmusicapi/parsers/browsing.py b/ytmusicapi/parsers/browsing.py index 308f859..c221cd9 100644 --- a/ytmusicapi/parsers/browsing.py +++ b/ytmusicapi/parsers/browsing.py @@ -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 diff --git a/ytmusicapi/parsers/search.py b/ytmusicapi/parsers/search.py index bf38653..b1a8d5f 100644 --- a/ytmusicapi/parsers/search.py +++ b/ytmusicapi/parsers/search.py @@ -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