Update watcher.py

This commit is contained in:
TylerCG 2025-09-13 21:02:34 -04:00
parent 040d626c3a
commit 20f24d7a42

View File

@ -1,3 +1,4 @@
#!/usr/bin/env python3
import os
import subprocess
import time
@ -9,7 +10,7 @@ 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
self.seen_dirs = set() # Track processed directories
def on_created(self, event):
if event.is_directory:
@ -20,29 +21,31 @@ class NewBookHandler(FileSystemEventHandler):
self.process_new_dir(event.dest_path)
def process_new_dir(self, path):
# avoid duplicate processing
# Avoid duplicate processing
if path in self.seen_dirs:
return
self.seen_dirs.add(path)
# small delay to allow files to finish copying
# Short 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("._")
]
files = os.listdir(path)
except FileNotFoundError:
return # directory disappeared
return # directory may have been removed
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 yet)")
print(f"⚠️ Skipping {path} (no .m4b files detected)")
return
book_name = os.path.basename(path)
print(f"📕 New book detected: {book_name} at {path}")
print("Contains .m4b files:")
for f in m4b_files:
print(" ", f)
# Run your main.py script
try:
subprocess.run(
["python3", "main.py", "-da", path],
@ -59,6 +62,15 @@ 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
try:
while True:
time.sleep(1)