seedbox_sync.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # Usage: sonarr_sync.py my-seedbox /seedbox/path/to/data /local/working /local/metadata /local/data
  2. # Get all file names in HOST:HOST_DATA_PATH
  3. # Get all previously processed file names in LOCAL_METADATA_PATH
  4. # Diff the above to get newly added files
  5. # For each new file:
  6. # Copy file to LOCAL_WORKING_PATH (used in case of transfer failure)
  7. # Add file name to LOCAL_METADATA_PATH
  8. # Move file to LOCAL_DATA_PATH
  9. # */1 * * * * /usr/bin/run-one /usr/bin/python3 /path/to/seedbox_sync.py <seedbox host> /seedbox/path/to/completed/ /local/path/to/downloading /local/path/to/processed /local/path/to/ready 2>&1 | /usr/bin/logger -t seedbox
  10. import subprocess
  11. import sys
  12. if len(sys.argv) != 6:
  13. print("One or more args are undefined")
  14. sys.exit(1)
  15. host, host_data_path, local_working_path, local_metadata_path, local_data_path = sys.argv[1:6]
  16. r = subprocess.run(["ssh", host, "bash", "-c", f"IFS=$'\n'; ls {host_data_path}"], stdout=subprocess.PIPE, check=True)
  17. available = {f for f in r.stdout.decode().split('\n') if f}
  18. r = subprocess.run(["bash", "-c", f"IFS=$'\n'; ls {local_metadata_path}"], stdout=subprocess.PIPE, check=True)
  19. processed = {f for f in r.stdout.decode().split('\n') if f}
  20. new = available - processed
  21. for new_file in new:
  22. # Be super cautious about empty file names, wouldn't want to `rm -rf` a folder by accident
  23. if not new_file:
  24. continue
  25. print(f"Processing: {new_file}")
  26. subprocess.run(["rsync", "-rsvv", f'{host}:{host_data_path}/{new_file}', f'{local_working_path}'], check=True)
  27. r = subprocess.run(["touch", f'{local_metadata_path}/{new_file}'], check=True)
  28. print(f"Moving to ready: {new_file}")
  29. subprocess.run(["rsync", "-r", f'{local_working_path}/{new_file}', f'{local_data_path}'], check=True)
  30. subprocess.run(["rm", "-rf", f'{local_working_path}/{new_file}'], check=True)