|
25 | 25 | from tidalapi.exceptions import ObjectNotFound, TooManyRequests |
26 | 26 | from tidalapi.types import ItemOrder, JsonObj, OrderDirection |
27 | 27 | from tidalapi.user import LoggedInUser |
| 28 | +from tidalapi.workers import get_items |
28 | 29 |
|
29 | 30 | if TYPE_CHECKING: |
30 | 31 | from tidalapi.artist import Artist |
@@ -161,6 +162,40 @@ def parse_factory(self, json_obj: JsonObj) -> "Playlist": |
161 | 162 | self.parse(json_obj) |
162 | 163 | return copy.copy(self.factory()) |
163 | 164 |
|
| 165 | + def get_tracks_count( |
| 166 | + self, |
| 167 | + ) -> int: |
| 168 | + """Get the total number of tracks in the playlist. |
| 169 | +
|
| 170 | + This performs a minimal API request (limit=1) to fetch metadata about the tracks |
| 171 | + without retrieving all of them. The API response contains 'totalNumberOfItems', |
| 172 | + which represents the total items (tracks) available. |
| 173 | + :return: The number of items available. |
| 174 | + """ |
| 175 | + params = {"limit": 1, "offset": 0} |
| 176 | + |
| 177 | + json_obj = self.request.map_request( |
| 178 | + self._base_url % self.id + "/tracks", params=params |
| 179 | + ) |
| 180 | + return json_obj.get("totalNumberOfItems", 0) |
| 181 | + |
| 182 | + def get_items_count( |
| 183 | + self, |
| 184 | + ) -> int: |
| 185 | + """Get the total number of items in the playlist. |
| 186 | +
|
| 187 | + This performs a minimal API request (limit=1) to fetch metadata about the tracks |
| 188 | + without retrieving all of them. The API response contains 'totalNumberOfItems', |
| 189 | + which represents the total items (tracks) available. |
| 190 | + :return: The number of items available. |
| 191 | + """ |
| 192 | + params = {"limit": 1, "offset": 0} |
| 193 | + |
| 194 | + json_obj = self.request.map_request( |
| 195 | + self._base_url % self.id + "/items", params=params |
| 196 | + ) |
| 197 | + return json_obj.get("totalNumberOfItems", 0) |
| 198 | + |
164 | 199 | def tracks( |
165 | 200 | self, |
166 | 201 | limit: Optional[int] = None, |
@@ -195,6 +230,20 @@ def tracks( |
195 | 230 | ) |
196 | 231 | ) |
197 | 232 |
|
| 233 | + def tracks_paginated( |
| 234 | + self, |
| 235 | + order: Optional[ItemOrder] = None, |
| 236 | + order_direction: Optional[OrderDirection] = None, |
| 237 | + ) -> List["Playlist"]: |
| 238 | + """Get the tracks in the playlist, using pagination. |
| 239 | +
|
| 240 | + :param order: Optional; A :class:`ItemOrder` describing the ordering type when returning the user favorite tracks. eg.: "NAME, "DATE" |
| 241 | + :param order_direction: Optional; A :class:`OrderDirection` describing the ordering direction when sorting by `order`. eg.: "ASC", "DESC" |
| 242 | + :return: A :class:`list` :class:`~tidalapi.playlist.Playlist` objects containing the favorite tracks. |
| 243 | + """ |
| 244 | + count = self.get_tracks_count() |
| 245 | + return get_items(self.tracks, count, order, order_direction) |
| 246 | + |
198 | 247 | def items( |
199 | 248 | self, |
200 | 249 | limit: int = 100, |
|
0 commit comments