feat(qbittorrent_api): add support for mult qbittorrent instances
This commit is contained in:
1 parent
8377e61306
commit
b00d8b8249
1 file changed
+42
-20
@@ -1,21 +1,18 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
from datetime import timedelta
|
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
QBT_URL = "http://192.168.0.2:8080"
|
API_KEY_PRIVATE = "qbt_Fw6R39WuJVY7rdhv2IXIc5n8Yfhc"
|
||||||
API_KEY = "qbt_Fw6R39WuJVY7rdhv2IXIc5n8Yfhc"
|
API_KEY_PUBLIC = "qbt_dsrXBb27K4HBqbVKhKruh5GTHHLz"
|
||||||
|
|
||||||
|
|
||||||
|
def get_torrents(instance):
|
||||||
HEADERS = {
|
HEADERS = {
|
||||||
"Authorization": f"Bearer {API_KEY}",
|
"Authorization": f"Bearer {instance.api_key}",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def get_torrents():
|
|
||||||
r = requests.get(
|
r = requests.get(
|
||||||
f"{QBT_URL}/api/v2/torrents/info",
|
f"http://{instance.ip_adress}/api/v2/torrents/info",
|
||||||
headers=HEADERS,
|
headers=HEADERS,
|
||||||
timeout=30,
|
timeout=30,
|
||||||
)
|
)
|
||||||
@@ -23,9 +20,12 @@ def get_torrents():
|
|||||||
return r.json()
|
return r.json()
|
||||||
|
|
||||||
|
|
||||||
def get_trackers(torrent_hash):
|
def get_trackers(instance, torrent_hash):
|
||||||
|
HEADERS = {
|
||||||
|
"Authorization": f"Bearer {instance.api_key}",
|
||||||
|
}
|
||||||
r = requests.get(
|
r = requests.get(
|
||||||
f"{QBT_URL}/api/v2/torrents/trackers",
|
f"http://{instance.ip_address}/api/v2/torrents/trackers",
|
||||||
params={"hash": torrent_hash},
|
params={"hash": torrent_hash},
|
||||||
headers=HEADERS,
|
headers=HEADERS,
|
||||||
timeout=30,
|
timeout=30,
|
||||||
@@ -34,9 +34,12 @@ def get_trackers(torrent_hash):
|
|||||||
return r.json()
|
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(
|
requests.post(
|
||||||
f"{QBT_URL}/api/v2/torrents/setForceStart",
|
f"http://{instance.ip_adress}/api/v2/torrents/setForceStart",
|
||||||
data={
|
data={
|
||||||
"hashes": torrent_hash,
|
"hashes": torrent_hash,
|
||||||
"value": str(enabled).lower(),
|
"value": str(enabled).lower(),
|
||||||
@@ -46,7 +49,7 @@ def set_force_start(torrent_hash, enabled=True):
|
|||||||
).raise_for_status()
|
).raise_for_status()
|
||||||
|
|
||||||
|
|
||||||
def force_start_by_tracker(trackers):
|
def force_start_by_tracker(instance, trackers):
|
||||||
"""
|
"""
|
||||||
trackers = {
|
trackers = {
|
||||||
"myanonamouse": True,
|
"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"]
|
torrent_hash = torrent["hash"]
|
||||||
name = torrent["name"]
|
name = torrent["name"]
|
||||||
|
|
||||||
tracker_urls = " ".join(
|
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()
|
).lower()
|
||||||
|
|
||||||
for tracker, force_start in trackers.items():
|
for tracker, force_start in trackers.items():
|
||||||
@@ -76,7 +79,7 @@ def format_duration(seconds):
|
|||||||
return f"{days}d ({hours:.1f}h)"
|
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 = {
|
sort_keys = {
|
||||||
"name": lambda t: t["name"].lower(),
|
"name": lambda t: t["name"].lower(),
|
||||||
"ratio": lambda t: t.get("ratio", 0),
|
"ratio": lambda t: t.get("ratio", 0),
|
||||||
@@ -84,7 +87,7 @@ def show_torrents(sort_by="name", reverse=True):
|
|||||||
}
|
}
|
||||||
|
|
||||||
torrents = sorted(
|
torrents = sorted(
|
||||||
get_torrents(),
|
get_torrents(instance),
|
||||||
key=sort_keys[sort_by],
|
key=sort_keys[sort_by],
|
||||||
reverse=reverse,
|
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)
|
name_w = max((len(name) for name, _, _ in rows), default=0)
|
||||||
ratio_w = max((len(ratio) for _, ratio, _ 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:
|
for name, ratio, seed_time in rows:
|
||||||
print(f"{name:<{name_w}} Ratio: {ratio:>{ratio_w}} Seed Time: {seed_time}")
|
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__":
|
if __name__ == "__main__":
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
@@ -122,15 +134,25 @@ if __name__ == "__main__":
|
|||||||
)
|
)
|
||||||
args = parser.parse_args()
|
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(
|
show_torrents(
|
||||||
|
private,
|
||||||
|
sort_by=args.sort,
|
||||||
|
reverse=not args.asc,
|
||||||
|
)
|
||||||
|
show_torrents(
|
||||||
|
public,
|
||||||
sort_by=args.sort,
|
sort_by=args.sort,
|
||||||
reverse=not args.asc,
|
reverse=not args.asc,
|
||||||
)
|
)
|
||||||
if args.force:
|
if args.force:
|
||||||
force_start_by_tracker(
|
force_start_by_tracker(
|
||||||
|
private,
|
||||||
{
|
{
|
||||||
"myanonamouse": True,
|
"myanonamouse": True,
|
||||||
"leo-test": True,
|
"leo-test": True,
|
||||||
"nordicq": True,
|
"nordicq": True,
|
||||||
}
|
},
|
||||||
)
|
)
|
||||||
Reference in new issue
Block a user