diff options
| author | Robert Honz <exislow+github@gmail.com> | 2024-01-18 21:57:35 +0100 |
|---|---|---|
| committer | Robert Honz <exislow+github@gmail.com> | 2024-01-18 21:57:35 +0100 |
| commit | 922b3d265519e38e465b869379a0accfa16431c5 (patch) | |
| tree | 519e72c74c28c8599a12ab745a9085a4b27d2921 /tidal_dl_ng/helper | |
| parent | caf1ad70dfd5a97dd2d94be274c3a62ad0185b3c (diff) | |
| download | tidal-dl-ng-922b3d265519e38e465b869379a0accfa16431c5.tar.gz | |
✨ Result pagination implement for GUI.
Diffstat (limited to 'tidal_dl_ng/helper')
| -rw-r--r-- | tidal_dl_ng/helper/tidal.py | 53 |
1 files changed, 52 insertions, 1 deletions
diff --git a/tidal_dl_ng/helper/tidal.py b/tidal_dl_ng/helper/tidal.py index 5c73b10..1a1fcc1 100644 --- a/tidal_dl_ng/helper/tidal.py +++ b/tidal_dl_ng/helper/tidal.py @@ -1,4 +1,5 @@ -from tidalapi import Track +from tidalapi import Album, Mix, Playlist, Session, Track, Video +from tidalapi.session import SearchTypes from tidal_dl_ng.constants import MediaType @@ -41,3 +42,53 @@ def get_tidal_media_type(url_media: str) -> MediaType | bool: result = MediaType.MIX return result + + +def search_all_results(session: Session, needle: str, types_media: SearchTypes = None) -> dict[str, [SearchTypes]]: + limit: int = 300 + offset: int = 0 + done: bool = False + result: dict[str, [SearchTypes]] = {} + + while not done: + tmp_result: dict[str, [SearchTypes]] = session.search( + query=needle, models=types_media, limit=limit, offset=offset + ) + tmp_done: bool = True + + for key, value in tmp_result.items(): + # Append pagination results, if there are any + if offset == 0: + result = tmp_result + tmp_done = False + elif bool(value): + result[key] += value + tmp_done = False + + # Next page + offset += limit + done = tmp_done + + return result + + +def items_all_results(media_list: [Mix | Playlist | Album]) -> [Track | Video]: + limit: int = 100 + offset: int = 0 + done: bool = False + result: [Track | Video] = [] + + if isinstance(media_list, Mix): + result = media_list.items() + else: + while not done: + tmp_result: [Track | Video] = media_list.items(limit=limit, offset=offset) + + if bool(tmp_result): + result += tmp_result + # Get the next page in the next iteration. + offset += limit + else: + done = True + + return result |
