challenge¶
Support for the bible.usccb.org JavaScript “Checking connection” challenge.
When the site cannot serve a page from its cache, it may respond with a 403 page that runs a JavaScript proof-of-work and stores the result in a cookie. Solving it requires a real browser, which is driven through the optional playwright dependency:
pip install 'catholic-mass-readings[browser]'
The browser can be launched locally (headless by default) or, to avoid installing a browser in the current environment (e.g. a Docker image), an already running browser can be used instead:
CATHOLIC_MASS_READINGS_BROWSER_CDP_URL: connect to a Chromium based browser over the Chrome DevTools Protocol, e.g. Chrome on the host started with--remote-debugging-port=9222.CATHOLIC_MASS_READINGS_BROWSER_WS_ENDPOINT: connect to a Playwright server, e.g. one started withnpx playwright run-server --port 3000or themcr.microsoft.com/playwrightimage.CATHOLIC_MASS_READINGS_BROWSER_CHANNEL: launch an installed browser instead of the bundled one (chrome,msedge, …).CATHOLIC_MASS_READINGS_BROWSER_HEADLESS: set tofalseto show the launched browser.CATHOLIC_MASS_READINGS_BROWSER_TIMEOUT: seconds to wait for the challenge to complete (default: 30; invalid values are ignored with a warning). Raise it on slow machines.CATHOLIC_MASS_READINGS_BROWSER_COOKIE_CACHE: file in which to save the challenge cookie, so that separate runs reuse it until it expires (about 30 minutes).
The CLI’s --browser-* options take precedence over these variables. ChallengeError is raised when the
challenge is encountered and cannot be passed (e.g. without the browser extra, or when it times out).
See the “Browser support and Docker” page of the documentation for Docker examples.
- class catholic_mass_readings.challenge.BrowserConfig(cdp_url: str | None = None, ws_endpoint: str | None = None, channel: str | None = None, headless: bool = True, timeout: float = 30.0, cookie_cache: str | None = None)¶
Configures how the browser used to solve the JavaScript challenge is obtained.
- cdp_url: str | None = None¶
Connect to a running Chromium based browser over CDP (e.g.
http://127.0.0.1:9222).Use an IP address: Chrome rejects DevTools requests whose
Hostis a name other thanlocalhost(e.g.host.docker.internal), andlocalhostmay first resolve to IPv6, adding a long delay.
- channel: str | None = None¶
Browser channel to launch when not connecting to a running browser (e.g.
chrome).
- cookie_cache: str | None = None¶
File in which to save the challenge cookie so that it is reused across runs until it expires.
~is expanded, and the file is created readable only by the current user.
- classmethod from_env(environ: Mapping[str, str] | None = None) BrowserConfig¶
Creates the config from the
CATHOLIC_MASS_READINGS_BROWSER_*environment variables.>>> BrowserConfig.from_env({"CATHOLIC_MASS_READINGS_BROWSER_CDP_URL": "http://127.0.0.1:9222"}) ... BrowserConfig(cdp_url='http://127.0.0.1:9222', ws_endpoint=None, channel=None, headless=True, ...) >>> BrowserConfig.from_env({"CATHOLIC_MASS_READINGS_BROWSER_HEADLESS": "false"}).headless False
- headless: bool = True¶
Whether a launched browser is headless.
- timeout: float = 30.0¶
Seconds to wait for the browser to connect and the challenge to complete.
A page that makes no progress for half of this (at least 10 seconds) is reloaded to restart the challenge.
- ws_endpoint: str | None = None¶
Connect to a Playwright server (e.g.
ws://playwright:3000/).
- exception catholic_mass_readings.challenge.ChallengeError¶
Raised when the JavaScript challenge is encountered and cannot be solved.
- class catholic_mass_readings.challenge.Clearance(cookies: list[dict[str, Any]], user_agent: str)¶
The browser state that allows requests to pass the challenge.
- cookies: list[dict[str, Any]]¶
Cookies (in Playwright’s format) set by the browser for the site.
- property expires: float | None¶
When the proof cookie expires (seconds since the epoch), if known.
>>> Clearance([{"name": PROOF_COOKIE, "value": "p", "expires": 1800.0}], "UA").expires 1800.0 >>> Clearance([{"name": PROOF_COOKIE, "value": "p", "expires": -1}], "UA").expires is None True
- is_expired(margin: float = 60.0) bool¶
Whether the proof cookie expires within margin seconds (unknown expiry is treated as valid).
- user_agent: str¶
User-Agent of the browser that solved the challenge.
- catholic_mass_readings.challenge.is_challenge(status_code: int, headers: Mapping[str, str | None], text: str | None = None) bool¶
Determines whether a response is the JavaScript challenge page.
When the body is available (text), the challenge is recognised by its content. Otherwise (e.g. for HEAD requests) the Varnish
Retry-After403 that serves the challenge is treated as the challenge.>>> is_challenge(403, {}, "<script>var PROOF_COOKIE_NAME = 'X_Obolus_Proof';</script>") True >>> is_challenge(403, {"server": "Varnish", "retry-after": "0"}, "Too many requests") False >>> is_challenge(403, {"server": "Varnish", "retry-after": "0"}) True >>> is_challenge(404, {"server": "Varnish", "retry-after": "0"}) False
- async catholic_mass_readings.challenge.load_clearance(path: str) Clearance | None¶
Loads the clearance saved by save_clearance, unless the file is missing, invalid or expired.
- async catholic_mass_readings.challenge.save_clearance(path: str, clearance: Clearance) None¶
Saves the clearance to path (readable only by the current user), logging rather than raising on failure.
- async catholic_mass_readings.challenge.solve(url: str, config: BrowserConfig) Clearance¶
Loads url in a browser, waits for the proof cookie and returns the resulting cookies.
The proof is computed either by the challenge page itself or, when the browser is let through without it, by the
/obolus-challengescript that runs in the background of the real page.- Raises:
ChallengeError – When
playwrightis not installed, the browser cannot be launched or connected to, or the challenge is not passed within BrowserConfig.timeout.