"""iTunes Search API client — free, no API key, no auth, no rate-limit registration. Used as a fallback when MusicBrainz + Cover Art Archive has no match/art for a release. """ import requests from typing import Optional, Dict ITUNES_BASE = "https://itunes.apple.com/search" def lookup_album(artist: str, album: str) -> Optional[Dict]: """Best-effort match for an artist+album against the iTunes Store catalog.""" try: r = requests.get(ITUNES_BASE, params={ "term": f"{artist} {album}", "media": "music", "entity": "album", "limit": 1, }, timeout=15) r.raise_for_status() results = (r.json() or {}).get("results") or [] except Exception: return None if not results: return None res = results[0] art = res.get("artworkUrl100") if art: # iTunes serves a small default; the same CDN path accepts arbitrary sizes. art = art.replace("100x100bb", "1200x1200bb") return { "title": res.get("collectionName") or album, "artist": res.get("artistName") or artist, "year": (res.get("releaseDate") or "")[:4] or None, "art_url": art, }