Files
watchlist/app/external.py
T
ki 0bca2eaaa2
CI / test (push) Has been cancelled
CI / docker (push) Has been cancelled
chore: beta release v0.5.0-beta
- Bump version to 0.5.0-beta
- Add Apache-2.0 LICENSE (official text) + NOTICE
- Add Docker support: multi-stage Dockerfile + docker-compose
  * non-root user, tini PID 1, gunicorn + uvicorn workers
  * healthcheck, OCI labels, persistent /data volume
- Add GitHub Actions CI (lint/import-check + Docker smoke-test)
- Backend polish:
  * modern lifespan handler (replaces deprecated on_event)
  * explicit sort validation (400 instead of 500)
  * title min_length=1 schema validation
  * configurable data dir + http timeout via env
  * avg_rating helper extracted (DRY)
- Frontend polish:
  * beta badge in title, footer with live version
  * loading spinner on initial fetch
  * updated README, badges, config table, roadmap
2026-07-21 23:34:01 +02:00

74 lines
2.4 KiB
Python

"""Externe Cover-/Metadaten-Quellen (Open Library)."""
# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations
import logging
import os
from typing import Optional
import httpx
log = logging.getLogger(__name__)
OL_SEARCH = "https://openlibrary.org/search.json"
OL_COVER = "https://covers.openlibrary.org/b/isbn/{isbn}-L.jpg"
DEFAULT_TIMEOUT = float(os.environ.get("WATCHSTACK_HTTP_TIMEOUT", "8"))
def lookup_book_by_isbn(isbn: str, *, timeout: float = DEFAULT_TIMEOUT) -> Optional[dict]:
"""Sucht Buchdaten anhand ISBN via Open Library."""
if not isbn:
return None
isbn = isbn.strip().replace("-", "").replace(" ", "")
try:
with httpx.Client(timeout=timeout) as client:
r = client.get(OL_SEARCH, params={"isbn": isbn, "limit": 1})
r.raise_for_status()
data = r.json()
except Exception as exc: # pragma: no cover
log.warning("Open-Library-Suche fehlgeschlagen: %s", exc)
return None
docs = data.get("docs") or []
if not docs:
return None
d = docs[0]
return {
"title": d.get("title"),
"original_title": d.get("title"),
"author": ", ".join(d.get("author_name") or []) or None,
"release_year": d.get("first_publish_year"),
"cover_url": OL_COVER.format(isbn=isbn) if isbn else None,
"total_pages": d.get("number_of_pages_median"),
"description": None, # OL liefert hier keine handliche Kurzbeschreibung
}
def lookup_book_by_title(title: str, *, timeout: float = DEFAULT_TIMEOUT) -> Optional[dict]:
"""Sucht Buchdaten anhand Titel (Fallback)."""
if not title:
return None
try:
with httpx.Client(timeout=timeout) as client:
r = client.get(OL_SEARCH, params={"title": title, "limit": 1})
r.raise_for_status()
data = r.json()
except Exception as exc: # pragma: no cover
log.warning("Open-Library-Suche fehlgeschlagen: %s", exc)
return None
docs = data.get("docs") or []
if not docs:
return None
d = docs[0]
isbn = None
isbns = d.get("isbn") or []
if isbns:
isbn = isbns[0]
return {
"title": d.get("title"),
"author": ", ".join(d.get("author_name") or []) or None,
"release_year": d.get("first_publish_year"),
"cover_url": OL_COVER.format(isbn=isbn) if isbn else None,
"total_pages": d.get("number_of_pages_median"),
}