calibre upload

This commit is contained in:
2026-05-10 16:36:47 +02:00
parent 246606161b
commit 8e208e4701
+6
View File
@@ -21,6 +21,7 @@ class CalibreClient:
self._cfg = cfg self._cfg = cfg
self._session = requests.Session() self._session = requests.Session()
self._authenticated = False self._authenticated = False
self._upload_csrf: str | None = None
def _ensure_auth(self) -> None: def _ensure_auth(self) -> None:
if self._authenticated: if self._authenticated:
@@ -59,11 +60,16 @@ class CalibreClient:
try: try:
self._ensure_auth() self._ensure_auth()
if not self._upload_csrf:
page = self._session.get(f"{self._cfg.url}/upload", timeout=30)
self._upload_csrf = _extract_csrf(page.text)
mime = MIME_TYPES.get(book_path.suffix.lower(), "application/octet-stream") mime = MIME_TYPES.get(book_path.suffix.lower(), "application/octet-stream")
with book_path.open("rb") as fh: with book_path.open("rb") as fh:
resp = self._session.post( resp = self._session.post(
f"{self._cfg.url}/upload", f"{self._cfg.url}/upload",
files={"btn-upload": (book_path.name, fh, mime)}, files={"btn-upload": (book_path.name, fh, mime)},
data={"csrf_token": self._upload_csrf} if self._upload_csrf else {},
timeout=120, timeout=120,
) )
resp.raise_for_status() resp.raise_for_status()