Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix usdb covers fallback #344

Merged
merged 4 commits into from
Feb 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- Add more fine-grained options for video container/codec selection.

## Fixes
- Cover postprocessing parameters are ignored if USDB cover is downloaded as fallback.

## Features

Expand Down
8 changes: 5 additions & 3 deletions src/usdb_syncer/resource_dl.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ def download_image(url: str, logger: Log) -> bytes | None:
return None
except requests.RequestException:
logger.error(
f"Failed to retrieve {url}. The server may be down or your internet "
"connection is currently unavailable."
f"Failed to retrieve {url}. The URL might be invalid, the server may be "
"down or your internet connection is currently unavailable."
)
return None
if reply.status_code in range(100, 399):
Expand All @@ -203,6 +203,7 @@ def download_and_process_image(
details: SongDetails,
kind: ImageKind,
max_width: CoverMaxSize | None,
process: bool = True,
) -> Path | None:
logger = song_logger(details.song_id)
if not (img_bytes := download_image(url, logger)):
Expand All @@ -217,7 +218,8 @@ def download_and_process_image(
with path.open("wb") as file:
file.write(img_bytes)

_process_image(meta_tags, max_width, path)
if process:
_process_image(meta_tags, max_width, path)
return path


Expand Down
5 changes: 3 additions & 2 deletions src/usdb_syncer/song_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,13 +455,13 @@ def _maybe_download_cover(ctx: _Context) -> None:
return
if ctx.details.cover_url:
ctx.logger.warning("Falling back to small USDB cover.")
if _download_cover_url(ctx, ctx.details.cover_url):
if _download_cover_url(ctx, ctx.details.cover_url, process=False):
return
keep = " Keeping last resource." if ctx.out.cover.resource else ""
ctx.logger.error(f"Failed to download cover!{keep}")


def _download_cover_url(ctx: _Context, url: str) -> bool:
def _download_cover_url(ctx: _Context, url: str, process: bool = True) -> bool:
"""True if download was successful (or is unnecessary)."""
assert ctx.options.cover
if ctx.out.cover.resource == url:
Expand All @@ -474,6 +474,7 @@ def _download_cover_url(ctx: _Context, url: str) -> bool:
details=ctx.details,
kind=resource_dl.ImageKind.COVER,
max_width=ctx.options.cover.max_size,
process=process,
):
ctx.out.cover.resource = url
ctx.out.cover.new_fname = path.name
Expand Down
1 change: 1 addition & 0 deletions tests/integration/test_song_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def _download_and_process_image(
details: Any,
kind: ImageKind,
max_width: Any,
process: bool = True,
) -> Path | None:
path = target_stem.with_name(f"{target_stem.name} [{kind.value}].jpg")
path.touch()
Expand Down