#!/usr/bin/env python3
from concurrent.futures import ThreadPoolExecutor, as_completed
from pathlib import Path
import os, subprocess, re
BASE=Path('/home/spazio/cosmos_books/talk-to-the-entities-beginning-manual')
IMG=BASE/'ocr_images'; PAGES=BASE/'text/pages'; FULL=BASE/'text/book_full.txt'
TESS='/home/spazio/.local/opt/tesseract-local/usr/bin/tesseract'
env=os.environ.copy(); env['LD_LIBRARY_PATH']='/home/spazio/.local/opt/tesseract-local/usr/lib/x86_64-linux-gnu'; env['TESSDATA_PREFIX']='/home/spazio/.local/opt/tesseract-local/usr/share/tesseract-ocr/5/tessdata'; env['OMP_THREAD_LIMIT']='1'
PAGES.mkdir(parents=True,exist_ok=True)
imgs=sorted(IMG.glob('page-*.jpg'))
def run(img):
    out=PAGES/img.stem
    txt=out.with_suffix('.txt')
    if txt.exists() and txt.stat().st_size>100: return img.stem,'skip'
    subprocess.run([TESS,str(img),str(out),'-l','rus','--psm','3'],env=env,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL,check=True)
    return img.stem,'ok'
with ThreadPoolExecutor(max_workers=4) as ex:
    futs=[ex.submit(run,x) for x in imgs]
    for f in as_completed(futs): print(*f.result(),flush=True)
with FULL.open('w',encoding='utf-8') as w:
    for img in imgs:
        num=img.stem.split('-')[-1]
        txt=(PAGES/img.stem).with_suffix('.txt')
        w.write(f'\n\n--- PAGE {num} ---\n\n')
        w.write(txt.read_text(encoding='utf-8',errors='replace'))
for x in imgs: x.unlink()
IMG.rmdir()
print('DONE',FULL.stat().st_size)
