From d5b2e24423c654ebdfbb0abd230874e2d4fa5f7f Mon Sep 17 00:00:00 2001 From: CaliBrain Date: Sun, 16 Mar 2025 02:40:09 -0400 Subject: [PATCH] feat: Add CUSTOM_SCRIPT documentation and implementation details (#90) --- backend.py | 8 ++++++-- config.py | 11 +++++++++++ readme.md | 23 +++++++++++++++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/backend.py b/backend.py index 351682f..4ebb57d 100644 --- a/backend.py +++ b/backend.py @@ -6,7 +6,7 @@ from pathlib import Path from typing import Dict, List, Optional, Any, Tuple from logger import setup_logger -from config import TMP_DIR, MAIN_LOOP_SLEEP_TIME, INGEST_DIR +from config import TMP_DIR, MAIN_LOOP_SLEEP_TIME, INGEST_DIR, CUSTOM_SCRIPT from models import book_queue, BookInfo, QueueStatus, SearchFilters import book_manager @@ -118,9 +118,13 @@ def _download_book(book_id: str) -> bool: if not success: raise Exception("Unkown error downloading book") + if CUSTOM_SCRIPT: + subprocess.run([CUSTOM_SCRIPT, book_path]) + final_path = INGEST_DIR / f"{book_id}.{book_info.format}" - shutil.move(book_path, final_path) + if os.path.exists(book_path): + shutil.move(book_path, final_path) return True except Exception as e: logger.error(f"Error downloading book: {e}") diff --git a/config.py b/config.py index 3ceb124..d7a2203 100644 --- a/config.py +++ b/config.py @@ -66,6 +66,17 @@ BOOK_LANGUAGE = [l for l in BOOK_LANGUAGE if l in [lang['code'] for lang in _SUP if len(BOOK_LANGUAGE) == 0: BOOK_LANGUAGE = ['en'] +# Custom script settings +CUSTOM_SCRIPT = os.getenv("CUSTOM_SCRIPT", None).strip() +# check if the script is valid +if CUSTOM_SCRIPT: + if not os.path.exists(CUSTOM_SCRIPT): + logger.error(f"Custom script {CUSTOM_SCRIPT} does not exist") + CUSTOM_SCRIPT = None + elif not os.access(CUSTOM_SCRIPT, os.X_OK): + logger.error(f"Custom script {CUSTOM_SCRIPT} is not executable") + CUSTOM_SCRIPT = None + # API settings FLASK_HOST = os.getenv("FLASK_HOST", "0.0.0.0") FLASK_PORT = int(os.getenv("FLASK_PORT", 5003)) diff --git a/readme.md b/readme.md index 361821a..6d9c839 100644 --- a/readme.md +++ b/readme.md @@ -105,6 +105,29 @@ HTTP_PROXY=http://username:password@proxy.example.com:8080 HTTPS_PROXY=http://username:password@proxy.example.com:8080 ``` + +#### Custom configuration + +| Variable | Description | Default Value | +| ---------------------- | ----------------------------------------------------------- | ----------------------- | +| `CUSTOM_SCRIPT` | Path to an executable script that tuns after each download | `` | + +If `CUSTOM_SCRIPT` is set, it will be executed after each successful download but before the file is moved to the ingest directory. This allows for custom processing like format conversion or validation. + +The script is called with the full path of the downloaded file as its argument. Important notes: +- The script must preserve the original filename for proper processing +- The file can be modified or even deleted if needed +- The file will be moved to `/cwa-book-ingest` after the script execution (if not deleted) + +You can specify these configuration in this format : +``` +environment: + - CUSTOM_SCRIPT=/scripts/process-book.sh + +volumes: + - local/scripts/custom_script.sh:/scripts/process-book.sh +``` + ### Volume Configuration ```yaml