feat: Add CUSTOM_SCRIPT documentation and implementation details (#90)

This commit is contained in:
CaliBrain
2025-03-16 02:40:09 -04:00
committed by GitHub
parent 3a92c5de78
commit d5b2e24423
3 changed files with 40 additions and 2 deletions

View File

@@ -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}")

View File

@@ -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))

View File

@@ -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