sync errors
This commit is contained in:
+32
-14
@@ -1,6 +1,7 @@
|
||||
import hashlib
|
||||
import logging
|
||||
import re
|
||||
import time
|
||||
from pathlib import Path
|
||||
from urllib.parse import quote
|
||||
|
||||
@@ -101,20 +102,37 @@ class CalibreClient:
|
||||
return "skipped_duplicate"
|
||||
|
||||
mime = MIME_TYPES.get(book_path.suffix.lower(), "application/octet-stream")
|
||||
with book_path.open("rb") as fh:
|
||||
resp = self._session.post(
|
||||
f"{self._cfg.url}/upload",
|
||||
files={"btn-upload": (book_path.name, fh, mime)},
|
||||
data={"csrf_token": self._upload_csrf} if self._upload_csrf else {},
|
||||
timeout=120,
|
||||
)
|
||||
if not resp.ok:
|
||||
log.error("Upload HTTP %s — body: %s", resp.status_code, resp.text[:300])
|
||||
resp.raise_for_status()
|
||||
log.info("Uploaded: %s", book_path.name)
|
||||
db.record_book(book_path.name, file_hash, zip_source, "uploaded")
|
||||
return "uploaded"
|
||||
except requests.HTTPError:
|
||||
last_err: Exception | None = None
|
||||
for attempt in range(1, 4):
|
||||
try:
|
||||
with book_path.open("rb") as fh:
|
||||
resp = self._session.post(
|
||||
f"{self._cfg.url}/upload",
|
||||
files={"btn-upload": (book_path.name, fh, mime)},
|
||||
data={"csrf_token": self._upload_csrf} if self._upload_csrf else {},
|
||||
timeout=120,
|
||||
)
|
||||
if not resp.ok:
|
||||
log.error("Upload HTTP %s (attempt %d/3) — body: %s", resp.status_code, attempt, resp.text[:300])
|
||||
resp.raise_for_status()
|
||||
log.info("Uploaded: %s", book_path.name)
|
||||
db.record_book(book_path.name, file_hash, zip_source, "uploaded")
|
||||
return "uploaded"
|
||||
except requests.HTTPError as e:
|
||||
last_err = e
|
||||
if resp.status_code in (502, 503, 504) and attempt < 3:
|
||||
delay = 1 if attempt == 1 else 3
|
||||
log.warning("HTTP %s on attempt %d/3 — retrying in %ds ...", resp.status_code, attempt, delay)
|
||||
time.sleep(delay)
|
||||
continue
|
||||
if resp.status_code == 400 and attempt == 1:
|
||||
log.warning("HTTP 400 — CSRF token likely expired, re-authenticating ...")
|
||||
self._authenticated = False
|
||||
self._upload_csrf = None
|
||||
self._ensure_auth()
|
||||
continue
|
||||
break
|
||||
|
||||
db.record_book(book_path.name, file_hash, zip_source, "error")
|
||||
return "error"
|
||||
except Exception as e:
|
||||
|
||||
Reference in New Issue
Block a user