feat: Add dir monitor

This commit is contained in:
2025-05-31 12:28:29 +03:00
parent be279a5f86
commit b2b6355977
2 changed files with 323 additions and 0 deletions
+54
View File
@@ -0,0 +1,54 @@
#!/usr/bin/env python3
import sys
from pathlib import Path
import shutil
import subprocess
import logging
# Configure logging
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
logger = logging.getLogger(__name__)
TARGET_DIR = Path("/mnt/box/files/_raw/prt/")
SCAN_SCRIPT_PATH = Path("~/_scripts/scan_vids.py").expanduser()
VIDEO_EXTENSIONS = {".mkv", ".mp4"}
def main():
# Simplified: Assume correct arguments are passed
event_name = sys.argv[1]
src_path = Path(sys.argv[2]).resolve()
logger.info(f"Received event: {event_name}, Source path: {src_path}")
if event_name.lower() == "moved":
src_path = Path(sys.argv[3]).resolve()
if not src_path.is_file() or src_path.suffix.lower() not in VIDEO_EXTENSIONS:
logger.info(f"Ignoring non-video file or non-existent file: {src_path.name}")
exit(0)
return
logger.info(f"Processing created video file: {src_path}")
target_path = TARGET_DIR / src_path.name
logger.info(f"Moving '{src_path}' to '{target_path}'")
src_path.rename(target_path)
subprocess.run(
[str(SCAN_SCRIPT_PATH), str(target_path)],
text=True,
check=True,
)
has_other_videos = any(f.is_file() and f.suffix.lower() in VIDEO_EXTENSIONS for f in src_path.parent.iterdir())
has_large_files = any(f.is_file() and f.stat().st_size > 30 * 1024 * 1024 for f in src_path.parent.iterdir())
if not (has_other_videos or has_large_files):
logger.info(f"No other video files or large files in '{src_path.parent}'. Deleting it.")
shutil.rmtree(src_path.parent)
if __name__ == "__main__":
main()