From 687e6110838f1aadfba6491bbafb5dfa3f4be009 Mon Sep 17 00:00:00 2001 From: bobokun Date: Sat, 29 Jan 2022 09:52:49 -0500 Subject: [PATCH] Added mover script for Unraid --- scripts/mover.py | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 scripts/mover.py diff --git a/scripts/mover.py b/scripts/mover.py new file mode 100644 index 0000000..dd94138 --- /dev/null +++ b/scripts/mover.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python3 +# This standalone script is used to pause torrents older than last x days, run mover (in Unraid) and start torrents again once completed +import os, sys, time +from datetime import datetime, timedelta + + +# --DEFINE VARIABLES--# +# Set Number of Days to stop torrents for the move +days = 2 +qbt_host = 'qbittorrent:8080' +qbt_user = None +qbt_pass = None +# --DEFINE VARIABLES--# + +# --START SCRIPT--# +try: + from qbittorrentapi import Client, LoginFailed, APIConnectionError +except ModuleNotFoundError: + print("Requirements Error: qbittorrentapi not installed. Please install with pip") + sys.exit(0) + +current = datetime.now() +timeoffset = (current - timedelta(days=days)).timestamp() + + +def stop_start_torrents(torrent_list, pause=True): + for torrent in torrent_list: + if (torrent.added_on >= timeoffset): + if pause: + torrent.pause() + else: + torrent.resume() + else: + break + + +if __name__ == '__main__': + try: + client = Client(host=qbt_host, username=qbt_user, password=qbt_pass) + except LoginFailed: + raise("Qbittorrent Error: Failed to login. Invalid username/password.") + except APIConnectionError: + raise("Qbittorrent Error: Unable to connect to the client.") + except Exception: + raise("Qbittorrent Error: Unable to connect to the client.") + torrent_list = client.torrents.info(sort='added_on', reverse=True) + + # Pause Torrents + print(f"Pausing torrents from the last {days} Days") + stop_start_torrents(torrent_list, True) + time.sleep(10) + # Start mover + print("Starting Mover") + os.system('/usr/local/sbin/mover.old start') + # Start Torrents + print(f"Resuming paused torrents from the last {days} Days") + stop_start_torrents(torrent_list, False)