tidal: modify pagination function (#2231)
authorJozef Kruszynski <60214390+jozefKruszynski@users.noreply.github.com>
Mon, 23 Jun 2025 14:10:17 +0000 (16:10 +0200)
committerGitHub <noreply@github.com>
Mon, 23 Jun 2025 14:10:17 +0000 (16:10 +0200)
tidal: Add cursor pagination to paginate api function

music_assistant/providers/tidal/__init__.py

index bb2e5de9b58d2cac817051f994e1cf3e29bf24ae..373c50651125b3a831ff4d56c08fa1bcc5fac84a 100644 (file)
@@ -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)