Skip to content

Commit 4345ca2

Browse files
committed
Add support for get_*_count()
1 parent 998df78 commit 4345ca2

1 file changed

Lines changed: 79 additions & 8 deletions

File tree

tidalapi/user.py

Lines changed: 79 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,10 @@ def artists_paginated(
555555
:param order_direction: Optional; A :class:`OrderDirection` describing the ordering direction when sorting by `order`. eg.: "ASC", "DESC"
556556
:return: A :class:`list` :class:`~tidalapi.artist.Artist` objects containing the favorite artists.
557557
"""
558-
return get_items(self.session.user.favorites.artists, order, order_direction)
558+
count = self.session.user.favorites.get_artists_count()
559+
return get_items(
560+
self.session.user.favorites.artists, count, order, order_direction
561+
)
559562

560563
def artists(
561564
self,
@@ -587,6 +590,20 @@ def artists(
587590
),
588591
)
589592

593+
def get_artists_count(
594+
self,
595+
) -> int:
596+
"""Get the total number of artists in the user's collection.
597+
This performs a minimal API request (limit=1) to fetch metadata about
598+
the artists without retrieving all of them. The API response contains
599+
'totalNumberOfItems', which represents the total items (artists) available.
600+
:return: The number of items available.
601+
"""
602+
params = {"limit": 1, "offset": 0}
603+
604+
json_obj = self.requests.map_request(f"{self.base_url}/artists", params=params)
605+
return json_obj.get("totalNumberOfItems", 0)
606+
590607
def albums_paginated(
591608
self,
592609
order: Optional[AlbumOrder] = None,
@@ -598,7 +615,10 @@ def albums_paginated(
598615
:param order_direction: Optional; A :class:`OrderDirection` describing the ordering direction when sorting by `order`. eg.: "ASC", "DESC"
599616
:return: A :class:`list` :class:`~tidalapi.album.Album` objects containing the favorite albums.
600617
"""
601-
return get_items(self.session.user.favorites.albums, order, order_direction)
618+
count = self.session.user.favorites.get_artists_count()
619+
return get_items(
620+
self.session.user.favorites.albums, count, order, order_direction
621+
)
602622

603623
def albums(
604624
self,
@@ -628,19 +648,35 @@ def albums(
628648
),
629649
)
630650

651+
def get_albums_count(
652+
self,
653+
) -> int:
654+
"""Get the total number of albums in the user's collection.
655+
This performs a minimal API request (limit=1) to fetch metadata about
656+
the albums without retrieving all of them. The API response contains
657+
'totalNumberOfItems', which represents the total items (albums) available.
658+
:return: The number of items available.
659+
"""
660+
params = {"limit": 1, "offset": 0}
661+
662+
json_obj = self.requests.map_request(f"{self.base_url}/albums", params=params)
663+
return json_obj.get("totalNumberOfItems", 0)
664+
631665
def playlists_paginated(
632666
self,
633667
order: Optional[PlaylistOrder] = None,
634668
order_direction: Optional[OrderDirection] = None,
635669
) -> List["Playlist"]:
636-
"""Get the users favorite playlists relative to the root folder, using
637-
pagination.
670+
"""Get the users favorite playlists, using pagination.
638671
639672
:param order: Optional; A :class:`PlaylistOrder` describing the ordering type when returning the user favorite playlists. eg.: "NAME, "DATE"
640673
:param order_direction: Optional; A :class:`OrderDirection` describing the ordering direction when sorting by `order`. eg.: "ASC", "DESC"
641674
:return: A :class:`list` :class:`~tidalapi.playlist.Playlist` objects containing the favorite playlists.
642675
"""
643-
return get_items(self.session.user.favorites.playlists, order, order_direction)
676+
count = self.session.user.favorites.get_playlists_count()
677+
return get_items(
678+
self.session.user.favorites.playlists, count, order, order_direction
679+
)
644680

645681
def playlists(
646682
self,
@@ -724,19 +760,40 @@ def playlist_folders(
724760
),
725761
)
726762

763+
def get_playlists_count(self) -> int:
764+
"""Get the total number of playlists in the user's root collection.
765+
This performs a minimal API request (limit=1) to fetch metadata about
766+
the playlists without retrieving all of them. The API response contains
767+
'totalNumberOfItems', which represents the total playlists available.
768+
:return: The number of items available.
769+
"""
770+
params = {"folderId": "root", "offset": 0, "limit": 1, "includeOnly": ""}
771+
772+
endpoint = "my-collection/playlists/folders"
773+
json_obj = self.session.request.map_request(
774+
url=urljoin(
775+
self.session.config.api_v2_location,
776+
endpoint,
777+
),
778+
params=params,
779+
)
780+
return json_obj.get("totalNumberOfItems", 0)
781+
727782
def tracks_paginated(
728783
self,
729784
order: Optional[ItemOrder] = None,
730785
order_direction: Optional[OrderDirection] = None,
731786
) -> List["Playlist"]:
732-
"""Get the users favorite playlists relative to the root folder, using
733-
pagination.
787+
"""Get the users favorite tracks, using pagination.
734788
735789
:param order: Optional; A :class:`ItemOrder` describing the ordering type when returning the user favorite tracks. eg.: "NAME, "DATE"
736790
:param order_direction: Optional; A :class:`OrderDirection` describing the ordering direction when sorting by `order`. eg.: "ASC", "DESC"
737791
:return: A :class:`list` :class:`~tidalapi.playlist.Playlist` objects containing the favorite tracks.
738792
"""
739-
return get_items(self.session.user.favorites.tracks, order, order_direction)
793+
count = self.session.user.favorites.get_tracks_count()
794+
return get_items(
795+
self.session.user.favorites.tracks, count, order, order_direction
796+
)
740797

741798
def tracks(
742799
self,
@@ -766,6 +823,20 @@ def tracks(
766823
),
767824
)
768825

826+
def get_tracks_count(
827+
self,
828+
) -> int:
829+
"""Get the total number of tracks in the user's collection.
830+
This performs a minimal API request (limit=1) to fetch metadata about
831+
the tracks without retrieving all of them. The API response contains
832+
'totalNumberOfItems', which represents the total items (tracks) available.
833+
:return: The number of items available.
834+
"""
835+
params = {"limit": 1, "offset": 0}
836+
837+
json_obj = self.requests.map_request(f"{self.base_url}/tracks", params=params)
838+
return json_obj.get("totalNumberOfItems", 0)
839+
769840
def videos(
770841
self,
771842
limit: Optional[int] = None,

0 commit comments

Comments
 (0)