Files
Server-scripts/torrent/torrent_qbittorrent_api.py
T

156 lines
3.9 KiB
Python
Executable File

#!/usr/bin/env python3
import argparse
import requests
API_KEY_PRIVATE = "qbt_Fw6R39WuJVY7rdhv2IXIc5n8Yfhc"
API_KEY_PUBLIC = "qbt_dsrXBb27K4HBqbVKhKruh5GTHHLz"
def get_torrents(instance):
HEADERS = {
"Authorization": f"Bearer {instance.api_key}",
}
r = requests.get(
f"http://{instance.ip_adress}/api/v2/torrents/info",
headers=HEADERS,
timeout=30,
)
r.raise_for_status()
return r.json()
def get_trackers(instance, torrent_hash):
HEADERS = {
"Authorization": f"Bearer {instance.api_key}",
}
r = requests.get(
f"http://{instance.ip_address}/api/v2/torrents/trackers",
params={"hash": torrent_hash},
headers=HEADERS,
timeout=30,
)
r.raise_for_status()
return r.json()
def set_force_start(instance, torrent_hash, enabled=True):
HEADERS = {
"Authorization": f"Bearer {instance.api_key}",
}
requests.post(
f"http://{instance.ip_adress}/api/v2/torrents/setForceStart",
data={
"hashes": torrent_hash,
"value": str(enabled).lower(),
},
headers=HEADERS,
timeout=30,
).raise_for_status()
def force_start_by_tracker(instance, trackers):
"""
trackers = {
"myanonamouse": True,
"leo-test": False,
"nordicq": True,
}
"""
for torrent in get_torrents(instance):
torrent_hash = torrent["hash"]
name = torrent["name"]
tracker_urls = " ".join(
t.get("url", "") for t in get_trackers(instance, torrent_hash)
).lower()
for tracker, force_start in trackers.items():
if tracker.lower() in tracker_urls:
set_force_start(torrent_hash, force_start)
print(f"{name} -> force_start={force_start} (matched {tracker})")
break
def format_duration(seconds):
hours = seconds / 3600
days = seconds // 86400
return f"{days}d ({hours:.1f}h)"
def show_torrents(instance, sort_by="name", reverse=True):
sort_keys = {
"name": lambda t: t["name"].lower(),
"ratio": lambda t: t.get("ratio", 0),
"seed_time": lambda t: t.get("seeding_time", 0),
}
torrents = sorted(
get_torrents(instance),
key=sort_keys[sort_by],
reverse=reverse,
)
rows = [
(
t["name"],
f"{t.get('ratio', 0):.2f}",
format_duration(t.get("seeding_time", 0)),
)
for t in torrents
]
name_w = max((len(name) for name, _, _ in rows), default=0)
ratio_w = max((len(ratio) for _, ratio, _ in rows), default=0)
print("=============================")
print(f"Instance: {instance.name}")
print("=============================")
for name, ratio, seed_time in rows:
print(f"{name:<{name_w}} Ratio: {ratio:>{ratio_w}} Seed Time: {seed_time}")
class Qbittorrent:
def __init__(self, ip_adress, name, api_key) -> None:
self.ip_adress = ip_adress
self.name = name
self.api_key = api_key
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--sort",
choices=["name", "ratio", "seed_time"],
default="name",
)
parser.add_argument(
"--force",
action="store_true",
)
parser.add_argument(
"--asc",
action="store_true",
)
args = parser.parse_args()
private = Qbittorrent("192.168.0.2:8080", "PRIVATE", API_KEY_PRIVATE)
public = Qbittorrent("192.168.0.2:8082", "PUBLIC", API_KEY_PUBLIC)
show_torrents(
private,
sort_by=args.sort,
reverse=not args.asc,
)
show_torrents(
public,
sort_by=args.sort,
reverse=not args.asc,
)
if args.force:
force_start_by_tracker(
private,
{
"myanonamouse": True,
"leo-test": True,
"nordicq": True,
},
)