Save album covers on download (#128)

* save cover

* create cover directory before saving

* prepare cover settings

* add cover settings

* add filename setting
This commit is contained in:
Oskar Dudziński
2025-06-03 14:50:13 +02:00
committed by GitHub
parent a147c94110
commit ed0918e7b0
3 changed files with 36 additions and 6 deletions
+23 -3
View File
@@ -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,
)
+7
View File
@@ -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
+6 -3
View File
@@ -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: