From 1b94985014dc8eb5df4a2de3cdca75c883e0615e Mon Sep 17 00:00:00 2001 From: sigma67 Date: Mon, 26 Sep 2022 10:36:02 +0200 Subject: [PATCH] retrieve all continuations when limit parameter is None (closes #300) Squashed commit of the following: commit 3f447e39a403bea746b18e79cd7a9177a20ef79c Merge: 8746a2a c31c8da Author: Adrian Date: Sun Sep 25 12:17:44 2022 -0400 Merge branch 'master' of https://github.com/adrinu/ytmusicapi into retrieve-all-playlists commit 8746a2af5bdc12e5e16a87b6c69d39dde8712390 Author: Adrian Date: Sun Sep 25 12:17:06 2022 -0400 Limit to other funcs commit 5b69523003b6d0c2a00e31e1ed4a022efeabee65 Merge: b7a3e75 d36d85d Author: Adrian Date: Sun Sep 25 12:07:45 2022 -0400 Merge branch 'retrieve-all-playlists' of https://github.com/Auzzy/ytmusicapi into retrieve-all-playlists commit c31c8dafae0c805dee68d2df19b3fe1ac454d707 Author: Adrian Date: Sun Sep 25 11:35:05 2022 -0400 Limit is none implementation commit 6768f1d66de832c17b5557c910f4b2a353386690 Merge: b7a3e75 d36d85d Author: Adrian Date: Sun Sep 25 01:06:59 2022 -0400 Merge branch 'retrieve-all-playlists' of https://github.com/Auzzy/ytmusicapi into retrieve-all-playlists commit d36d85de9b3dff08a9997ca712817724b01608a3 Author: Austin Noto-Moniz Date: Mon May 30 16:55:53 2022 -0400 get_library_playlists: When limit is None, return them all. --- tests/test.py | 51 ++++++++++++++++++++++++++++------- ytmusicapi/continuations.py | 2 +- ytmusicapi/mixins/library.py | 7 ++--- ytmusicapi/mixins/uploads.py | 14 +++++----- ytmusicapi/parsers/library.py | 8 +++--- 5 files changed, 60 insertions(+), 22 deletions(-) diff --git a/tests/test.py b/tests/test.py index 0896a24..418bc8f 100644 --- a/tests/test.py +++ b/tests/test.py @@ -230,6 +230,17 @@ class TestYTMusic(unittest.TestCase): playlists = self.yt_auth.get_library_playlists(50) self.assertGreater(len(playlists), 25) + def test_get_all_library_playlists(self): + current_length = len(self.yt_auth.get_library_playlists(25)) + expected_length = current_length + while expected_length <= current_length: + expected_length = current_length + 1 + current_length = len(self.yt_auth.get_library_playlists(expected_length)) + expected_length -= 1 + + playlists = self.yt_auth.get_library_playlists(None) + self.assertEqual(len(playlists), expected_length) + def test_get_library_songs(self): songs = self.yt_brand.get_library_songs(100) self.assertGreaterEqual(len(songs), 100) @@ -374,8 +385,15 @@ class TestYTMusic(unittest.TestCase): ############### def test_get_library_upload_songs(self): - results = self.yt_auth.get_library_upload_songs(50, order='z_to_a') - self.assertGreater(len(results), 25) + current_length = len(self.yt_auth.get_library_upload_songs(25)) + expected_length = current_length + while expected_length <= current_length: + expected_length = current_length + 1 + current_length = len(self.yt_auth.get_library_upload_songs(expected_length)) + expected_length -= 1 + + songs = self.yt_auth.get_library_upload_songs(None) + self.assertEqual(len(songs), expected_length) @unittest.skip("Must not have any uploaded songs to pass") def test_get_library_upload_songs_empty(self): @@ -383,8 +401,15 @@ class TestYTMusic(unittest.TestCase): self.assertEquals(len(results), 0) def test_get_library_upload_albums(self): - results = self.yt_auth.get_library_upload_albums(50, order='a_to_z') - self.assertGreater(len(results), 40) + current_length = len(self.yt_auth.get_library_upload_album(25)) + expected_length = current_length + while expected_length <= current_length: + expected_length = current_length + 1 + current_length = len(self.yt_auth.get_library_upload_album(expected_length)) + expected_length -= 1 + + albums = self.yt_auth.get_library_upload_album(None) + self.assertEqual(len(albums), expected_length) @unittest.skip("Must not have any uploaded albums to pass") def test_get_library_upload_albums_empty(self): @@ -392,14 +417,22 @@ class TestYTMusic(unittest.TestCase): self.assertEquals(len(results), 0) def test_get_library_upload_artists(self): - results = self.yt_auth.get_library_upload_artists(50) - self.assertGreater(len(results), 25) + current_length = len(self.yt_auth.get_library_upload_artists(25)) + expected_length = current_length + while expected_length <= current_length: + expected_length = current_length + 1 + current_length = len(self.yt_auth.get_library_upload_artists(expected_length)) + expected_length -= 1 + + artists = self.yt_auth.get_library_upload_artists(None) + self.assertEqual(len(artists), expected_length) + results = self.yt_auth.get_library_upload_artists(50, order='a_to_z') - self.assertGreater(len(results), 25) + self.assertGreaterEqual(len(results), current_length) results = self.yt_auth.get_library_upload_artists(50, order='z_to_a') - self.assertGreater(len(results), 25) + self.assertGreaterEqual(len(results), current_length) results = self.yt_auth.get_library_upload_artists(50, order='recently_added') - self.assertGreater(len(results), 25) + self.assertGreaterEqual(len(results), current_length) @unittest.skip("Must not have any uploaded artsts to pass") def test_get_library_upload_artists_empty(self): diff --git a/ytmusicapi/continuations.py b/ytmusicapi/continuations.py index aedd8d5..97a4885 100644 --- a/ytmusicapi/continuations.py +++ b/ytmusicapi/continuations.py @@ -3,7 +3,7 @@ from ytmusicapi.navigation import nav def get_continuations(results, continuation_type, limit, request_func, parse_func, ctoken_path=""): items = [] - while 'continuations' in results and len(items) < limit: + while 'continuations' in results and (limit is None or len(items) < limit): additionalParams = get_continuation_params(results, ctoken_path) response = request_func(additionalParams) if 'continuationContents' in response: diff --git a/ytmusicapi/mixins/library.py b/ytmusicapi/mixins/library.py index 212601d..f2081ee 100644 --- a/ytmusicapi/mixins/library.py +++ b/ytmusicapi/mixins/library.py @@ -9,7 +9,7 @@ class LibraryMixin: """ Retrieves the playlists in the user's library. - :param limit: Number of playlists to retrieve + :param limit: Number of playlists to retrieve. `None` retrieves them all. :return: List of owned playlists. Each item is in the following format:: @@ -35,9 +35,10 @@ class LibraryMixin: request_func = lambda additionalParams: self._send_request( endpoint, body, additionalParams) parse_func = lambda contents: parse_content_list(contents, parse_playlist) + remaining_limit = None if limit is None else (limit - len(playlists)) playlists.extend( - get_continuations(results, 'gridContinuation', limit - len(playlists), - request_func, parse_func)) + get_continuations(results, 'gridContinuation', remaining_limit, request_func, + parse_func)) return playlists diff --git a/ytmusicapi/mixins/uploads.py b/ytmusicapi/mixins/uploads.py index 7eb3e12..d4aee56 100644 --- a/ytmusicapi/mixins/uploads.py +++ b/ytmusicapi/mixins/uploads.py @@ -17,7 +17,7 @@ class UploadsMixin: """ Returns a list of uploaded songs - :param limit: How many songs to return. Default: 25 + :param limit: How many songs to return. `None` retrieves them all. Default: 25 :param order: Order of songs to return. Allowed values: 'a_to_z', 'z_to_a', 'recently_added'. Default: Default order. :return: List of uploaded songs. @@ -58,9 +58,10 @@ class UploadsMixin: if 'continuations' in results: request_func = lambda additionalParams: self._send_request( endpoint, body, additionalParams) + remaining_limit = None if limit is None else (limit - len(songs)) songs.extend( - get_continuations(results, 'musicShelfContinuation', limit - len(songs), - request_func, parse_uploaded_items)) + get_continuations(results, 'musicShelfContinuation', remaining_limit, request_func, + parse_uploaded_items)) return songs @@ -68,7 +69,7 @@ class UploadsMixin: """ Gets the albums of uploaded songs in the user's library. - :param limit: Number of albums to return. Default: 25 + :param limit: Number of albums to return. `None` retrives them all. Default: 25 :param order: Order of albums to return. Allowed values: 'a_to_z', 'z_to_a', 'recently_added'. Default: Default order. :return: List of albums as returned by :py:func:`get_library_albums` """ @@ -87,7 +88,7 @@ class UploadsMixin: """ Gets the artists of uploaded songs in the user's library. - :param limit: Number of artists to return. Default: 25 + :param limit: Number of artists to return. `None` retrieves them all. Default: 25 :param order: Order of artists to return. Allowed values: 'a_to_z', 'z_to_a', 'recently_added'. Default: Default order. :return: List of artists as returned by :py:func:`get_library_artists` """ @@ -143,8 +144,9 @@ class UploadsMixin: request_func = lambda additionalParams: self._send_request( endpoint, body, additionalParams) parse_func = lambda contents: parse_uploaded_items(contents) + remaining_limit = None if limit is None else (limit - len(items)) items.extend( - get_continuations(results, 'musicShelfContinuation', limit, request_func, + get_continuations(results, 'musicShelfContinuation', remaining_limit, request_func, parse_func)) return items diff --git a/ytmusicapi/parsers/library.py b/ytmusicapi/parsers/library.py index e1e5162..5bdd884 100644 --- a/ytmusicapi/parsers/library.py +++ b/ytmusicapi/parsers/library.py @@ -34,8 +34,9 @@ def parse_library_albums(response, request_func, limit): if 'continuations' in results: parse_func = lambda contents: parse_albums(contents) + remaining_limit = None if limit is None else (limit - len(albums)) albums.extend( - get_continuations(results, 'gridContinuation', limit - len(albums), request_func, + get_continuations(results, 'gridContinuation', remaining_limit, request_func, parse_func)) return albums @@ -92,9 +93,10 @@ def parse_library_artists(response, request_func, limit): if 'continuations' in results: parse_func = lambda contents: parse_artists(contents) + remaining_limit = None if limit is None else (limit - len(artists)) artists.extend( - get_continuations(results, 'musicShelfContinuation', limit - len(artists), - request_func, parse_func)) + get_continuations(results, 'musicShelfContinuation', remaining_limit, request_func, + parse_func)) return artists