From 3bf7b49eab96d05f97297efce1f0a74845ccaa61 Mon Sep 17 00:00:00 2001 From: nick42d <133559267+nick42d@users.noreply.github.com> Date: Sat, 5 Aug 2023 18:23:23 +0800 Subject: [PATCH] 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 --- tests/test.py | 14 +++++++++++++- ytmusicapi/mixins/search.py | 9 ++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/tests/test.py b/tests/test.py index 0180d2c..2fb6539 100644 --- a/tests/test.py +++ b/tests/test.py @@ -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): diff --git a/ytmusicapi/mixins/search.py b/ytmusicapi/mixins/search.py index 8a3fe19..d22cdb6 100644 --- a/ytmusicapi/mixins/search.py +++ b/ytmusicapi/mixins/search.py @@ -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