"""Google Books-Matching — Phase 5.""" import httpx from .base import MatchResult GB_BASE = "https://www.googleapis.com/books/v1" async def search_google_books(title: str, author: str | None = None) -> list[MatchResult]: q = f'intitle:"{title}"' if author: q += f' inauthor:"{author}"' async with httpx.AsyncClient(timeout=10) as client: resp = await client.get(f"{GB_BASE}/volumes", params={"q": q, "maxResults": 5, "langRestrict": "de"}) resp.raise_for_status() data = resp.json() results = [] for item in data.get("items", []): vol = item.get("volumeInfo", {}) authors = vol.get("authors", []) results.append( MatchResult( source="google_books", source_id=item.get("id", ""), title=vol.get("title", title), author=authors[0] if authors else None, description=vol.get("description"), publisher=vol.get("publisher"), publish_year=int(vol.get("publishedDate", "0")[:4]) if vol.get("publishedDate") else None, language=vol.get("language"), confidence=0.5, ) ) return results