From 52346baf52aa446e8dd5ea041ffa8bf0c2f363d5 Mon Sep 17 00:00:00 2001 From: Gabby Morgan Date: Sat, 17 Oct 2026 08:18:13 +0000 Subject: [PATCH] gutendex: send a User-Agent (Cloudflare 403s the default urllib agent) Gutendex.com sits behind Cloudflare, which rejects urllib's default `Python-urllib/` User-Agent with HTTP 403. Both skills therefore failed on their first real call ("could not reach the Gutendex catalog ... HTTP Error 403"); the hermetic loopback tests cannot catch this by design. Send an identifying `USER_AGENT` on every request (the /books catalog lookup and the plain-text download) and assert it in each skill's test so the regression is caught locally. Verified with a real run: find_books returned 41 live results for "jane austen"; read_book downloaded and saved Pride and Prejudice (1342). --- skills/gutendex/find_books/skill.py | 11 +++++++++- skills/gutendex/find_books/skill.test.py | 19 ++++++++++++++++- skills/gutendex/read_book/skill.py | 15 +++++++++++-- skills/gutendex/read_book/skill.test.py | 27 +++++++++++++++++++++++- 4 files changed, 67 insertions(+), 5 deletions(-) diff --git a/skills/gutendex/find_books/skill.py b/skills/gutendex/find_books/skill.py index 5ba8a9a..c4dd029 100644 --- a/skills/gutendex/find_books/skill.py +++ b/skills/gutendex/find_books/skill.py @@ -6,6 +6,9 @@ request's search terms are taken from the user's query and the catalog is searched by author/title (`search`) or by genre/subject (`topic`) — which one is a SemIf decision over the real query. A real HTTP failure is reported honestly; book data is never invented. + +Gutendex sits behind Cloudflare, which rejects the default `Python-urllib` +User-Agent with HTTP 403, so every request identifies itself with `USER_AGENT`. """ from __future__ import annotations @@ -29,6 +32,10 @@ CONTRACT = { "gutendex_base_url": "Base URL of the Gutendex Project Gutenberg API, e.g. https://gutendex.com (no trailing slash needed).", } +# Gutendex's Cloudflare front blocks the default `Python-urllib/` agent +# with HTTP 403; a plain, identifying agent is accepted. +USER_AGENT = "semif-agent/1.0" + TIMEOUT_SECONDS = 20 MAX_RESULTS = 5 RESULT_CHARS = 300 @@ -88,7 +95,9 @@ def _get_json(base_url, path, params=None): url = base_url + path if params: url += "?" + urllib.parse.urlencode(params) - http_request = urllib.request.Request(url, headers={"Accept": "application/json"}) + http_request = urllib.request.Request( + url, headers={"Accept": "application/json", "User-Agent": USER_AGENT} + ) with urllib.request.urlopen(http_request, timeout=TIMEOUT_SECONDS) as response: return json.loads(response.read().decode("utf-8")) diff --git a/skills/gutendex/find_books/skill.test.py b/skills/gutendex/find_books/skill.test.py index cf616f5..95bfbd8 100644 --- a/skills/gutendex/find_books/skill.test.py +++ b/skills/gutendex/find_books/skill.test.py @@ -65,6 +65,7 @@ class Handler(BaseHTTPRequestHandler): def do_GET(self): self.server.seen.append(self.path) + self.server.user_agents.append(self.headers.get("User-Agent")) if self.server.status != 200: self._send({"detail": "boom"}, self.server.status) return @@ -86,9 +87,10 @@ class Handler(BaseHTTPRequestHandler): def start_server(status=200, results=None, count=None): - handler = type("Handler", (Handler,), {"status": status, "seen": []}) + handler = type("Handler", (Handler,), {"status": status, "seen": [], "user_agents": []}) server = ThreadingHTTPServer(("127.0.0.1", 0), handler) server.seen = handler.seen + server.user_agents = handler.user_agents server.status = status server.results = FIXTURE_BOOKS if results is None else results server.count = len(server.results) if count is None else count @@ -146,6 +148,20 @@ def test_empty_result_is_reported_plainly(): server.server_close() +def test_sends_an_identifying_user_agent(): + # Gutendex's Cloudflare front 403s the default `Python-urllib/` agent, + # so the body must identify itself (the real-service bug this guards). + server, url = start_server() + try: + run(url, "find books by charles dickens", selected="search") + assert server.user_agents == [skill.USER_AGENT], server.user_agents + assert not str(server.user_agents[0]).startswith("Python-urllib"), server.user_agents + print("user-agent:", server.user_agents[0]) + finally: + server.shutdown() + server.server_close() + + def test_http_failure_is_reported_honestly(): server, url = start_server(status=503) try: @@ -175,6 +191,7 @@ def main(): test_searches_by_author_or_title() test_searches_by_genre_via_topic() test_empty_result_is_reported_plainly() + test_sends_an_identifying_user_agent() test_http_failure_is_reported_honestly() test_missing_query_asks_for_one() print("ok") diff --git a/skills/gutendex/read_book/skill.py b/skills/gutendex/read_book/skill.py index d9b9573..0f349af 100644 --- a/skills/gutendex/read_book/skill.py +++ b/skills/gutendex/read_book/skill.py @@ -6,6 +6,9 @@ text is downloaded over HTTP. A book is far too long to read into a chat result, so the full text is saved to a `.txt` file under `output_dir` and a short summary plus the path is returned. Real failures are reported honestly; text is never invented. + +Gutendex sits behind Cloudflare, which rejects the default `Python-urllib` +User-Agent with HTTP 403, so every request identifies itself with `USER_AGENT`. """ from __future__ import annotations @@ -32,6 +35,10 @@ CONTRACT = { "output_dir": "Directory where the downloaded book's full text is saved as a .txt file, e.g. /home/me/books.", } +# Gutendex's Cloudflare front blocks the default `Python-urllib/` agent +# with HTTP 403; a plain, identifying agent is accepted. +USER_AGENT = "semif-agent/1.0" + TIMEOUT_SECONDS = 60 MAX_CANDIDATES = 8 MAX_QUERY_CHARS = 80 @@ -101,13 +108,17 @@ def _get_json(base_url, path, params=None): url = base_url + path if params: url += "?" + urllib.parse.urlencode(params) - http_request = urllib.request.Request(url, headers={"Accept": "application/json"}) + http_request = urllib.request.Request( + url, headers={"Accept": "application/json", "User-Agent": USER_AGENT} + ) with urllib.request.urlopen(http_request, timeout=TIMEOUT_SECONDS) as response: return json.loads(response.read().decode("utf-8")) def _get_text(url): - http_request = urllib.request.Request(url, headers={"Accept": "text/plain"}) + http_request = urllib.request.Request( + url, headers={"Accept": "text/plain", "User-Agent": USER_AGENT} + ) with urllib.request.urlopen(http_request, timeout=TIMEOUT_SECONDS) as response: return response.read().decode("utf-8", "replace") diff --git a/skills/gutendex/read_book/skill.test.py b/skills/gutendex/read_book/skill.test.py index 3174f68..a0a8ef8 100644 --- a/skills/gutendex/read_book/skill.test.py +++ b/skills/gutendex/read_book/skill.test.py @@ -53,6 +53,7 @@ class Handler(BaseHTTPRequestHandler): def do_GET(self): self.server.seen.append(self.path) + self.server.user_agents.append(self.headers.get("User-Agent")) if self.path.startswith("/books/"): book_id = self.path.rsplit("/", 1)[-1].split("?")[0] book = self.server.by_id.get(book_id) @@ -94,10 +95,11 @@ def book(book_id, title, author, url, mime="text/plain; charset=utf-8"): def start_server(): - handler = type("Handler", (Handler,), {"seen": []}) + handler = type("Handler", (Handler,), {"seen": [], "user_agents": []}) server = ThreadingHTTPServer(("127.0.0.1", 0), handler) url = f"http://127.0.0.1:{server.server_address[1]}" server.seen = handler.seen + server.user_agents = handler.user_agents server.text_body = TEXT_BODY server.text_status = 200 server.search_results = [] @@ -206,6 +208,28 @@ def test_missing_plain_text_format_is_reported(): shutil.rmtree(output_dir, ignore_errors=True) +def test_sends_an_identifying_user_agent(): + # Gutendex's Cloudflare front 403s the default `Python-urllib/` agent, + # so both the catalog lookup and the text download must identify themselves. + server, url = start_server() + output_dir = tempfile.mkdtemp() + try: + pride = book(1342, "Pride and Prejudice", "Jane Austen", url) + server.search_results = [pride] + server.by_id["1342"] = pride + run(url, output_dir, "read pride and prejudice") + assert server.user_agents, "no request was made" + assert all(ua == skill.USER_AGENT for ua in server.user_agents), server.user_agents + assert not any( + str(ua).startswith("Python-urllib") for ua in server.user_agents + ), server.user_agents + print("user-agents:", server.user_agents) + finally: + server.shutdown() + server.server_close() + shutil.rmtree(output_dir, ignore_errors=True) + + def test_download_failure_is_reported_honestly(): server, url = start_server() output_dir = tempfile.mkdtemp() @@ -230,6 +254,7 @@ def main(): test_picks_among_candidates() test_no_match_asks_for_a_book() test_missing_plain_text_format_is_reported() + test_sends_an_identifying_user_agent() test_download_failure_is_reported_honestly() print("ok")