Update watcher.py
This commit is contained in:
parent
79666caeeb
commit
35917c506e
77
watcher.py
77
watcher.py
@ -1,84 +1,57 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
import time
|
import time
|
||||||
from watchdog.observers import Observer
|
from watchdog.observers import Observer
|
||||||
from watchdog.events import FileSystemEventHandler
|
from watchdog.events import FileSystemEventHandler
|
||||||
|
|
||||||
WATCH_DIR = "/data" # Mount your host Books folder here
|
WATCH_DIR = "/data"
|
||||||
DEBUG = os.getenv("DEBUG", "false").lower() == "true"
|
DEBUG = os.environ.get("DEBUG", "false").lower() == "true"
|
||||||
MERGE_ALL = os.getenv("MERGE_ALL", "true").lower() == "true"
|
MERGE_ALL = os.environ.get("MERGE_ALL", "false").lower() == "true"
|
||||||
|
|
||||||
class NewBookHandler(FileSystemEventHandler):
|
class NewBookHandler(FileSystemEventHandler):
|
||||||
def __init__(self):
|
|
||||||
super().__init__()
|
|
||||||
self.seen_dirs = set() # Track processed directories
|
|
||||||
|
|
||||||
def on_created(self, event):
|
def on_created(self, event):
|
||||||
|
if DEBUG:
|
||||||
|
print(f"📂 [DEBUG] Created: {event.src_path} (dir={event.is_directory})")
|
||||||
if event.is_directory:
|
if event.is_directory:
|
||||||
self.process_new_dir(event.src_path)
|
self.process_new_dir(event.src_path)
|
||||||
|
|
||||||
def on_moved(self, event):
|
def on_moved(self, event):
|
||||||
|
if DEBUG:
|
||||||
|
print(f"📂 [DEBUG] Moved: {event.src_path} → {event.dest_path} (dir={event.is_directory})")
|
||||||
if event.is_directory:
|
if event.is_directory:
|
||||||
self.process_new_dir(event.dest_path)
|
self.process_new_dir(event.dest_path)
|
||||||
|
|
||||||
|
def on_modified(self, event):
|
||||||
|
if DEBUG:
|
||||||
|
print(f"📂 [DEBUG] Modified: {event.src_path} (dir={event.is_directory})")
|
||||||
|
|
||||||
|
def on_deleted(self, event):
|
||||||
|
if DEBUG:
|
||||||
|
print(f"📂 [DEBUG] Deleted: {event.src_path} (dir={event.is_directory})")
|
||||||
|
|
||||||
def process_new_dir(self, path):
|
def process_new_dir(self, path):
|
||||||
# Avoid duplicate processing
|
# find .m4b files in this new book directory
|
||||||
if path in self.seen_dirs:
|
m4b_files = [
|
||||||
return
|
f for f in os.listdir(path)
|
||||||
self.seen_dirs.add(path)
|
if os.path.isfile(os.path.join(path, f)) and f.lower().endswith(".m4b") and not f.startswith("._")
|
||||||
|
]
|
||||||
|
|
||||||
# Short delay to allow files to finish copying
|
if MERGE_ALL and len(m4b_files) <= 1:
|
||||||
time.sleep(2)
|
|
||||||
|
|
||||||
try:
|
|
||||||
files = os.listdir(path)
|
|
||||||
except FileNotFoundError:
|
|
||||||
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:
|
|
||||||
if DEBUG:
|
if DEBUG:
|
||||||
print(f"⚠️ Skipping {path} (no .m4b files detected)")
|
print(f"⚠️ [DEBUG] Skipping {path}, only {len(m4b_files)} file(s).")
|
||||||
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
|
return
|
||||||
|
|
||||||
book_name = os.path.basename(path)
|
book_name = os.path.basename(path)
|
||||||
print(f"📕 New book detected: {book_name} at {path}")
|
print(f"📕 New book detected: {book_name} at {path} ({len(m4b_files)} file(s))")
|
||||||
print("Contains .m4b files:")
|
|
||||||
for f in m4b_files:
|
|
||||||
print(" ", f)
|
|
||||||
|
|
||||||
# Run your main.py script
|
subprocess.run(["python3", "main.py", "-da", path], check=True)
|
||||||
try:
|
|
||||||
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}")
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
print(f"👀 Watching {WATCH_DIR} for new books...")
|
||||||
observer = Observer()
|
observer = Observer()
|
||||||
event_handler = NewBookHandler()
|
event_handler = NewBookHandler()
|
||||||
observer.schedule(event_handler, WATCH_DIR, recursive=True)
|
observer.schedule(event_handler, WATCH_DIR, recursive=True)
|
||||||
observer.start()
|
observer.start()
|
||||||
print(f"👀 Watching for new book folders in {WATCH_DIR} ...")
|
|
||||||
|
|
||||||
# --- 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:
|
try:
|
||||||
while True:
|
while True:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user