From dc7271e22ca91f1400a73978b2ed80c5844019bb Mon Sep 17 00:00:00 2001 From: FuzzyGrim Date: Mon, 25 May 2026 22:44:33 +0200 Subject: [PATCH] add user agent to openlibrary requests --- src/app/providers/openlibrary.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/app/providers/openlibrary.py b/src/app/providers/openlibrary.py index a0e061fa..a0533e6e 100644 --- a/src/app/providers/openlibrary.py +++ b/src/app/providers/openlibrary.py @@ -17,6 +17,7 @@ logger = logging.getLogger(__name__) base_url = "https://openlibrary.org/api" search_url = "https://openlibrary.org/search.json" +headers = {"User-Agent": "Yamtrack/1.0 (github@fuzzygrim.com)"} def handle_error(error): @@ -48,6 +49,7 @@ def search(query, page): "GET", search_url, params=params, + headers=headers, ) except requests.RequestException as e: handle_error(e) @@ -135,6 +137,7 @@ async def async_book(media_id): Sources.OPENLIBRARY.value, "GET", book_url, + headers=headers, ) except requests.RequestException as e: handle_error(e) @@ -150,6 +153,7 @@ async def async_book(media_id): Sources.OPENLIBRARY.value, "GET", work_url, + headers=headers, ) except requests.RequestException as e: handle_error(e) @@ -264,7 +268,7 @@ async def get_authors(response): authors = [] author_entries = response.get("authors", []) - async with aiohttp.ClientSession() as session: + async with aiohttp.ClientSession(headers=headers) as session: tasks = [] for author in author_entries: if isinstance(author, dict) and "author" in author: @@ -324,7 +328,10 @@ async def get_editions(response_book, response_work): # limit to 500 editions, pagination is not supported url = f"https://openlibrary.org/works/{work_id}/editions.json?limit=500" - async with aiohttp.ClientSession() as session, session.get(url) as response: + async with ( + aiohttp.ClientSession(headers=headers) as session, + session.get(url) as response, + ): if response.status == requests.codes.ok: data = await response.json() return [ @@ -352,7 +359,10 @@ async def get_ratings(response_work): url = f"https://openlibrary.org/works/{work_id}/ratings.json" - async with aiohttp.ClientSession() as session, session.get(url) as response: + async with ( + aiohttp.ClientSession(headers=headers) as session, + session.get(url) as response, + ): if response.status == requests.codes.ok: data = await response.json() summary = data.get("summary", {})