diff --git a/watcher.py b/watcher.py index 430091e..d991e6b 100644 --- a/watcher.py +++ b/watcher.py @@ -4,9 +4,13 @@ import time from watchdog.observers import Observer from watchdog.events import FileSystemEventHandler -WATCH_DIR = "/data" # Mount host Books folder here (Books:/data) +WATCH_DIR = "/data" # Mount your host Books folder here class NewBookHandler(FileSystemEventHandler): + def __init__(self): + super().__init__() + self.seen_dirs = set() # avoid processing the same dir multiple times + def on_created(self, event): if event.is_directory: self.process_new_dir(event.src_path) @@ -16,17 +20,24 @@ class NewBookHandler(FileSystemEventHandler): self.process_new_dir(event.dest_path) def process_new_dir(self, path): - # Only process if directory contains at least one .m4b file + # avoid duplicate processing + if path in self.seen_dirs: + return + self.seen_dirs.add(path) + + # small delay to allow files to finish copying + time.sleep(2) + 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 + return # directory disappeared if not m4b_files: - print(f"⚠️ Skipping {path} (no .m4b files found yet)") + print(f"⚠️ Skipping {path} (no .m4b files yet)") return book_name = os.path.basename(path)