Update watcher.py
This commit is contained in:
parent
8ced1d80f9
commit
040d626c3a
19
watcher.py
19
watcher.py
@ -4,9 +4,13 @@ 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 host Books folder here (Books:/data)
|
WATCH_DIR = "/data" # Mount your host Books folder here
|
||||||
|
|
||||||
class NewBookHandler(FileSystemEventHandler):
|
class NewBookHandler(FileSystemEventHandler):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
self.seen_dirs = set() # avoid processing the same dir multiple times
|
||||||
|
|
||||||
def on_created(self, event):
|
def on_created(self, event):
|
||||||
if event.is_directory:
|
if event.is_directory:
|
||||||
self.process_new_dir(event.src_path)
|
self.process_new_dir(event.src_path)
|
||||||
@ -16,17 +20,24 @@ class NewBookHandler(FileSystemEventHandler):
|
|||||||
self.process_new_dir(event.dest_path)
|
self.process_new_dir(event.dest_path)
|
||||||
|
|
||||||
def process_new_dir(self, 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:
|
try:
|
||||||
m4b_files = [
|
m4b_files = [
|
||||||
f for f in os.listdir(path)
|
f for f in os.listdir(path)
|
||||||
if f.lower().endswith(".m4b") and not f.startswith("._")
|
if f.lower().endswith(".m4b") and not f.startswith("._")
|
||||||
]
|
]
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
return # directory might have been deleted quickly
|
return # directory disappeared
|
||||||
|
|
||||||
if not m4b_files:
|
if not m4b_files:
|
||||||
print(f"⚠️ Skipping {path} (no .m4b files found yet)")
|
print(f"⚠️ Skipping {path} (no .m4b files yet)")
|
||||||
return
|
return
|
||||||
|
|
||||||
book_name = os.path.basename(path)
|
book_name = os.path.basename(path)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user