From 050c56e6370f9e9bc258fccd4e2770798a984e18 Mon Sep 17 00:00:00 2001 From: Jozef Kruszynski <60214390+jozefKruszynski@users.noreply.github.com> Date: Mon, 23 Jun 2025 16:10:17 +0200 Subject: [PATCH] tidal: modify pagination function (#2231) tidal: Add cursor pagination to paginate api function --- music_assistant/providers/tidal/__init__.py | 25 ++++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/music_assistant/providers/tidal/__init__.py b/music_assistant/providers/tidal/__init__.py index bb2e5de9..373c5065 100644 --- a/music_assistant/providers/tidal/__init__.py +++ b/music_assistant/providers/tidal/__init__.py @@ -596,13 +596,22 @@ class TidalProvider(MusicProvider): item_key: str = "items", nested_key: str | None = None, limit: int = DEFAULT_LIMIT, + cursor_based: bool = False, **kwargs: Any, ) -> AsyncGenerator[Any, None]: """Paginate through all items from a Tidal API endpoint.""" offset = 0 + cursor = None + while True: # Get a batch of items - params = {"limit": limit, "offset": offset} + params = {"limit": limit} + if cursor_based: + if cursor: + params["cursor"] = cursor # Add cursor if available + else: + params["offset"] = offset # Use offset for offset-based pagination + if "params" in kwargs: params.update(kwargs.pop("params")) @@ -620,14 +629,15 @@ class TidalProvider(MusicProvider): yield item[nested_key] else: yield item + # Update cursor or offset for the next batch + if cursor_based: + cursor = response.get("cursor") # Update cursor from the response + if not cursor: + break # Stop if no next cursor is provided # Update offset for next batch offset += len(items) - # Stop if we've received fewer items than the limit - if len(items) < limit: - break - def _extract_data( self, api_result: dict[str, Any] | tuple[dict[str, Any], str] ) -> dict[str, Any]: @@ -1447,7 +1457,10 @@ class TidalProvider(MusicProvider): mix_path = "favorites/mixes" async for mix_item in self._paginate_api( - mix_path, item_key="items", base_url=self.BASE_URL_V2 + mix_path, + item_key="items", + base_url=self.BASE_URL_V2, + cursor_based=True, ): if mix_item and mix_item.get("id"): yield self._parse_playlist(mix_item, is_mix=True) -- 2.34.1