From 6a737af17fa2f6029e11bf20448a686dd948f016 Mon Sep 17 00:00:00 2001 From: oskvr37 Date: Fri, 24 Jan 2025 22:55:55 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20handle=20api=20and=20auth=20errors?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tiddl/cli/download/__init__.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/tiddl/cli/download/__init__.py b/tiddl/cli/download/__init__.py index 886fbb9..3b5c82b 100644 --- a/tiddl/cli/download/__init__.py +++ b/tiddl/cli/download/__init__.py @@ -9,8 +9,9 @@ from ..ctx import Context, passContext from tiddl.download import downloadTrackStream from tiddl.models import TrackArg, ARG_TO_QUALITY, Track, PlaylistTrack, Album -from tiddl.utils import formatTrack, trackExists +from tiddl.utils import formatTrack, trackExists, TidalResource from tiddl.metadata import addMetadata, Cover +from tiddl.exceptions import ApiError, AuthError @click.command("download") @@ -34,7 +35,7 @@ def DownloadCommand( api = ctx.obj.getApi() - def downloadTrack(track: Track, file_name: str, cover_data=b"") -> None: + def downloadTrack(track: Track, file_name: str, cover_data=b""): if not track.allowStreaming: click.echo( f"{click.style('✖', 'yellow')} Track {click.style(file_name, 'yellow')} does not allow streaming" @@ -94,7 +95,7 @@ def DownloadCommand( downloadTrack(track=track, file_name=file_name, cover_data=cover_data) - for resource in ctx.obj.resources: + def handleResource(resource: TidalResource): match resource.type: case "track": track = api.getTrack(resource.id) @@ -140,6 +141,16 @@ def DownloadCommand( downloadTrack(track=item.item, file_name=file_name) + for resource in ctx.obj.resources: + try: + handleResource(resource) + + except ApiError as e: + click.echo(click.style(f"✖ {e}", "red")) + + except AuthError as e: + click.echo(click.style(f"✖ {e}", "red")) + UrlGroup.add_command(DownloadCommand) SearchGroup.add_command(DownloadCommand)