- renderer: Node/Playwright, single browser + pooled contexts with concurrency
cap and FIFO queue, Redis render cache (graceful no-op without Redis)
- extractor: FastAPI + Trafilatura, HTML -> {title, text, metadata}
- orchestrator: /scrape (render -> extract) with per-stage trace, aggregated
/health, structured JSON logging on all services
- docker-compose for orchestrator/renderer/extractor/redis with healthchecks
Verified locally: end-to-end scrape of example.com and Wikipedia; error paths
for unreachable hosts and bad schemes.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
47 lines
1.1 KiB
Python
47 lines
1.1 KiB
Python
"""Extractor: rendered HTML -> pure text/data. Mechanical only, no inference."""
|
|
|
|
import json
|
|
import logging
|
|
import sys
|
|
|
|
from fastapi import FastAPI
|
|
from pydantic import BaseModel
|
|
|
|
from .extract import extract_content
|
|
|
|
|
|
class JsonFormatter(logging.Formatter):
|
|
def format(self, record: logging.LogRecord) -> str:
|
|
return json.dumps(
|
|
{
|
|
"ts": self.formatTime(record),
|
|
"level": record.levelname.lower(),
|
|
"service": "extractor",
|
|
"msg": record.getMessage(),
|
|
}
|
|
)
|
|
|
|
|
|
handler = logging.StreamHandler(sys.stdout)
|
|
handler.setFormatter(JsonFormatter())
|
|
logging.basicConfig(level=logging.INFO, handlers=[handler])
|
|
log = logging.getLogger("extractor")
|
|
|
|
app = FastAPI(title="LWSS Extractor")
|
|
|
|
|
|
class ExtractRequest(BaseModel):
|
|
html: str
|
|
url: str
|
|
|
|
|
|
@app.get("/health")
|
|
def health() -> dict:
|
|
return {"status": "ok", "service": "extractor"}
|
|
|
|
|
|
@app.post("/extract")
|
|
def extract(req: ExtractRequest) -> dict:
|
|
result = extract_content(req.html, req.url)
|
|
log.info("extracted %s: %d chars via %s", req.url, len(result["text"]), result["extractor_used"])
|
|
return result
|