feat(qbittorrent_api): add support for mult qbittorrent instances
This commit is contained in:
1 parent
99749c153c
commit
e3889d136c
1 file changed
+43
-21
@@ -1,21 +1,18 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
from datetime import timedelta
|
||||
|
||||
import argparse
|
||||
import requests
|
||||
|
||||
QBT_URL = "http://192.168.0.2:8080"
|
||||
API_KEY = "qbt_Fw6R39WuJVY7rdhv2IXIc5n8Yfhc"
|
||||
|
||||
HEADERS = {
|
||||
"Authorization": f"Bearer {API_KEY}",
|
||||
}
|
||||
API_KEY_PRIVATE = "qbt_Fw6R39WuJVY7rdhv2IXIc5n8Yfhc"
|
||||
API_KEY_PUBLIC = "qbt_dsrXBb27K4HBqbVKhKruh5GTHHLz"
|
||||
|
||||
|
||||
def get_torrents():
|
||||
def get_torrents(instance):
|
||||
HEADERS = {
|
||||
"Authorization": f"Bearer {instance.api_key}",
|
||||
}
|
||||
r = requests.get(
|
||||
f"{QBT_URL}/api/v2/torrents/info",
|
||||
f"http://{instance.ip_adress}/api/v2/torrents/info",
|
||||
headers=HEADERS,
|
||||
timeout=30,
|
||||
)
|
||||
@@ -23,9 +20,12 @@ def get_torrents():
|
||||
return r.json()
|
||||
|
||||
|
||||
def get_trackers(torrent_hash):
|
||||
def get_trackers(instance, torrent_hash):
|
||||
HEADERS = {
|
||||
"Authorization": f"Bearer {instance.api_key}",
|
||||
}
|
||||
r = requests.get(
|
||||
f"{QBT_URL}/api/v2/torrents/trackers",
|
||||
f"http://{instance.ip_address}/api/v2/torrents/trackers",
|
||||
params={"hash": torrent_hash},
|
||||
headers=HEADERS,
|
||||
timeout=30,
|
||||
@@ -34,9 +34,12 @@ def get_trackers(torrent_hash):
|
||||
return r.json()
|
||||
|
||||
|
||||
def set_force_start(torrent_hash, enabled=True):
|
||||
def set_force_start(instance, torrent_hash, enabled=True):
|
||||
HEADERS = {
|
||||
"Authorization": f"Bearer {instance.api_key}",
|
||||
}
|
||||
requests.post(
|
||||
f"{QBT_URL}/api/v2/torrents/setForceStart",
|
||||
f"http://{instance.ip_adress}/api/v2/torrents/setForceStart",
|
||||
data={
|
||||
"hashes": torrent_hash,
|
||||
"value": str(enabled).lower(),
|
||||
@@ -46,7 +49,7 @@ def set_force_start(torrent_hash, enabled=True):
|
||||
).raise_for_status()
|
||||
|
||||
|
||||
def force_start_by_tracker(trackers):
|
||||
def force_start_by_tracker(instance, trackers):
|
||||
"""
|
||||
trackers = {
|
||||
"myanonamouse": True,
|
||||
@@ -55,12 +58,12 @@ def force_start_by_tracker(trackers):
|
||||
}
|
||||
"""
|
||||
|
||||
for torrent in get_torrents():
|
||||
for torrent in get_torrents(instance):
|
||||
torrent_hash = torrent["hash"]
|
||||
name = torrent["name"]
|
||||
|
||||
tracker_urls = " ".join(
|
||||
t.get("url", "") for t in get_trackers(torrent_hash)
|
||||
t.get("url", "") for t in get_trackers(instance, torrent_hash)
|
||||
).lower()
|
||||
|
||||
for tracker, force_start in trackers.items():
|
||||
@@ -76,7 +79,7 @@ def format_duration(seconds):
|
||||
return f"{days}d ({hours:.1f}h)"
|
||||
|
||||
|
||||
def show_torrents(sort_by="name", reverse=True):
|
||||
def show_torrents(instance, sort_by="name", reverse=True):
|
||||
sort_keys = {
|
||||
"name": lambda t: t["name"].lower(),
|
||||
"ratio": lambda t: t.get("ratio", 0),
|
||||
@@ -84,7 +87,7 @@ def show_torrents(sort_by="name", reverse=True):
|
||||
}
|
||||
|
||||
torrents = sorted(
|
||||
get_torrents(),
|
||||
get_torrents(instance),
|
||||
key=sort_keys[sort_by],
|
||||
reverse=reverse,
|
||||
)
|
||||
@@ -100,11 +103,20 @@ def show_torrents(sort_by="name", reverse=True):
|
||||
|
||||
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(
|
||||
@@ -122,15 +134,25 @@ if __name__ == "__main__":
|
||||
)
|
||||
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,
|
||||
}
|
||||
},
|
||||
)
|
||||
Reference in new issue
Block a user