diff options
| author | Robert Honz <exislow+github@gmail.com> | 2025-03-03 09:31:37 +0100 |
|---|---|---|
| committer | Robert Honz <exislow+github@gmail.com> | 2025-03-03 09:31:37 +0100 |
| commit | 0684994850439bccb8c2a8f20df708a826d99654 (patch) | |
| tree | f84b6aca398e9d071e57aa3846e7cb7fd5cefdd0 /tidal_dl_ng/cli.py | |
| parent | 2aff7c920e9cddcd66df08e0e9299a9375c249d3 (diff) | |
| download | tidal-dl-ng-0684994850439bccb8c2a8f20df708a826d99654.tar.gz | |
✨ Downloads can be now paused / resumed. Fixes #149 & 324
👷 Shutdown is handled more gracefully.
Diffstat (limited to 'tidal_dl_ng/cli.py')
| -rw-r--r-- | tidal_dl_ng/cli.py | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/tidal_dl_ng/cli.py b/tidal_dl_ng/cli.py index e951fc5..9239977 100644 --- a/tidal_dl_ng/cli.py +++ b/tidal_dl_ng/cli.py @@ -1,9 +1,12 @@ #!/usr/bin/env python +import signal +import sys from collections.abc import Callable from pathlib import Path from typing import Annotated, Optional import typer +from config import HandlingApp from rich.console import Group from rich.live import Live from rich.progress import ( @@ -64,6 +67,7 @@ def _download(ctx: typer.Context, urls: list[str], try_login: bool = True) -> bo # Create initial objects. settings: Settings = Settings() + handling_app: HandlingApp = HandlingApp() progress: Progress = Progress( TextColumn("[progress.description]{task.description}"), SpinnerColumn(), @@ -92,8 +96,9 @@ def _download(ctx: typer.Context, urls: list[str], try_login: bool = True) -> bo fn_logger=fn_logger, progress=progress, progress_overall=progress_overall, + event_abort=handling_app.event_abort, + event_run=handling_app.event_run, ) - progress_table = Table.grid() # Style Progress display. @@ -112,6 +117,10 @@ def _download(ctx: typer.Context, urls: list[str], try_login: bool = True) -> bo for item in urls: media_type: MediaType | bool = False + # Exit loop if abort signal is set. + if handling_app.event_abort.is_set(): + return False + # Extract media name and id from link. if "http" in item: media_type = get_tidal_media_type(item) @@ -143,6 +152,10 @@ def _download(ctx: typer.Context, urls: list[str], try_login: bool = True) -> bo item_ids.append(item_id) for item_id in item_ids: + # Exit loop if abort signal is set. + if handling_app.event_abort.is_set(): + return False + dl.items( media_id=item_id, media_type=media_type, @@ -379,5 +392,21 @@ def gui(ctx: typer.Context): gui_activate(ctx.obj[CTX_TIDAL]) +def handle_sigint_term(signum, frame): + """Set app abort event, so threads can check it and shutdown. + + :param signum: + :param frame: + :return: + """ + handling_app: HandlingApp = HandlingApp() + + handling_app.event_abort.set() + + if __name__ == "__main__": + # Catch CTRL+C + signal.signal(signal.SIGINT, handle_sigint_term) + signal.signal(signal.SIGTERM, handle_sigint_term) + app() |
