Skip to content

Commit adc3cbf

Browse files
committed
Get playlist tracks, items count. Get playlist tracks paginated.
1 parent 23498ce commit adc3cbf

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

tidalapi/playlist.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from tidalapi.exceptions import ObjectNotFound, TooManyRequests
2626
from tidalapi.types import ItemOrder, JsonObj, OrderDirection
2727
from tidalapi.user import LoggedInUser
28+
from tidalapi.workers import get_items
2829

2930
if TYPE_CHECKING:
3031
from tidalapi.artist import Artist
@@ -161,6 +162,40 @@ def parse_factory(self, json_obj: JsonObj) -> "Playlist":
161162
self.parse(json_obj)
162163
return copy.copy(self.factory())
163164

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+
164199
def tracks(
165200
self,
166201
limit: Optional[int] = None,
@@ -195,6 +230,20 @@ def tracks(
195230
)
196231
)
197232

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+
198247
def items(
199248
self,
200249
limit: int = 100,

0 commit comments

Comments
 (0)