Ruff lint (#496)

* add ruff, pdm

* update .gitignore

* format

* test

* check failure

* fix constants.py
This commit is contained in:
sigma67 2023-12-31 12:54:07 +01:00 • committed by GitHub
commit 1fd93dcfb6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
39 changed files with 1866 additions and 1234 deletions

View file

@ -1,16 +1,18 @@
from typing import List, Dict, Union
from typing import Dict, List, Union
from ytmusicapi.continuations import get_continuations
from ytmusicapi.parsers.search import *
class SearchMixin:
def search(self,
query: str,
filter: str = None,
scope: str = None,
limit: int = 20,
ignore_spelling: bool = False) -> List[Dict]:
def search(
self,
query: str,
filter: str = None,
scope: str = None,
limit: int = 20,
ignore_spelling: bool = False,
) -> List[Dict]:
"""
Search YouTube music
Returns results within the provided category.
@ -130,79 +132,94 @@ class SearchMixin:
"""
body = {'query': query}
endpoint = 'search'
body = {"query": query}
endpoint = "search"
search_results = []
filters = [
'albums', 'artists', 'playlists', 'community_playlists', 'featured_playlists', 'songs',
'videos', 'profiles', 'podcasts', 'episodes'
"albums",
"artists",
"playlists",
"community_playlists",
"featured_playlists",
"songs",
"videos",
"profiles",
"podcasts",
"episodes",
]
if filter and filter not in filters:
raise Exception(
"Invalid filter provided. Please use one of the following filters or leave out the parameter: "
+ ', '.join(filters))
+ ", ".join(filters)
)
scopes = ['library', 'uploads']
scopes = ["library", "uploads"]
if scope and scope not in scopes:
raise Exception(
"Invalid scope provided. Please use one of the following scopes or leave out the parameter: "
+ ', '.join(scopes))
+ ", ".join(scopes)
)
if scope == scopes[1] and filter:
raise Exception(
"No filter can be set when searching uploads. Please unset the filter parameter when scope is set to "
"uploads. ")
"uploads. "
)
if scope == scopes[0] and filter in filters[3:5]:
raise Exception(f"{filter} cannot be set when searching library. "
f"Please use one of the following filters or leave out the parameter: "
+ ', '.join(filters[0:3] + filters[5:]))
raise Exception(
f"{filter} cannot be set when searching library. "
f"Please use one of the following filters or leave out the parameter: "
+ ", ".join(filters[0:3] + filters[5:])
)
params = get_search_params(filter, scope, ignore_spelling)
if params:
body['params'] = params
body["params"] = params
response = self._send_request(endpoint, body)
# no results
if 'contents' not in response:
if "contents" not in response:
return search_results
if 'tabbedSearchResultsRenderer' in response['contents']:
if "tabbedSearchResultsRenderer" in response["contents"]:
tab_index = 0 if not scope or filter else scopes.index(scope) + 1
results = response['contents']['tabbedSearchResultsRenderer']['tabs'][tab_index][
'tabRenderer']['content']
results = response["contents"]["tabbedSearchResultsRenderer"]["tabs"][tab_index]["tabRenderer"][
"content"
]
else:
results = response['contents']
results = response["contents"]
results = nav(results, SECTION_LIST)
# no results
if len(results) == 1 and 'itemSectionRenderer' in results:
if len(results) == 1 and "itemSectionRenderer" in results:
return search_results
# set filter for parser
if filter and 'playlists' in filter:
filter = 'playlists'
if filter and "playlists" in filter:
filter = "playlists"
elif scope == scopes[1]:
filter = scopes[1]
for res in results:
if 'musicCardShelfRenderer' in res:
top_result = parse_top_result(res['musicCardShelfRenderer'],
self.parser.get_search_result_types())
if "musicCardShelfRenderer" in res:
top_result = parse_top_result(
res["musicCardShelfRenderer"], self.parser.get_search_result_types()
)
search_results.append(top_result)
if results := nav(res, ['musicCardShelfRenderer', 'contents'], True):
if results := nav(res, ["musicCardShelfRenderer", "contents"], True):
category = None
# category "more from youtube" is missing sometimes
if 'messageRenderer' in results[0]:
category = nav(results.pop(0), ['messageRenderer'] + TEXT_RUN_TEXT)
if "messageRenderer" in results[0]:
category = nav(results.pop(0), ["messageRenderer"] + TEXT_RUN_TEXT)
type = None
else:
continue
elif 'musicShelfRenderer' in res:
results = res['musicShelfRenderer']['contents']
elif "musicShelfRenderer" in res:
results = res["musicShelfRenderer"]["contents"]
type_filter = filter
category = nav(res, MUSIC_SHELF + TITLE_TEXT, True)
if not type_filter and scope == scopes[0]:
@ -214,8 +231,7 @@ class SearchMixin:
continue
search_result_types = self.parser.get_search_result_types()
search_results.extend(
parse_search_results(results, search_result_types, type, category))
search_results.extend(parse_search_results(results, search_result_types, type, category))
if filter: # if filter is set, there are continuations
@ -226,14 +242,18 @@ class SearchMixin:
return parse_search_results(contents, search_result_types, type, category)
search_results.extend(
get_continuations(res['musicShelfRenderer'], 'musicShelfContinuation',
limit - len(search_results), request_func, parse_func))
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[str], List[Dict]]:
def get_search_suggestions(self, query: str, detailed_runs=False) -> Union[List[str], List[Dict]]:
"""
Get Search Suggestions
@ -300,8 +320,8 @@ class SearchMixin:
]
"""
body = {'input': query}
endpoint = 'music/get_search_suggestions'
body = {"input": query}
endpoint = "music/get_search_suggestions"
response = self._send_request(endpoint, body)
search_suggestions = parse_search_suggestions(response, detailed_runs)