pryscraper/parser.py
cryptorugmunch a7c30b12cd
Some checks failed
CI / lint (push) Failing after 2s
CI / typecheck (push) Failing after 2s
CI / test (push) Failing after 2s
CI / Secret scan (gitleaks) (push) Failing after 1s
CI / Security audit (bandit) (push) Failing after 2s
chore(lint): auto-fix 253 of 283 ruff issues (F401, I001, E402, RUF100, UP037, SIM105)
Mass ruff auto-fix:
  - ruff check --fix: 109 issues fixed (F401 unused imports,
    I001 unsorted imports, UP037 quoted annotations, SIM105
    suppressible exception, RUF100 unused-noqa)
  - ruff check --fix --unsafe-fixes: 22 additional issues
  - ruff format: 70 files reformatted
  - Manual pass: fix 16 misplaced import httpx lines
  - Manual pass: fix remaining E402 (import-after-docstring)

Result: 283 errors -> 30 errors.

The remaining 30 are real issues that need manual review:
  5 F401 unused-import (likely auto-generated stubs)
  5 F821 undefined-name (real bugs in code that references
    redis/pydantic/LLMRegistry without imports)
  3 BLE001 (the compliance LLM fallback is intentional; the
    other two are real)
  3 RUF012 mutable-class-default
  3 SIM105, 3 SIM117, 2 E722, 2 E741
  1 B007, 1 B025, 1 E402, 1 RUF200 (pyproject.toml issue)

Tests: 436/437 pass (1 pre-existing SSE sandbox failure).
format check + import sort: now clean.
make ci: still gated on the 30 remaining real issues.
Follow-up: triage the 30 issues file-by-file.
2026-07-02 21:51:25 +02:00

126 lines
4.9 KiB
Python

"""Pry — document parser for PDF, DOCX, images, CSV, JSON, HTML.
Uses asyncio subprocess for CPU-bound OCR to avoid blocking the event loop.
Temp files are always cleaned up in finally blocks.
"""
# SPDX-License-Identifier: MIT
# Copyright (c) 2026 Rug Munch Media LLC
#
# Part of Pry — https://git.rugmunch.io/RugMunchMedia/pryscraper
# Licensed under MIT. See LICENSE.
import asyncio
import contextlib
import io
import os
import tempfile
from pathlib import Path
from typing import Any
class DocumentParser:
"""Parse PDFs, DOCX, images, and other document formats to clean text.
CPU-bound operations (OCR, PDF parsing) run in executor threads."""
async def parse(self, url: str, timeout: int = 60) -> dict[str, Any]:
import httpx
async with httpx.AsyncClient(
timeout=httpx.Timeout(timeout), follow_redirects=True
) as client:
resp = await client.get(url)
resp.raise_for_status()
content = resp.content
content_type = resp.headers.get("content-type", "")
filename = Path(url).name.lower()
loop = asyncio.get_event_loop()
return await loop.run_in_executor(None, self._parse_bytes, content, content_type, filename)
def _parse_bytes(self, data: bytes, content_type: str, filename: str = "") -> dict[str, Any]:
if filename.endswith(".pdf") or "pdf" in content_type:
return self._parse_pdf(data)
elif filename.endswith(".docx") or "word" in content_type:
return self._parse_docx(data)
elif filename.endswith((".md", ".markdown")) or "markdown" in content_type:
return {
"text": data.decode("utf-8", errors="replace"),
"format": "markdown",
"pages": 1,
}
elif filename.endswith(".csv") or "csv" in content_type:
return {"text": data.decode("utf-8", errors="replace"), "format": "csv", "pages": 1}
elif filename.endswith(".json") or "json" in content_type:
return {"text": data.decode("utf-8", errors="replace"), "format": "json", "pages": 1}
elif filename.endswith((".html", ".htm")) or "html" in content_type:
import trafilatura
text = data.decode("utf-8", errors="replace")
extracted = trafilatura.extract(text, output_format="markdown")
return {"text": extracted or text[:500000], "format": "html", "pages": 1}
elif (
filename.endswith((".png", ".jpg", ".jpeg", ".gif", ".webp")) or "image" in content_type
):
return self._parse_image(data)
elif filename.endswith(".txt") or "text" in content_type:
return {"text": data.decode("utf-8", errors="replace"), "format": "text", "pages": 1}
else:
try:
text = data.decode("utf-8", errors="replace")
return {"text": text[:500000], "format": "unknown", "pages": 1}
except UnicodeDecodeError:
return {
"text": f"[Binary file: {filename}, {len(data)} bytes]",
"format": "binary",
"pages": 0,
}
def _parse_pdf(self, data: bytes) -> dict[str, Any]:
from pypdf import PdfReader
reader = PdfReader(io.BytesIO(data))
pages = []
for page in reader.pages:
text = page.extract_text()
if text:
pages.append(text)
return {
"text": "\n\n".join(pages),
"format": "pdf",
"pages": len(pages),
"metadata": {
"title": reader.metadata.get("/Title", "") or "",
"author": reader.metadata.get("/Author", "") or "",
},
}
def _parse_docx(self, data: bytes) -> dict[str, Any]:
from docx import Document
doc = Document(io.BytesIO(data))
paragraphs = [p.text for p in doc.paragraphs if p.text.strip()]
return {"text": "\n\n".join(paragraphs), "format": "docx", "pages": 1}
def _parse_image(self, data: bytes) -> dict[str, Any]:
"""OCR via tesseract. Temp file cleaned up in finally block."""
fname = None
try:
from PIL import Image
img = Image.open(io.BytesIO(data))
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as f:
img.save(f, format="PNG")
fname = f.name
result = __import__("subprocess").run(
["tesseract", fname, "stdout", "--oem", "1", "-l", "eng"],
capture_output=True,
text=True,
timeout=30,
)
return {"text": result.stdout, "format": "image", "pages": 1}
except OSError as e:
return {"text": f"[Image OCR failed: {e}]", "format": "image", "pages": 0}
finally:
if fname and os.path.exists(fname):
with contextlib.suppress(OSError):
os.unlink(fname)