-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqb_set_categories.py
More file actions
executable file
·90 lines (67 loc) · 2.15 KB
/
Copy pathqb_set_categories.py
File metadata and controls
executable file
·90 lines (67 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python3
"""
Automatically set the categories for torrents with known file extensions.
"""
import requests
import time
QBT_HOST = "http://127.0.0.1:8990"
USERNAME = "admin"
TARGET_CATEGORY = "musique"
session = requests.Session()
# Ordered dictionary.
EXT_TO_CATEGORY = {"musique": {"mp3", "flac", "wav"}, "nas": {"mp4", "mkv"}}
def login():
url = f"{QBT_HOST}/api/v2/auth/login"
resp = session.post(
url,
data={"username": USERNAME},
)
resp.raise_for_status()
def get_torrents():
url = f"{QBT_HOST}/api/v2/torrents/info"
resp = session.get(url)
resp.raise_for_status()
return resp.json()
def get_categories():
url = f"{QBT_HOST}/api/v2/torrents/categories"
resp = session.get(url)
resp.raise_for_status()
return resp.json()
def get_files(torrent_hash):
url = f"{QBT_HOST}/api/v2/torrents/files?hash={torrent_hash}"
resp = session.get(url)
resp.raise_for_status()
return resp.json()
def set_category(torrent_hash, category):
url = f"{QBT_HOST}/api/v2/torrents/setCategory"
resp = session.post(url, data={"hashes": torrent_hash, "category": category})
resp.raise_for_status()
return resp.text == "Ok."
def main():
try:
login()
except Exception:
return
categories = get_categories()
for category in EXT_TO_CATEGORY.keys():
if category not in categories:
print(f"Category '{category}' does not exist. Exiting.")
return
torrents = get_torrents()
seven_days_ago = time.time() - 7 * 24 * 3600
for t in torrents:
added_on = t.get("added_on")
if not added_on or added_on < seven_days_ago:
continue
# Only process torrents without a category
if t.get("category"):
continue
files = get_files(t["hash"])
extensions = {f["name"].lower().split(".")[-1] for f in files}
for cat, exts in EXT_TO_CATEGORY.items():
if extensions & exts:
print(f"Setting category to '{cat}' for torrent '{t['name']}'")
set_category(t["hash"], cat)
break
if __name__ == "__main__":
main()