From ed0918e7b0ced74345a7289d7e5390a6b330dc4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Dudzi=C5=84ski?= <56404247+oskvr37@users.noreply.github.com> Date: Tue, 3 Jun 2025 14:50:13 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Save=20album=20covers=20on=20downlo?= =?UTF-8?q?ad=20(#128)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * save cover * create cover directory before saving * prepare cover settings * add cover settings * add filename setting --- tiddl/cli/download/__init__.py | 26 +++++++++++++++++++++++--- tiddl/config.py | 7 +++++++ tiddl/metadata.py | 9 ++++++--- 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/tiddl/cli/download/__init__.py b/tiddl/cli/download/__init__.py index 798d401..222f92e 100644 --- a/tiddl/cli/download/__init__.py +++ b/tiddl/cli/download/__init__.py @@ -95,7 +95,16 @@ def DownloadCommand( SINGLES_FILTER = SINGLES_FILTER or ctx.obj.config.download.singles_filter # TODO: pretty print - logging.debug((QUALITY, TEMPLATE, PATH, THREADS_COUNT, DO_NOT_SKIP, SINGLES_FILTER)) + logging.debug( + ( + QUALITY, + TEMPLATE, + PATH, + THREADS_COUNT, + DO_NOT_SKIP, + SINGLES_FILTER, + ) + ) DOWNLOAD_QUALITY = ARG_TO_QUALITY[QUALITY or ctx.obj.config.download.quality] @@ -257,7 +266,12 @@ def DownloadCommand( def downloadAlbum(album: Album): logging.info(f"Album {album.title!r}") - cover_data = Cover(album.cover).content if album.cover else b"" + cover = ( + Cover(uid=album.cover, size=ctx.obj.config.cover.size) + if album.cover + else None + ) + is_cover_saved = False offset = 0 @@ -271,10 +285,16 @@ def DownloadCommand( album_artist=album.artist.name, ) + if cover and not is_cover_saved and ctx.obj.config.cover.save: + path = Path(PATH) if PATH else ctx.obj.config.download.path + cover_path = path / Path(filename).parent + cover.save(cover_path, ctx.obj.config.cover.filename) + is_cover_saved = True + submitItem( item.item, filename, - cover_data, + cover.content if cover else b"", item.credits, album.artist.name, ) diff --git a/tiddl/config.py b/tiddl/config.py index a60a590..3205e1d 100644 --- a/tiddl/config.py +++ b/tiddl/config.py @@ -37,9 +37,16 @@ class AuthConfig(BaseModel): country_code: str = "" +class CoverConfig(BaseModel): + save: bool = False + size: int = 1280 + filename: str = "cover.jpg" + + class Config(BaseModel): template: TemplateConfig = TemplateConfig() download: DownloadConfig = DownloadConfig() + cover: CoverConfig = CoverConfig() auth: AuthConfig = AuthConfig() omit_cache: bool = False diff --git a/tiddl/metadata.py b/tiddl/metadata.py index a4e233d..da6b79c 100644 --- a/tiddl/metadata.py +++ b/tiddl/metadata.py @@ -1,6 +1,7 @@ import logging import requests +from os import makedirs from pathlib import Path from mutagen.easymp4 import EasyMP4 as MutagenEasyMP4 @@ -170,16 +171,18 @@ class Cover: return req.content - def save(self, directory_path: Path): + def save(self, directory_path: Path, filename="cover.jpg"): if not self.content: logger.error("cover file content is empty") return - - file = directory_path / "cover.jpg" + + file = directory_path / filename if file.exists(): logger.debug(f"cover already exists ({file})") return + + makedirs(directory_path, exist_ok=True) try: with file.open("wb") as f: