From 79666caeeb9f05907c639788a5f51589813e4a0e Mon Sep 17 00:00:00 2001 From: TylerCG <117808427+TylerCG@users.noreply.github.com> Date: Sat, 13 Sep 2025 21:10:08 -0400 Subject: [PATCH] Update watcher.py --- watcher.py | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/watcher.py b/watcher.py index f9eabc1..f77ad70 100644 --- a/watcher.py +++ b/watcher.py @@ -6,6 +6,8 @@ from watchdog.observers import Observer from watchdog.events import FileSystemEventHandler WATCH_DIR = "/data" # Mount your host Books folder here +DEBUG = os.getenv("DEBUG", "false").lower() == "true" +MERGE_ALL = os.getenv("MERGE_ALL", "true").lower() == "true" class NewBookHandler(FileSystemEventHandler): def __init__(self): @@ -36,7 +38,14 @@ class NewBookHandler(FileSystemEventHandler): m4b_files = [f for f in files if f.lower().endswith(".m4b") and not f.startswith("._")] if not m4b_files: - print(f"⚠️ Skipping {path} (no .m4b files detected)") + if DEBUG: + print(f"⚠️ Skipping {path} (no .m4b files detected)") + return + + # Skip if only 1 file and MERGE_ALL is enabled + if MERGE_ALL and len(m4b_files) == 1: + if DEBUG: + print(f"⏭️ Skipping {path} (only 1 .m4b file, nothing to merge)") return book_name = os.path.basename(path) @@ -47,10 +56,10 @@ class NewBookHandler(FileSystemEventHandler): # Run your main.py script try: - subprocess.run( - ["python3", "main.py", "-da", path], - check=True - ) + cmd = ["python3", "main.py", "-da", path] + if MERGE_ALL: + cmd.append("--all") # pass flag to main.py + subprocess.run(cmd, check=True) print(f"✅ Finished processing {book_name}") except subprocess.CalledProcessError as e: print(f"❌ Error processing {book_name}: {e}") @@ -62,14 +71,14 @@ if __name__ == "__main__": observer.start() print(f"👀 Watching for new book folders in {WATCH_DIR} ...") - # --- Startup scan: just print existing folders --- - for root, dirs, files in os.walk(WATCH_DIR): - m4b_files = [f for f in files if f.lower().endswith(".m4b") and not f.startswith("._")] - if m4b_files: - print(f"📁 Existing book folder detected (startup scan): {root}") - for f in m4b_files: - print(" ", f) - # Do NOT call process_new_dir -> prevents running main.py on existing files + # --- Startup scan: just print existing folders if DEBUG is enabled --- + if DEBUG: + for root, dirs, files in os.walk(WATCH_DIR): + m4b_files = [f for f in files if f.lower().endswith(".m4b") and not f.startswith("._")] + if m4b_files: + print(f"📁 Existing book folder detected (startup scan): {root}") + for f in m4b_files: + print(" ", f) try: while True: