mirror of
https://github.com/ThePhaseless/Byparr.git
synced 2026-11-15 12:08:19 +00:00
The retry in read_settled_html covered page.content() racing a navigation. #418 only showed that race on the early read taken right after domcontentloaded, and this branch already removes that read. The late read now matches the Cloudflare path, which reads after the idle wait and has never retried. When the retry fires at the end of the budget, remaining_ms() is 1 ms. The second idle wait then times out at once, and the second read lands in the same 60-150 ms window that failed. So the retry adds a helper and threads timer into content.py for a case it does not cover. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
35 lines
1.1 KiB
Python
35 lines
1.1 KiB
Python
import base64
|
|
|
|
from playwright.async_api import Page
|
|
|
|
from src.models import LinkRequest
|
|
from src.utils import logger
|
|
|
|
|
|
async def build_response_content(
|
|
page: Page,
|
|
request: LinkRequest,
|
|
page_request: object,
|
|
) -> tuple[str, str]:
|
|
"""Build (content_type, response_content) from the settled page."""
|
|
if request.return_only_cookies:
|
|
return "text/html", ""
|
|
|
|
if page_request and page_request.headers.get("content-type", "").startswith(
|
|
"application/pdf"
|
|
):
|
|
return await fetch_pdf_content(page)
|
|
|
|
return "text/html", await page.content()
|
|
|
|
|
|
async def fetch_pdf_content(page: Page) -> tuple[str, str]:
|
|
"""Fetch raw PDF bytes as base64, falling back to viewer HTML on failure."""
|
|
try:
|
|
fetch_response = await page.request.fetch(page.url)
|
|
response_content = base64.b64encode(await fetch_response.body()).decode("ascii")
|
|
except Exception:
|
|
logger.exception("Failed to fetch PDF bytes, falling back to viewer HTML")
|
|
return "text/html", await page.content()
|
|
return "application/pdf", response_content
|