diff --git a/tests/mixins/test_browsing.py b/tests/mixins/test_browsing.py index f626d50..73aabfc 100644 --- a/tests/mixins/test_browsing.py +++ b/tests/mixins/test_browsing.py @@ -26,12 +26,13 @@ class TestBrowsing: def test_get_artist_albums(self, yt): artist = yt.get_artist("UCj5ZiBBqpe0Tg4zfKGHEFuQ") results = yt.get_artist_albums(artist["albums"]["browseId"], artist["albums"]["params"]) - assert len(results) > 0 - - def test_get_artist_singles(self, yt): - artist = yt.get_artist("UCAeLFBCQS7FvI8PvBrWvSBg") + assert len(results) == 100 results = yt.get_artist_albums(artist["singles"]["browseId"], artist["singles"]["params"]) - assert len(results) > 0 + assert len(results) < 100 + + artist = yt.get_artist("UC6LfFqHnWV8iF94n54jwYGw") + results = yt.get_artist_albums(artist["albums"]["browseId"], artist["albums"]["params"], limit=None) + assert len(results) >= 300 def test_get_user(self, yt): results = yt.get_user("UC44hbeRoCZVVMVg5z0FfIww") diff --git a/ytmusicapi/mixins/browsing.py b/ytmusicapi/mixins/browsing.py index a013e45..6035d90 100644 --- a/ytmusicapi/mixins/browsing.py +++ b/ytmusicapi/mixins/browsing.py @@ -252,12 +252,13 @@ class BrowsingMixin(MixinProtocol): artist.update(self.parser.parse_artist_contents(results)) return artist - def get_artist_albums(self, channelId: str, params: str) -> List[Dict]: + def get_artist_albums(self, channelId: str, params: str, limit: int | None = 100) -> List[Dict]: """ Get the full list of an artist's albums or singles :param channelId: browseId of the artist as returned by :py:func:`get_artist` :param params: params obtained by :py:func:`get_artist` + :param limit: Number of albums to return. `None` retrieves them all. Default: 100 :return: List of albums in the format of :py:func:`get_library_albums`, except artists key is missing. @@ -266,8 +267,17 @@ class BrowsingMixin(MixinProtocol): endpoint = "browse" response = self._send_request(endpoint, body) results = nav(response, SINGLE_COLUMN_TAB + SECTION_LIST_ITEM) - results = nav(results, GRID_ITEMS, True) or nav(results, CAROUSEL_CONTENTS) - albums = parse_albums(results) + contents = nav(results, GRID_ITEMS, True) or nav(results, CAROUSEL_CONTENTS) + albums = parse_albums(contents) + + results = nav(results, GRID, True) + if "continuations" in results: + request_func = lambda additionalParams: self._send_request(endpoint, body, additionalParams) + parse_func = lambda contents: parse_albums(contents) + remaining_limit = None if limit is None else (limit - len(albums)) + albums.extend( + get_continuations(results, "gridContinuation", remaining_limit, request_func, parse_func) + ) return albums