Update watcher.py
This commit is contained in:
parent
5621d3531f
commit
8ced1d80f9
39
watcher.py
39
watcher.py
@ -4,22 +4,49 @@ 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 directory here
|
WATCH_DIR = "/data" # Mount host Books folder here (Books:/data)
|
||||||
|
|
||||||
class NewBookHandler(FileSystemEventHandler):
|
class NewBookHandler(FileSystemEventHandler):
|
||||||
def on_created(self, event):
|
def on_created(self, event):
|
||||||
if event.is_directory:
|
if event.is_directory:
|
||||||
book_dir = event.src_path
|
self.process_new_dir(event.src_path)
|
||||||
print(f"New directory detected: {book_dir}")
|
|
||||||
# Run your main.py with the new book directory
|
def on_moved(self, event):
|
||||||
subprocess.run(["python3", "main.py", "-da", book_dir], check=True)
|
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__":
|
if __name__ == "__main__":
|
||||||
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 folders in {WATCH_DIR} ...")
|
print(f"👀 Watching for new book folders in {WATCH_DIR} ...")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user