2019-12-28 15:37:08 -08:00
|
|
|
# Copyright (C) 2019 Ian Alexander: https://github.com/ianalexander
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2020-01-28 12:56:19 -08:00
|
|
|
import logging
|
2019-12-28 15:37:08 -08:00
|
|
|
import os
|
|
|
|
import time
|
|
|
|
from datetime import datetime
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
from watchdog.events import PatternMatchingEventHandler
|
|
|
|
from watchdog.observers import Observer
|
|
|
|
|
|
|
|
import ocrmypdf
|
|
|
|
|
|
|
|
INPUT_DIRECTORY = os.getenv('OCR_INPUT_DIRECTORY', '/input')
|
|
|
|
OUTPUT_DIRECTORY = os.getenv('OCR_OUTPUT_DIRECTORY', '/output')
|
2020-01-20 10:45:28 -08:00
|
|
|
OUTPUT_DIRECTORY_YEAR_MONTH = bool(os.getenv('OCR_OUTPUT_DIRECTORY_YEAR_MONTH', False))
|
2020-01-19 19:11:54 -08:00
|
|
|
ON_SUCCESS_DELETE = bool(os.getenv('OCR_ON_SUCCESS_DELETE', False))
|
|
|
|
DESKEW = bool(os.getenv('OCR_DESKEW', False))
|
2020-01-20 10:45:28 -08:00
|
|
|
POLL_NEW_FILE_SECONDS = os.getenv('OCR_POLL_NEW_FILE_SECONDS', 1)
|
|
|
|
LOGLEVEL = os.environ.get('OCR_LOGLEVEL', 'INFO').upper()
|
2019-12-28 15:37:08 -08:00
|
|
|
PATTERNS = ['*.pdf']
|
|
|
|
|
2020-01-28 12:56:19 -08:00
|
|
|
log = logging.getLogger('ocrmypdf-watcher')
|
2019-12-28 15:37:08 -08:00
|
|
|
|
2020-01-28 12:56:19 -08:00
|
|
|
|
|
|
|
def get_output_dir(root, basename):
|
2019-12-28 15:37:08 -08:00
|
|
|
if OUTPUT_DIRECTORY_YEAR_MONTH:
|
|
|
|
today = datetime.today()
|
2020-01-28 12:56:19 -08:00
|
|
|
output_directory_year_month = (
|
|
|
|
Path(root) / str(today.year) / f'{today.month:02d}'
|
2019-12-28 15:37:08 -08:00
|
|
|
)
|
|
|
|
if not output_directory_year_month.exists():
|
|
|
|
output_directory_year_month.mkdir(parents=True, exist_ok=True)
|
2020-01-28 12:56:19 -08:00
|
|
|
output_path = Path(output_directory_year_month) / basename
|
2019-12-28 15:37:08 -08:00
|
|
|
else:
|
2020-01-28 12:56:19 -08:00
|
|
|
output_path = Path(OUTPUT_DIRECTORY) / basename
|
|
|
|
return output_path
|
|
|
|
|
|
|
|
|
|
|
|
def wait_for_file_ready(file_path):
|
2020-01-19 19:11:54 -08:00
|
|
|
# This loop waits to make sure that the file is completely loaded on
|
|
|
|
# disk before attempting to read. Docker sometimes will publish the
|
|
|
|
# watchdog event before the file is actually fully on disk, causing
|
|
|
|
# pikepdf to fail.
|
2020-01-28 12:56:19 -08:00
|
|
|
|
2020-01-19 19:11:54 -08:00
|
|
|
current_size = None
|
2020-01-28 12:56:19 -08:00
|
|
|
while current_size != file_path.stat().st_size:
|
|
|
|
current_size = file_path.stat().st_size
|
|
|
|
log.debug(f'file_path current_size: {current_size}')
|
2020-01-20 10:45:28 -08:00
|
|
|
time.sleep(POLL_NEW_FILE_SECONDS)
|
2020-01-28 12:56:19 -08:00
|
|
|
|
|
|
|
|
|
|
|
def execute_ocrmypdf(file_path):
|
|
|
|
file_path = Path(file_path)
|
|
|
|
output_path = get_output_dir(OUTPUT_DIRECTORY, file_path.name)
|
|
|
|
|
|
|
|
log.info("-" * 20)
|
|
|
|
log.info(f'New file: {file_path}. Waiting until fully loaded...')
|
|
|
|
log.info(f'Attempting to OCRmyPDF to: {output_path}')
|
|
|
|
wait_for_file_ready(file_path)
|
2020-01-19 19:11:54 -08:00
|
|
|
exit_code = ocrmypdf.ocr(
|
|
|
|
input_file=file_path, output_file=output_path, deskew=DESKEW
|
|
|
|
)
|
|
|
|
if exit_code == 0 and ON_SUCCESS_DELETE:
|
2020-01-28 12:56:19 -08:00
|
|
|
log.info(f'OCR is done. Deleting: {file_path}')
|
|
|
|
file_path.unlink()
|
2020-01-19 19:11:54 -08:00
|
|
|
else:
|
2020-01-28 12:56:19 -08:00
|
|
|
log.info('OCR is done')
|
2019-12-28 15:37:08 -08:00
|
|
|
|
|
|
|
|
|
|
|
class HandleObserverEvent(PatternMatchingEventHandler):
|
|
|
|
def on_any_event(self, event):
|
2020-01-19 19:11:54 -08:00
|
|
|
if event.event_type in ['created']:
|
2019-12-28 15:37:08 -08:00
|
|
|
execute_ocrmypdf(event.src_path)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2020-01-28 12:56:19 -08:00
|
|
|
ocrmypdf.configure_logging(
|
|
|
|
verbosity=ocrmypdf.Verbosity.default, manage_root_logger=True
|
|
|
|
)
|
|
|
|
log.info(
|
2019-12-28 15:37:08 -08:00
|
|
|
f"Starting OCRmyPDF watcher with config:\n"
|
|
|
|
f"Input Directory: {INPUT_DIRECTORY}\n"
|
|
|
|
f"Output Directory: {OUTPUT_DIRECTORY}\n"
|
|
|
|
f"Output Directory Year & Month: {OUTPUT_DIRECTORY_YEAR_MONTH}"
|
|
|
|
)
|
2020-01-28 12:56:19 -08:00
|
|
|
log.debug(
|
2020-01-20 10:45:28 -08:00
|
|
|
f"INPUT_DIRECTORY: {INPUT_DIRECTORY}\n"
|
|
|
|
f"OUTPUT_DIRECTORY: {OUTPUT_DIRECTORY}\n"
|
|
|
|
f"OUTPUT_DIRECTORY_YEAR_MONTH: {OUTPUT_DIRECTORY_YEAR_MONTH}\n"
|
|
|
|
f"ON_SUCCESS_DELETE: {ON_SUCCESS_DELETE}\n"
|
|
|
|
f"DESKEW: {DESKEW}\n"
|
|
|
|
f"POLL_NEW_FILE_SECONDS: {POLL_NEW_FILE_SECONDS}\n"
|
|
|
|
f"LOGLEVEL: {LOGLEVEL}\n"
|
|
|
|
)
|
2020-01-28 12:56:19 -08:00
|
|
|
|
2019-12-28 15:37:08 -08:00
|
|
|
handler = HandleObserverEvent(patterns=PATTERNS)
|
|
|
|
observer = Observer()
|
|
|
|
observer.schedule(handler, INPUT_DIRECTORY, recursive=True)
|
|
|
|
observer.start()
|
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
time.sleep(1)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
observer.stop()
|
|
|
|
observer.join()
|