Fix for KeyError when searching community_playlists or featured_playlists. Closes #420 (#421)

* Fix for issue #420 KeyError when searching community_playlists or featured_playlists with scope library

* From PR feedback - better exception message for search guard clause.

* Fixed lint
This commit is contained in:
nick42d 2023-08-05 18:23:23 +08:00 • committed by GitHub
commit 3bf7b49eab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 2 deletions

View file

@ -137,6 +137,18 @@ class TestYTMusic(unittest.TestCase):
self.assertGreaterEqual(len(results), 1)
results = self.yt_auth.search("garrix", filter="playlists", scope="library")
self.assertGreaterEqual(len(results), 1)
self.assertRaises(Exception,
self.yt_auth.search,
"beatles",
filter="community_playlists",
scope="library",
limit=40)
self.assertRaises(Exception,
self.yt_auth.search,
"beatles",
filter="featured_playlists",
scope="library",
limit=40)
def test_get_artist(self):
results = self.yt.get_artist("MPLAUCmMUZbaYdNH0bEd1PAlAqsA")
@ -164,7 +176,7 @@ class TestYTMusic(unittest.TestCase):
def test_get_artist_singles(self):
artist = self.yt.get_artist("UCAeLFBCQS7FvI8PvBrWvSBg")
results = self.yt.get_artist_albums(artist["singles"]["browseId"],
artist["singles"]["params"])
artist["singles"]["params"])
self.assertGreater(len(results), 0)
def test_get_user(self):

View file

@ -19,8 +19,10 @@ class SearchMixin:
:param filter: Filter for item types. Allowed values: ``songs``, ``videos``, ``albums``, ``artists``, ``playlists``, ``community_playlists``, ``featured_playlists``, ``uploads``.
Default: Default search, including all types of items.
:param scope: Search scope. Allowed values: ``library``, ``uploads``.
For uploads, no filter can be set! An exception will be thrown if you attempt to do so.
Default: Search the public YouTube Music catalogue.
Changing scope from the default will reduce the number of settable filters. Setting a filter that is not permitted will throw an exception.
For uploads, no filter can be set.
For library, community_playlists and featured_playlists filter cannot be set.
:param limit: Number of search results to return
Default: 20
:param ignore_spelling: Whether to ignore YTM spelling suggestions.
@ -143,6 +145,11 @@ class SearchMixin:
"No filter can be set when searching uploads. Please unset the filter parameter when scope is set to "
"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:]))
params = get_search_params(filter, scope, ignore_spelling)
if params:
body['params'] = params