Merge pull request #26 from exislow/dl-wait-track
✨ Respect download delay when downloading a bunch of tracks / videos.
This commit is contained in:
+8
-6
@@ -105,7 +105,7 @@ def login(ctx: typer.Context) -> bool:
|
|||||||
def download(
|
def download(
|
||||||
ctx: typer.Context,
|
ctx: typer.Context,
|
||||||
urls: Annotated[Optional[list[str]], typer.Argument()] = None,
|
urls: Annotated[Optional[list[str]], typer.Argument()] = None,
|
||||||
list_urls: Annotated[
|
file_urls: Annotated[
|
||||||
Optional[Path],
|
Optional[Path],
|
||||||
typer.Option(
|
typer.Option(
|
||||||
"--list",
|
"--list",
|
||||||
@@ -122,8 +122,8 @@ def download(
|
|||||||
):
|
):
|
||||||
if not urls:
|
if not urls:
|
||||||
# Read the text file provided.
|
# Read the text file provided.
|
||||||
if list_urls:
|
if file_urls:
|
||||||
text = list_urls.read_text()
|
text = file_urls.read_text()
|
||||||
urls = text.splitlines()
|
urls = text.splitlines()
|
||||||
else:
|
else:
|
||||||
print("Provide either URLs, IDs or a file containing URLs (one per line).")
|
print("Provide either URLs, IDs or a file containing URLs (one per line).")
|
||||||
@@ -154,6 +154,8 @@ def download(
|
|||||||
# Style Progress display.
|
# Style Progress display.
|
||||||
progress_table.add_row(Panel.fit(progress, title="Download Progress", border_style="green", padding=(2, 2)))
|
progress_table.add_row(Panel.fit(progress, title="Download Progress", border_style="green", padding=(2, 2)))
|
||||||
|
|
||||||
|
urls_pos_last = len(urls) - 1
|
||||||
|
|
||||||
for item in urls:
|
for item in urls:
|
||||||
media_type: MediaType | bool = False
|
media_type: MediaType | bool = False
|
||||||
|
|
||||||
@@ -173,10 +175,10 @@ def download(
|
|||||||
with Live(progress_table, refresh_per_second=10):
|
with Live(progress_table, refresh_per_second=10):
|
||||||
# Download media.
|
# Download media.
|
||||||
if media_type in [MediaType.TRACK, MediaType.VIDEO]:
|
if media_type in [MediaType.TRACK, MediaType.VIDEO]:
|
||||||
|
download_delay: bool = bool(settings.data.download_delay and urls.index(item) < urls_pos_last)
|
||||||
|
|
||||||
dl.item(
|
dl.item(
|
||||||
media_id=item_id,
|
media_id=item_id, media_type=media_type, file_template=file_template, download_delay=download_delay
|
||||||
media_type=media_type,
|
|
||||||
file_template=file_template,
|
|
||||||
)
|
)
|
||||||
elif media_type in [MediaType.ALBUM, MediaType.PLAYLIST, MediaType.MIX]:
|
elif media_type in [MediaType.ALBUM, MediaType.PLAYLIST, MediaType.MIX]:
|
||||||
dl.items(
|
dl.items(
|
||||||
|
|||||||
+11
-1
@@ -181,6 +181,7 @@ class Download:
|
|||||||
media_id: str = None,
|
media_id: str = None,
|
||||||
media_type: MediaType = None,
|
media_type: MediaType = None,
|
||||||
video_download: bool = True,
|
video_download: bool = True,
|
||||||
|
download_delay: bool = False,
|
||||||
) -> (bool, str):
|
) -> (bool, str):
|
||||||
# If no media instance is provided, we need to create the media instance.
|
# If no media instance is provided, we need to create the media instance.
|
||||||
if media_id and media_type:
|
if media_id and media_type:
|
||||||
@@ -241,6 +242,16 @@ class Download:
|
|||||||
else:
|
else:
|
||||||
self.fn_logger.debug(f"Download skipped, since file exists: '{path_file}'")
|
self.fn_logger.debug(f"Download skipped, since file exists: '{path_file}'")
|
||||||
|
|
||||||
|
status_download: bool = not download_skip
|
||||||
|
|
||||||
|
# If a file was downloaded and the download delay is enabled, wait until the next download.
|
||||||
|
# Only use this, if you have a list of several Track items. Do not use this for list items.
|
||||||
|
if download_delay and status_download:
|
||||||
|
time_sleep: float = round(random.SystemRandom().uniform(2, 5), 1)
|
||||||
|
|
||||||
|
self.fn_logger.debug(f"Next download will start in {time_sleep} seconds.")
|
||||||
|
time.sleep(time_sleep)
|
||||||
|
|
||||||
return not download_skip, path_file
|
return not download_skip, path_file
|
||||||
|
|
||||||
def cover_url(self, sid: str, dimension: CoverDimensions = CoverDimensions.Px320):
|
def cover_url(self, sid: str, dimension: CoverDimensions = CoverDimensions.Px320):
|
||||||
@@ -355,7 +366,6 @@ class Download:
|
|||||||
if download_delay and status_download:
|
if download_delay and status_download:
|
||||||
time_sleep: float = round(random.SystemRandom().uniform(2, 5), 1)
|
time_sleep: float = round(random.SystemRandom().uniform(2, 5), 1)
|
||||||
|
|
||||||
# TODO: Fix logging. Is not displayed in debug window.
|
|
||||||
self.fn_logger.debug(f"Next download will start in {time_sleep} seconds.")
|
self.fn_logger.debug(f"Next download will start in {time_sleep} seconds.")
|
||||||
time.sleep(time_sleep)
|
time.sleep(time_sleep)
|
||||||
|
|
||||||
|
|||||||
+11
-6
@@ -483,25 +483,30 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
|
|||||||
if len(items) == 0:
|
if len(items) == 0:
|
||||||
logger_gui.error("Please select a row first.")
|
logger_gui.error("Please select a row first.")
|
||||||
else:
|
else:
|
||||||
|
items_pos_last = len(items) - 1
|
||||||
|
|
||||||
for item in items:
|
for item in items:
|
||||||
media: Track | Album | Playlist | Video = item.data(5, QtCore.Qt.ItemDataRole.UserRole)
|
media: Track | Album | Playlist | Video = item.data(5, QtCore.Qt.ItemDataRole.UserRole)
|
||||||
|
# Skip only if Track item, skip option set and the item is not the last in the list.
|
||||||
|
download_delay: bool = bool(
|
||||||
|
isinstance(media, Track | Video)
|
||||||
|
and self.settings.data.download_delay
|
||||||
|
and items.index(item) < items_pos_last
|
||||||
|
)
|
||||||
|
|
||||||
self.download(media, self.dl)
|
self.download(media, self.dl, delay_track=download_delay)
|
||||||
|
|
||||||
self.b_download.setText("Download")
|
self.b_download.setText("Download")
|
||||||
self.b_download.setEnabled(True)
|
self.b_download.setEnabled(True)
|
||||||
|
|
||||||
def download(self, media: Track | Album | Playlist | Video | Mix, dl: Download) -> None:
|
def download(self, media: Track | Album | Playlist | Video | Mix, dl: Download, delay_track: bool = False) -> None:
|
||||||
self.s_pb_reset.emit()
|
self.s_pb_reset.emit()
|
||||||
self.s_statusbar_message.emit(StatusbarMessage(message="Download started..."))
|
self.s_statusbar_message.emit(StatusbarMessage(message="Download started..."))
|
||||||
|
|
||||||
file_template = get_format_template(media, self.settings)
|
file_template = get_format_template(media, self.settings)
|
||||||
|
|
||||||
if isinstance(media, Track | Video):
|
if isinstance(media, Track | Video):
|
||||||
dl.item(
|
dl.item(media=media, file_template=file_template, download_delay=delay_track)
|
||||||
media=media,
|
|
||||||
file_template=file_template,
|
|
||||||
)
|
|
||||||
elif isinstance(media, Album | Playlist | Mix):
|
elif isinstance(media, Album | Playlist | Mix):
|
||||||
dl.items(
|
dl.items(
|
||||||
media=media,
|
media=media,
|
||||||
|
|||||||
Reference in New Issue
Block a user