import httpx from .base import MatchResult OL_BASE = "https://openlibrary.org" async def search_open_library(title: str, author: str | None = None) -> list[MatchResult]: params: dict = {"title": title, "limit": 5, "fields": "key,title,author_name,first_publish_year,cover_i,subject"} if author: params["author"] = author async with httpx.AsyncClient(timeout=12) as client: try: r = await client.get(f"{OL_BASE}/search.json", params=params) r.raise_for_status() data = r.json() except Exception: return [] results = [] for doc in data.get("docs", []): cover_url = None if doc.get("cover_i"): cover_url = f"https://covers.openlibrary.org/b/id/{doc['cover_i']}-L.jpg" results.append(MatchResult( source="open_library", source_id=doc.get("key", ""), title=doc.get("title", title), author=doc.get("author_name", [None])[0] if doc.get("author_name") else None, publish_year=doc.get("first_publish_year"), cover_url=cover_url, genres=doc.get("subject", [])[:5], confidence=0.55, )) return results async def get_work_details(work_key: str) -> MatchResult | None: """Lädt Beschreibung und Genres nach.""" async with httpx.AsyncClient(timeout=12) as client: try: r = await client.get(f"{OL_BASE}{work_key}.json") r.raise_for_status() data = r.json() except Exception: return None desc = data.get("description") if isinstance(desc, dict): desc = desc.get("value") return MatchResult( source="open_library", source_id=work_key, title=data.get("title", ""), description=desc, confidence=1.0, )