Update watcher.py
This commit is contained in:
parent
040d626c3a
commit
20f24d7a42
30
watcher.py
30
watcher.py
@ -1,3 +1,4 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
import time
|
import time
|
||||||
@ -9,7 +10,7 @@ WATCH_DIR = "/data" # Mount your host Books folder here
|
|||||||
class NewBookHandler(FileSystemEventHandler):
|
class NewBookHandler(FileSystemEventHandler):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
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):
|
def on_created(self, event):
|
||||||
if event.is_directory:
|
if event.is_directory:
|
||||||
@ -20,29 +21,31 @@ 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):
|
||||||
# avoid duplicate processing
|
# Avoid duplicate processing
|
||||||
if path in self.seen_dirs:
|
if path in self.seen_dirs:
|
||||||
return
|
return
|
||||||
self.seen_dirs.add(path)
|
self.seen_dirs.add(path)
|
||||||
|
|
||||||
# small delay to allow files to finish copying
|
# Short delay to allow files to finish copying
|
||||||
time.sleep(2)
|
time.sleep(2)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
m4b_files = [
|
files = os.listdir(path)
|
||||||
f for f in os.listdir(path)
|
|
||||||
if f.lower().endswith(".m4b") and not f.startswith("._")
|
|
||||||
]
|
|
||||||
except FileNotFoundError:
|
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:
|
if not m4b_files:
|
||||||
print(f"⚠️ Skipping {path} (no .m4b files yet)")
|
print(f"⚠️ Skipping {path} (no .m4b files detected)")
|
||||||
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}")
|
||||||
|
print("Contains .m4b files:")
|
||||||
|
for f in m4b_files:
|
||||||
|
print(" ", f)
|
||||||
|
|
||||||
|
# Run your main.py script
|
||||||
try:
|
try:
|
||||||
subprocess.run(
|
subprocess.run(
|
||||||
["python3", "main.py", "-da", path],
|
["python3", "main.py", "-da", path],
|
||||||
@ -59,6 +62,15 @@ if __name__ == "__main__":
|
|||||||
observer.start()
|
observer.start()
|
||||||
print(f"👀 Watching for new book folders in {WATCH_DIR} ...")
|
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:
|
try:
|
||||||
while True:
|
while True:
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user