"""OpenLibrary-Matching — Phase 5.""" 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} if author: params["author"] = author async with httpx.AsyncClient(timeout=10) as client: resp = await client.get(f"{OL_BASE}/search.json", params=params) resp.raise_for_status() data = resp.json() results = [] for doc in data.get("docs", []): 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"), confidence=0.6, ) ) return results