62 lines
2.0 KiB
Python
62 lines
2.0 KiB
Python
import os
|
|
import subprocess
|
|
import time
|
|
from watchdog.observers import Observer
|
|
from watchdog.events import FileSystemEventHandler
|
|
|
|
WATCH_DIR = "/data"
|
|
DEBUG = os.environ.get("DEBUG", "false").lower() == "true"
|
|
MERGE_ALL = os.environ.get("MERGE_ALL", "false").lower() == "true"
|
|
|
|
class NewBookHandler(FileSystemEventHandler):
|
|
def on_created(self, event):
|
|
if DEBUG:
|
|
print(f"📂 [DEBUG] Created: {event.src_path} (dir={event.is_directory})")
|
|
if event.is_directory:
|
|
self.process_new_dir(event.src_path)
|
|
|
|
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:
|
|
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):
|
|
# find .m4b files in this new book directory
|
|
m4b_files = [
|
|
f for f in os.listdir(path)
|
|
if os.path.isfile(os.path.join(path, f)) and f.lower().endswith(".m4b") and not f.startswith("._")
|
|
]
|
|
|
|
if MERGE_ALL and len(m4b_files) <= 1:
|
|
if DEBUG:
|
|
print(f"⚠️ [DEBUG] Skipping {path}, only {len(m4b_files)} file(s).")
|
|
return
|
|
|
|
book_name = os.path.basename(path)
|
|
print(f"📕 New book detected: {book_name} at {path} ({len(m4b_files)} file(s))")
|
|
|
|
subprocess.run(["python3", "main.py", "-da", path], check=True)
|
|
|
|
if __name__ == "__main__":
|
|
print(f"👀 Watching {WATCH_DIR} for new books...")
|
|
observer = Observer()
|
|
event_handler = NewBookHandler()
|
|
observer.schedule(event_handler, WATCH_DIR, recursive=True)
|
|
observer.start()
|
|
|
|
try:
|
|
while True:
|
|
time.sleep(1)
|
|
except KeyboardInterrupt:
|
|
observer.stop()
|
|
observer.join()
|