qbit_manage/scripts/mover.py
bobokun 4e08f62aaf
4.1.7 (#602)
* 4.1.7

* chore(docs): Sync wiki to docs [skip-cd]

* Bump flake8 from 7.0.0 to 7.1.0 (#585)

Bumps [flake8](https://github.com/pycqa/flake8) from 7.0.0 to 7.1.0.
- [Commits](https://github.com/pycqa/flake8/compare/7.0.0...7.1.0)

---
updated-dependencies:
- dependency-name: flake8
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump docker/build-push-action from 5 to 6 (#588)

* chore(docs): Sync wiki to docs [skip-cd]

* fix(unregistered): Ignore 520 (#592)

* fix: max vs min seeding time check (#596)

* fix: max vs min seeding time check

Allow max_seeding_time to be unlimited (-1) even if a min_seeding_time is set

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

* fix pre-commit test

* fix: check tracker status in udp & wss as well (#586)

* fix: check tracker status in udp & wss as well

* bump VERSION

---------

Co-authored-by: bobokun <12660469+bobokun@users.noreply.github.com>

* [pre-commit.ci] pre-commit autoupdate (#584)

updates:
- [github.com/hhatto/autopep8: v2.2.0 → v2.3.1](https://github.com/hhatto/autopep8/compare/v2.2.0...v2.3.1)
- [github.com/asottile/pyupgrade: v3.15.2 → v3.16.0](https://github.com/asottile/pyupgrade/compare/v3.15.2...v3.16.0)
- [github.com/PyCQA/flake8: 7.0.0 → 7.1.0](https://github.com/PyCQA/flake8/compare/7.0.0...7.1.0)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

* Fixes #595 to use BHD deleted reason list instead of API

* Fixes #591 [FR]: Logging header doesn't get logged for every run

* minor fixes in #591

* Adds deprecated message for bhd attribute in config

* Fix min_seeding_time tag removal when max_seeding_time is -1 (#598)

* Update share_limits.py to fix #590

---------

Co-authored-by: bobokun <12660469+bobokun@users.noreply.github.com>

* Adds logging to mover script

* Fixes [FR]: Remove orphaned without moving to orphaned_dir #590

* Fixes bug in printing schedule mode when run is True

* Fix bug in Use BHD list of reasons instead of API #595

* Add additional BHD deletion reason #595

* Fix bug in #595 when status contains optional message

* Update mover.py: add optional arg --status-filter to allow fine tune which torrents to pauseUpdate mover.py: add optional arg --status-filter to allow fine tune which torrents to pause (#599)

* Update mover.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update mover.py: making the default behavior to only move `completed` torrents

This will leave 1) unfinished files 2) actively seeding files in `downloading` torrents in cache. This helps to keep write-heavy operation on Cache, not on hard drive.

Change this to "all" if you want this to always move everything every time.

* minor fixes in help description

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: bobokun <12660469+bobokun@users.noreply.github.com>

* modify noHL threshold to address false positives

* Add additional BHD deletion reason #595

* Bump dependabot/fetch-metadata from 2.1.0 to 2.2.0 (#601)

Bumps [dependabot/fetch-metadata](https://github.com/dependabot/fetch-metadata) from 2.1.0 to 2.2.0.
- [Release notes](https://github.com/dependabot/fetch-metadata/releases)
- [Commits](https://github.com/dependabot/fetch-metadata/compare/v2.1.0...v2.2.0)

---
updated-dependencies:
- dependency-name: dependabot/fetch-metadata
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* 4.1.7

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: Actionbot <actions@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: bakerboy448 <55419169+bakerboy448@users.noreply.github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Amos (lflare) <me@amosng.com>
Co-authored-by: ineednewpajamas <73252768+ineednewpajamas@users.noreply.github.com>
Co-authored-by: Tony <5747393+convexshiba@users.noreply.github.com>
2024-07-05 16:55:11 -04:00

141 lines
5 KiB
Python
Executable file

#!/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 argparse
import logging
import os
import sys
import time
from datetime import datetime
from datetime import timedelta
# Configure logging
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s - %(levelname)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
handlers=[logging.StreamHandler(sys.stdout)],
)
parser = argparse.ArgumentParser(prog="Qbit Mover", description="Stop torrents and kick off Unraid mover process")
parser.add_argument("--host", help="qbittorrent host including port", required=True)
parser.add_argument("-u", "--user", help="qbittorrent user", default="admin")
parser.add_argument("-p", "--password", help="qbittorrent password", default="adminadmin")
parser.add_argument(
"--cache-mount",
"--cache_mount",
help="Cache mount point in Unraid. This is used to additionally filter for only torrents that exists on the cache mount."
"Use this option ONLY if you follow TRaSH Guides folder structure. (For default cache drive set this to /mnt/cache)",
default=None,
)
parser.add_argument(
"--days-from", "--days_from", help="Set Number of Days to stop torrents between two offsets", type=int, default=0
)
parser.add_argument("--days-to", "--days_to", help="Set Number of Days to stop torrents between two offsets", type=int, default=2)
parser.add_argument(
"--mover-old",
help="Use mover.old instead of mover. Useful if you're using the Mover Tuning Plugin",
action="store_true",
default=False,
)
parser.add_argument(
"--status-filter",
help="Define a status to limit which torrents to pause. Useful if you want to leave certain torrents unpaused.",
choices=[
"all",
"downloading",
"seeding",
"completed",
"paused",
"stopped",
"active",
"inactive",
"resumed",
"running",
"stalled",
"stalled_uploading",
"stalled_downloading",
"checking",
"moving",
"errored",
],
default="completed",
)
# --DEFINE VARIABLES--#
# --START SCRIPT--#
try:
from qbittorrentapi import APIConnectionError
from qbittorrentapi import Client
from qbittorrentapi import LoginFailed
except ModuleNotFoundError:
logging.error(
'Requirements Error: qbittorrent-api not installed. Please install using the command "pip install qbittorrent-api"'
)
sys.exit(1)
def filter_torrents(torrent_list, timeoffset_from, timeoffset_to, cache_mount):
result = []
for torrent in torrent_list:
if torrent.added_on >= timeoffset_to and torrent.added_on <= timeoffset_from:
if not cache_mount or exists_in_cache(cache_mount, torrent.content_path):
result.append(torrent)
elif torrent.added_on < timeoffset_to:
break
return result
def exists_in_cache(cache_mount, content_path):
cache_path = os.path.join(cache_mount, content_path.lstrip("/"))
return os.path.exists(cache_path)
def stop_start_torrents(torrent_list, pause=True):
for torrent in torrent_list:
if pause:
logging.info(f"Pausing: {torrent.name} [{torrent.added_on}]")
torrent.pause()
else:
logging.info(f"Resuming: {torrent.name} [{torrent.added_on}]")
torrent.resume()
if __name__ == "__main__":
current = datetime.now()
args = parser.parse_args()
if args.days_from > args.days_to:
raise ("Config Error: days_from must be set lower than days_to")
try:
client = Client(host=args.host, username=args.user, password=args.password)
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.")
timeoffset_from = current - timedelta(days=args.days_from)
timeoffset_to = current - timedelta(days=args.days_to)
torrent_list = client.torrents.info(status_filter=args.status_filter, sort="added_on", reverse=True)
torrents = filter_torrents(torrent_list, timeoffset_from.timestamp(), timeoffset_to.timestamp(), args.cache_mount)
# Pause Torrents
logging.info(f"Pausing [{len(torrents)}] torrents from {args.days_from} - {args.days_to} days ago")
stop_start_torrents(torrents, True)
time.sleep(10)
# Or using mover tunning
if args.mover_old:
# Start mover
logging.info("Starting mover.old to move files in to array disks.")
os.system("/usr/local/sbin/mover.old start")
else:
# Start mover
logging.info("Starting mover to move files in to array disks based on mover tuning preferences.")
os.system("/usr/local/sbin/mover start")
# Start Torrents
logging.info(f"Resuming [{len(torrents)}] paused torrents from {args.days_from} - {args.days_to} days ago")
stop_start_torrents(torrents, False)