- fetch lädt die PDF-Archivfassungen; verify prüft jetzt auch die Seitenangabe jedes OCR-Zitats - 4 falsche Seitenangaben in case.toml korrigiert (Kontoauszüge #663, #182, #629, #653) - Scan-/Handschrift-Belege rot umrandet und auf dem Deckblatt als 'selbst prüfen' gelistet - passt ein Stempeltext nicht, bricht die Mappe ab statt etwas wegzulassen Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
53 lines
1.9 KiB
Python
53 lines
1.9 KiB
Python
"""Paperless-ngx: OCR-Texte der im Fall referenzierten Dokumente in den lokalen Cache holen."""
|
||
from __future__ import annotations
|
||
|
||
import json
|
||
import os
|
||
import urllib.request
|
||
from pathlib import Path
|
||
|
||
|
||
def _env(name: str) -> str:
|
||
val = os.environ.get(name)
|
||
if not val:
|
||
raise SystemExit(f"{name} fehlt (siehe .env.example)")
|
||
return val
|
||
|
||
|
||
def fetch_content(doc_id: int) -> str:
|
||
url = f"{_env('PAPERLESS_URL').rstrip('/')}/api/documents/{doc_id}/?fields=id,content"
|
||
req = urllib.request.Request(url, headers={"Authorization": f"Token {_env('PAPERLESS_TOKEN')}"})
|
||
with urllib.request.urlopen(req, timeout=300) as resp:
|
||
return json.load(resp)["content"]
|
||
|
||
|
||
def fetch_all(doc_ids: set[int], cache_dir: Path, force: bool = False) -> list[int]:
|
||
cache_dir.mkdir(parents=True, exist_ok=True)
|
||
fetched = []
|
||
for doc_id in sorted(doc_ids):
|
||
path = cache_dir / f"{doc_id}.txt"
|
||
if path.exists() and not force:
|
||
continue
|
||
path.write_text(fetch_content(doc_id), encoding="utf-8")
|
||
fetched.append(doc_id)
|
||
return fetched
|
||
|
||
|
||
def fetch_pdfs(doc_ids: set[int], cache_dir: Path, force: bool = False) -> list[int]:
|
||
"""Archivfassung (PDF mit OCR-Textebene) für die Beweismappe – Seiten werden daraus kopiert und markiert."""
|
||
cache_dir.mkdir(parents=True, exist_ok=True)
|
||
fetched = []
|
||
for doc_id in sorted(doc_ids):
|
||
path = cache_dir / f"{doc_id}.pdf"
|
||
if path.exists() and not force:
|
||
continue
|
||
url = f"{_env('PAPERLESS_URL').rstrip('/')}/api/documents/{doc_id}/download/"
|
||
req = urllib.request.Request(url, headers={"Authorization": f"Token {_env('PAPERLESS_TOKEN')}"})
|
||
with urllib.request.urlopen(req, timeout=300) as resp:
|
||
daten = resp.read()
|
||
if not daten.startswith(b"%PDF"):
|
||
raise SystemExit(f"Paperless #{doc_id}: Download ist kein PDF")
|
||
path.write_bytes(daten)
|
||
fetched.append(doc_id)
|
||
return fetched
|