fix(session): make RnetResponse a context manager for cover downloads

This commit is contained in:
imSp4rky
2026-11-27 18:05:00 -07:00
parent 438419ab1b
commit e098ad38b9
4 changed files with 92 additions and 5 deletions
+5 -2
View File
@@ -12,6 +12,7 @@ from typing import Any
from unshackle.core import binaries
from unshackle.core.config import config
from unshackle.core.session import AnySession
from unshackle.core.titles.music import song_metadata
from unshackle.core.utils.collections import as_list
from unshackle.core.utils.subprocess import log_tool_run
@@ -76,7 +77,9 @@ class MusicMetadataResult:
}
def write_music_metadata(path: Path, song: Any, *, session: Any = None, source_md5: str = "") -> MusicMetadataResult:
def write_music_metadata(
path: Path, song: Any, *, session: AnySession | None = None, source_md5: str = ""
) -> MusicMetadataResult:
"""Write normalized music metadata for FLAC, Ogg, Opus, MP3, WAV, and M4A/MP4 audio files.
Opus files get no ``REPLAYGAIN_*`` tags, because RFC 7845 section 5.2.1 says an Opus file should not carry them.
@@ -324,7 +327,7 @@ def write_mp4_tags(path: Path, tags: TagValues, cover_data: bytes | None, mime_t
audio.save()
def download_cover(session: Any, artwork_url: str) -> tuple[bytes | None, str]:
def download_cover(session: AnySession | None, artwork_url: str) -> tuple[bytes | None, str]:
if not session or not artwork_url:
return None, ""
try:
+11 -1
View File
@@ -118,7 +118,11 @@ class RnetResponseHeaders(MutableMapping):
class RnetResponse:
"""Wraps rnet.BlockingResponse with a requests-compatible API."""
"""Wraps rnet.BlockingResponse with a requests-compatible API.
A ``with`` block gives this response and closes it at the end. Read the body inside the block:
a body left unread cannot be read after it.
"""
def __init__(self, resp: Any) -> None:
self._resp = resp
@@ -233,6 +237,12 @@ class RnetResponse:
except Exception:
pass
def __enter__(self) -> RnetResponse:
return self
def __exit__(self, *exc: Any) -> None:
self.close()
def utf8_text(res: Response | RnetResponse) -> str:
"""Return the response text, decoded as UTF-8 unless the server sent a charset.
+2 -2
View File
@@ -5,7 +5,6 @@ import subprocess
import tempfile
import time
from pathlib import Path
from typing import Any
from xml.sax.saxutils import escape
from unshackle.core import binaries
@@ -17,6 +16,7 @@ from unshackle.core.providers import (
get_available_providers,
resolve_by_ids,
)
from unshackle.core.session import AnySession
from unshackle.core.titles.episode import Episode
from unshackle.core.titles.movie import Movie
from unshackle.core.titles.music import Song
@@ -92,7 +92,7 @@ def tag_file(
tvdb_id: int | None = None,
anilist_id: int | str | None = None,
anime: bool = False,
session: Any = None,
session: AnySession | None = None,
) -> None:
log.debug("Tagging file %s with title %r", path, title)