diff --git a/watcher.py b/watcher.py index b0a17b0..430091e 100644 --- a/watcher.py +++ b/watcher.py @@ -4,22 +4,49 @@ import time from watchdog.observers import Observer from watchdog.events import FileSystemEventHandler -WATCH_DIR = "/data" # Mount your host directory here +WATCH_DIR = "/data" # Mount host Books folder here (Books:/data) class NewBookHandler(FileSystemEventHandler): def on_created(self, event): if event.is_directory: - book_dir = event.src_path - print(f"New directory detected: {book_dir}") - # Run your main.py with the new book directory - subprocess.run(["python3", "main.py", "-da", book_dir], check=True) + self.process_new_dir(event.src_path) + + def on_moved(self, event): + if event.is_directory: + self.process_new_dir(event.dest_path) + + def process_new_dir(self, path): + # Only process if directory contains at least one .m4b file + try: + m4b_files = [ + f for f in os.listdir(path) + if f.lower().endswith(".m4b") and not f.startswith("._") + ] + except FileNotFoundError: + return # directory might have been deleted quickly + + if not m4b_files: + print(f"⚠️ Skipping {path} (no .m4b files found yet)") + return + + book_name = os.path.basename(path) + print(f"📕 New book detected: {book_name} at {path}") + + try: + subprocess.run( + ["python3", "main.py", "-da", path], + check=True + ) + print(f"✅ Finished processing {book_name}") + except subprocess.CalledProcessError as e: + print(f"❌ Error processing {book_name}: {e}") if __name__ == "__main__": observer = Observer() event_handler = NewBookHandler() observer.schedule(event_handler, WATCH_DIR, recursive=True) observer.start() - print(f"Watching for new folders in {WATCH_DIR} ...") + print(f"👀 Watching for new book folders in {WATCH_DIR} ...") try: while True: