From 9f7d3aec1efb66d04963c76f70d6ba6e476b6574 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Dudzi=C5=84ski?= <56404247+oskvr37@users.noreply.github.com> Date: Fri, 5 Dec 2025 14:11:47 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Album/artist=20download=20errors?= =?UTF-8?q?=20related=20to=20templates=20are=20now=20handled=20(#257)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor variable name in _clean_segment function for clarity * Add error handling documentation for format_template function * Add error handling for album template formatting in download_callback --- tiddl/cli/commands/download/__init__.py | 27 +++++++++++++++++-------- tiddl/core/utils/format.py | 16 +++++++++------ 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/tiddl/cli/commands/download/__init__.py b/tiddl/cli/commands/download/__init__.py index 4613770..71f93c6 100644 --- a/tiddl/cli/commands/download/__init__.py +++ b/tiddl/cli/commands/download/__init__.py @@ -299,15 +299,26 @@ def download_callback( ) for album_item in album_items.items: + try: + template = TEMPLATE or CONFIG.templates.album + file_path = format_template( + template=template, + item=album_item.item, + album=album, + quality=get_item_quality(album_item.item), + ) + + except AttributeError as exc: + log.error(f"{exc=}") + ctx.obj.console.print( + f"[red]Wrong Album Template:[/] {exc} ({template=}, {album.id=}, {album_item.item.id=})" + ) + continue + futures.append( handle_item( item=album_item.item, - file_path=format_template( - template=TEMPLATE or CONFIG.templates.album, - item=album_item.item, - album=album, - quality=get_item_quality(album_item.item), - ), + file_path=file_path, track_metadata=Metadata( cover=cover, date=str(album.releaseDate), @@ -563,9 +574,9 @@ def download_callback( try: await handle_resource(r) except ApiError as e: - ctx.obj.console.print(f"[red]API Error:[/] {e} at {r}") + ctx.obj.console.print(f"[red]API Error:[/] {e} ({r})") except Exception as e: - ctx.obj.console.print(f"[red]Error:[/] {e} at {r}") + ctx.obj.console.print(f"[red]Error:[/] {e} ({r})") await asyncio.gather(*(wrapper(r) for r in ctx.obj.resources)) diff --git a/tiddl/core/utils/format.py b/tiddl/core/utils/format.py index fb14224..420549a 100644 --- a/tiddl/core/utils/format.py +++ b/tiddl/core/utils/format.py @@ -18,13 +18,13 @@ def _clean_segment(text: str) -> str: - Ensures the segment is never empty (uses "_" as fallback). """ - s = sanitize_string(text) - s = re.sub(r"\.{2,}", ".", s) - s = s.rstrip(" .") - s = re.sub(r"\s{2,}", " ", s) - s = s.strip() + text = sanitize_string(text) + text = re.sub(r"\.{2,}", ".", text) + text = text.rstrip(" .") + text = re.sub(r"\s{2,}", " ", text) + text = text.strip() - return s or "_" + return text or "_" class Explicit: @@ -202,6 +202,10 @@ def format_template( with_asterisk_ext: bool = True, **extra, ) -> str: + """ + Raises `AttributeError` on invalid template. + """ + custom_fields = {"now": datetime.now()} data = (