66 lines
1.8 KiB
Python
Executable File
66 lines
1.8 KiB
Python
Executable File
#!/usr/bin/python3
|
|
|
|
import sys
|
|
import os
|
|
import fasteners
|
|
import time
|
|
import subprocess
|
|
|
|
OCRBASE="/ocr"
|
|
INBOX="{}/inbox".format(OCRBASE)
|
|
OUTBOX="{}/outbox".format(OCRBASE)
|
|
|
|
|
|
lock = fasteners.InterProcessLock("/var/lock/ocr-watcher.lock")
|
|
|
|
if lock.acquire(timeout=2):
|
|
|
|
if not os.path.isdir(OUTBOX):
|
|
os.mkdir(OUTBOX)
|
|
if not os.path.isdir("{}/failed".format(OCRBASE)):
|
|
os.mkdir("{}/failed".format(OCRBASE))
|
|
if not os.path.isdir("{}/inbox".format(OCRBASE)):
|
|
os.mkdir("{}/inbox".format(OCRBASE))
|
|
if not os.path.isdir("{}/spool".format(OCRBASE)):
|
|
os.mkdir("{}/spool".format(OCRBASE))
|
|
if not os.path.isdir("{}/archive".format(OCRBASE)):
|
|
os.mkdir("{}/archive".format(OCRBASE))
|
|
if not os.path.isdir("{}/log".format(OCRBASE)):
|
|
os.mkdir("{}/log".format(OCRBASE))
|
|
|
|
while (True):
|
|
for e in os.scandir(INBOX):
|
|
SRCFILE="{}/{}".format(INBOX, e.name)
|
|
FAILFILE="{}/failed/{}".format(OCRBASE, e.name)
|
|
|
|
if e.is_file() and e.name.endswith(".pdf"):
|
|
SPOOLFILE="{}/spool/{}".format(OCRBASE, e.name)
|
|
TEMPFILE="{}/spool/{}.tmp".format(OCRBASE, e.name)
|
|
ARCHIVEFILE="{}/archive/{}".format(OCRBASE, e.name)
|
|
DSTFILE="{}/{}".format(OUTBOX, e.name)
|
|
LOGFILE="{}/log/{}.out".format(OCRBASE, e.name)
|
|
ERRFILE="{}/log/{}.err".format(OCRBASE, e.name)
|
|
|
|
# os.rename(SRCFILE, SPOOLFILE)
|
|
|
|
with open(LOGFILE,"w") as logfile:
|
|
with open(ERRFILE,"w") as errfile:
|
|
|
|
if subprocess.run(["/usr/bin/ocrmypdf","-l","deu","--rotate-pages","--deskew","--jobs","4", SRCFILE, TEMPFILE], stderr=errfile, stdout=logfile).returncode != 0:
|
|
if os.path.isfile(TEMPFILE):
|
|
os.unlink(TEMPFILE)
|
|
os.rename(SRCFILE, FAILFILE)
|
|
else:
|
|
os.rename(SRCFILE, ARCHIVEFILE)
|
|
os.rename(TEMPFILE, DSTFILE)
|
|
elif not e.name.startswith(".") and not e.is_dir():
|
|
os.rename(SRCFILE, FAILFILE)
|
|
time.sleep(5)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|