Skip to content

Commit

Permalink
✨ Result pagination implement for Downloader.
Browse files Browse the repository at this point in the history
  • Loading branch information
exislow committed Jan 18, 2024
1 parent 922b3d2 commit 1236298
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
14 changes: 8 additions & 6 deletions tidal_dl_ng/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from tidal_dl_ng.helper.decryption import decrypt_file, decrypt_security_token
from tidal_dl_ng.helper.exceptions import MediaMissing, MediaUnknown, UnknownManifestFormat
from tidal_dl_ng.helper.path import check_file_exists, format_path_media, path_file_sanitize
from tidal_dl_ng.helper.tidal import name_builder_item
from tidal_dl_ng.helper.tidal import items_results_all, name_builder_item
from tidal_dl_ng.helper.wrapper import WrapperLogger
from tidal_dl_ng.metadata import Metadata
from tidal_dl_ng.model.gui_data import ProgressBars
Expand Down Expand Up @@ -306,18 +306,20 @@ def items(
# Create file name and path
file_name_relative = format_path_media(file_template, media)

# TODO: Extend with pagination support: Iterate through `items` and `tracks`until len(returned list) == 0
# Get the items and name of the list.
# Get the name of the list and check, if videos should be included.
videos_include: bool = True

if isinstance(media, Mix):
items = media.items()
list_media_name = media.title[:30]
elif video_download:
items = media.items(limit=100)
list_media_name = media.name[:30]
else:
items = media.tracks(limit=999)
videos_include = False
list_media_name = media.name[:30]

# Get all items of the list.
items = items_results_all(media, videos_include=videos_include)

# Determine where to redirect the progress information.
if progress_gui is None:
progress_stdout: bool = True
Expand Down
6 changes: 3 additions & 3 deletions tidal_dl_ng/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import sys
from collections.abc import Callable

from helper.tidal import items_all_results, search_all_results
from helper.tidal import items_results_all, search_results_all

from tidal_dl_ng.helper.path import get_format_template

Expand Down Expand Up @@ -272,7 +272,7 @@ def populate_tree_results(self, results: [ResultSearch]):
self.tr_results.addTopLevelItem(child)

def search(self, query: str, types_media: SearchTypes) -> [ResultSearch]:
result_search: dict[str, [SearchTypes]] = search_all_results(
result_search: dict[str, [SearchTypes]] = search_results_all(
session=self.tidal.session, needle=query, types_media=types_media
)
result: [ResultSearch] = []
Expand Down Expand Up @@ -392,7 +392,7 @@ def list_items_show(self, media_list: Album | Playlist | None = None, point: QtC
media_list = item.data(3, QtCore.Qt.ItemDataRole.UserRole)

# Get all results
media_items: [Track | Video] = items_all_results(media_list)
media_items: [Track | Video] = items_results_all(media_list)
result: [ResultSearch] = self.search_result_to_model(media_items)

self.populate_tree_results(result)
Expand Down
10 changes: 7 additions & 3 deletions tidal_dl_ng/helper/tidal.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def get_tidal_media_type(url_media: str) -> MediaType | bool:
return result


def search_all_results(session: Session, needle: str, types_media: SearchTypes = None) -> dict[str, [SearchTypes]]:
def search_results_all(session: Session, needle: str, types_media: SearchTypes = None) -> dict[str, [SearchTypes]]:
limit: int = 300
offset: int = 0
done: bool = False
Expand Down Expand Up @@ -72,7 +72,7 @@ def search_all_results(session: Session, needle: str, types_media: SearchTypes =
return result


def items_all_results(media_list: [Mix | Playlist | Album]) -> [Track | Video]:
def items_results_all(media_list: [Mix | Playlist | Album], videos_include: bool = True) -> [Track | Video]:
limit: int = 100
offset: int = 0
done: bool = False
Expand All @@ -82,7 +82,11 @@ def items_all_results(media_list: [Mix | Playlist | Album]) -> [Track | Video]:
result = media_list.items()
else:
while not done:
tmp_result: [Track | Video] = media_list.items(limit=limit, offset=offset)
tmp_result: [Track | Video] = (
media_list.items(limit=limit, offset=offset)
if videos_include
else media_list.tracks(limit=limit, offset=offset)
)

if bool(tmp_result):
result += tmp_result
Expand Down

0 comments on commit 1236298

Please sign in to comment.