gutendex: send a User-Agent (Cloudflare 403s the default urllib agent)

Gutendex.com sits behind Cloudflare, which rejects urllib's default
`Python-urllib/<ver>` 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).
This commit is contained in:
gabby committed 2026-10-17 08:18:13 +00:00
1 parent b775069613
commit 52346baf52
4 files changed
+67 -5

No files matched your search

+13 -2
View File
@@ -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/<ver>` 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")