Scripts to probe a book's TOC, print each page-level item to PDF via the print popup, verify, merge per 대항목 and clean up temp files. Invoked only by /lawnb-pdf-download (disable-model-invocation). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
58 lines
1.7 KiB
Python
58 lines
1.7 KiB
Python
#!/usr/bin/env python3
|
|
"""Check the individual PDFs written by print_items.js.
|
|
|
|
Usage: python3 -I verify_pdfs.py <tmpDir>
|
|
|
|
Reports: file count, duplicate files (same hash = a stale page was printed
|
|
twice), articles whose first heading does not match the file label, empty
|
|
PDFs, and the first heading + page count of every non-article file
|
|
(총설 / 전론 / 후론 ...) so a human can eyeball them.
|
|
Exit status 1 when anything is wrong.
|
|
"""
|
|
import hashlib
|
|
import os
|
|
import re
|
|
import subprocess
|
|
import sys
|
|
|
|
tmp = sys.argv[1]
|
|
files = sorted(f for f in os.listdir(tmp) if f.endswith(".pdf"))
|
|
bad = 0
|
|
|
|
|
|
def text(path):
|
|
return subprocess.run(["pdftotext", path, "-"], capture_output=True, text=True).stdout
|
|
|
|
|
|
def pages(path):
|
|
out = subprocess.run(["pdfinfo", path], capture_output=True, text=True).stdout
|
|
return int(re.search(r"Pages:\s+(\d+)", out).group(1))
|
|
|
|
|
|
seen = {}
|
|
for f in files:
|
|
h = hashlib.md5(open(os.path.join(tmp, f), "rb").read()).hexdigest()
|
|
if h in seen:
|
|
print("DUPLICATE", f, "==", seen[h])
|
|
bad = 1
|
|
seen[h] = f
|
|
print("files:", len(files))
|
|
|
|
for f in files:
|
|
p = os.path.join(tmp, f)
|
|
m = re.match(r"s.+?_(\d{2,})_(.+)\.pdf$", f)
|
|
lab = m.group(2)
|
|
lines = [l.strip() for l in text(p).split("\n") if l.strip()]
|
|
if not lines:
|
|
print("EMPTY", f)
|
|
bad = 1
|
|
continue
|
|
if re.match(r"제\d+조(의\d+)?$", lab):
|
|
if not any(re.match(re.escape(lab) + r"(\s|\[|$)", l) for l in lines[:14]):
|
|
print("MISMATCH", f, "| first lines:", lines[2:4])
|
|
bad = 1
|
|
else:
|
|
head = next((l for l in lines[2:14] if l), "")
|
|
print(f"{f} | {pages(p)}쪽 | {' / '.join(lines[2:4])[:80]}")
|
|
sys.exit(bad)
|