- renderer: plain-fetch escalation when the browser render is blocked (4xx) or fails — some sites 403 automated browsers but serve plain HTTP clients - orchestrator: blocked (>=400), empty, and thin results now appear in failed_urls and the degraded trace list instead of passing silently; blocked/thin results are never cached - extractor: thin threshold raised to 500 chars (env: MIN_TEXT_CHARS) so block/challenge interstitials are flagged instead of passing as content Outcome on TechPowerUp (fingerprints Chromium AND Firefox, JS-challenges plain clients): still blocked — accepted per PRD §9 — but results are now flagged thin/degraded so the calling LLM knows to rely on other sources. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
86 lines
2.6 KiB
Python
86 lines
2.6 KiB
Python
"""Mechanical extraction: Trafilatura primary, readability-lxml fallback.
|
|
|
|
No inference anywhere — deterministic library-based extraction only. A result
|
|
is "thin" when it's shorter than MIN_TEXT_CHARS; thin/empty primary output
|
|
triggers the fallback chain, and the longest result wins.
|
|
"""
|
|
|
|
import os
|
|
import re
|
|
|
|
import trafilatura
|
|
from readability import Document
|
|
from lxml import html as lxml_html
|
|
|
|
# Real content pages are essentially never this short; block/challenge
|
|
# interstitials ("Automated bot check in progress...") routinely are.
|
|
MIN_TEXT_CHARS = int(os.environ.get("MIN_TEXT_CHARS", "500"))
|
|
|
|
|
|
def _trafilatura_extract(html: str, url: str, favor_recall: bool) -> str:
|
|
return (
|
|
trafilatura.extract(
|
|
html,
|
|
url=url,
|
|
include_tables=True,
|
|
include_links=False,
|
|
include_comments=False,
|
|
favor_recall=favor_recall,
|
|
)
|
|
or ""
|
|
)
|
|
|
|
|
|
def _readability_extract(html: str) -> str:
|
|
try:
|
|
doc = Document(html)
|
|
tree = lxml_html.fromstring(doc.summary())
|
|
text = tree.text_content()
|
|
# Collapse the whitespace soup text_content() produces.
|
|
text = re.sub(r"[ \t]+", " ", text)
|
|
text = re.sub(r"\n\s*\n\s*", "\n\n", text)
|
|
return text.strip()
|
|
except Exception:
|
|
return ""
|
|
|
|
|
|
def _metadata(html: str, url: str) -> tuple[str | None, dict]:
|
|
meta = trafilatura.extract_metadata(html, default_url=url)
|
|
if meta is None:
|
|
return None, {}
|
|
fields = {
|
|
k: v
|
|
for k, v in {
|
|
"author": meta.author,
|
|
"date": meta.date,
|
|
"sitename": meta.sitename,
|
|
"description": meta.description,
|
|
}.items()
|
|
if v
|
|
}
|
|
return meta.title, fields
|
|
|
|
|
|
def extract_content(html: str, url: str) -> dict:
|
|
candidates = [
|
|
("trafilatura", lambda: _trafilatura_extract(html, url, favor_recall=False)),
|
|
("trafilatura_recall", lambda: _trafilatura_extract(html, url, favor_recall=True)),
|
|
("readability", lambda: _readability_extract(html)),
|
|
]
|
|
|
|
best_name, best_text = "none", ""
|
|
for name, fn in candidates:
|
|
text = fn()
|
|
if len(text) > len(best_text):
|
|
best_name, best_text = name, text
|
|
if len(best_text) >= MIN_TEXT_CHARS and name == "trafilatura":
|
|
break # primary extractor produced substantial content; done
|
|
|
|
title, metadata = _metadata(html, url)
|
|
return {
|
|
"title": title,
|
|
"text": best_text,
|
|
"metadata": metadata,
|
|
"extractor_used": best_name if best_text else "none",
|
|
"thin": len(best_text) < MIN_TEXT_CHARS,
|
|
}
|