forked from andystewart999/qBittorrent_MkII
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
142 lines (119 loc) · 5.08 KB
/
helpers.py
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
"""Helper functions for qBittorrent."""
from qbittorrent.client import Client
from .const import *
all_torrents_prev = []
def setup_client(url: str, username: str, password: str, verify_ssl: bool) -> Client:
"""Create a qBittorrent client."""
client = Client(url, verify=verify_ssl, timeout=10)
client.login(username, password)
# Get an arbitrary attribute to test if connection succeeds
client.qbittorrent_version
return client
def find_torrent(torrent_list, default=None):
for x in torrent_list:
return x
return default
def get_detail(torrent, attribute):
value = ''
try:
value = torrent[attribute]
except Exception as err:
pass
return value
def get_version(client: Client):
return client.qbittorrent_version
def compare_torrents(client: Client):
#Return a list containing all changed torrents and their new state
global all_torrents_prev
completed_torrents= []
added_torrents= []
removed_torrents = []
#Retrieve current torrent info
all_torrents = client.torrents()
#Cater for a fresh startup - we don't want to raise a ton of events straight away
if all_torrents_prev:
for torrent in all_torrents_prev:
#See if each torrent is still in the new list
#If not, it's been deleted
#If so, check the completion_on value - has it finished?
prev_name = torrent['name']
prev_hash = torrent['hash']
prev_completion_on = torrent['completion_on']
found_torrent = find_torrent(x for x in all_torrents if x['hash'] == prev_hash)
if found_torrent is not None:
if found_torrent['completion_on'] != prev_completion_on:
#See if it's just completed downloading
if found_torrent['completion_on'] > 0:
#It's just finished downloading
#Get the torrent_specific details also - different information is returned in client.get_torrent() compared to client.torrents()
found_torrent_detail = client.get_torrent(found_torrent['hash'])
completed_torrents.append (found_torrent | found_torrent_detail)
else:
removed_torrents.append(torrent)
for torrent in all_torrents:
#See if any new torrents have been added - it won't be in the previous list
name = torrent['name']
hash = torrent['hash']
new_torrent = find_torrent(x for x in all_torrents_prev if x['hash'] == hash)
if new_torrent is None:
#Get the torrent_specific details also - different information is returned in client.get_torrent() compared to client.torrents()
added_torrent_detail = client.get_torrent(torrent['hash'])
added_torrents.append(torrent | added_torrent_detail)
all_torrents_prev = all_torrents
return completed_torrents, added_torrents, removed_torrents
def pause_downloads(client: Client, hash: str):
if hash != '' and hash != 'all':
try:
client.pause(hash)
except Exception as err:
pass
else:
client.pause_all()
return
def resume_downloads(client: Client, hash:str):
if hash != '' and hash != 'all':
try:
client.resume(hash)
except Exception as err:
pass
else:
client.resume_all()
return
def get_torrent_info(client: Client, hash: str):
if hash != '' and hash != 'all':
try:
torrent = client.get_torrent(hash)
#LOGGER.error(torrent)
return torrent
except Exception as err:
return {}
else:
torrents = client.torrents()
#LOGGER.error(torrents)
#For some reason, client.get_torrent() and client.torrents() have some differing key names! I've decided to stay consistent with the get_torrent naming
#Some of the values are still different, for example completion_date/completion_on for incomplete torrents are -36000 and -1
return {
"torrents": [
{
"hash": get_detail(torrent, "hash"),
"name": get_detail(torrent, "name"),
"addition_date": get_detail(torrent, "added_on"),
"completion_date": get_detail(torrent, "completion_on"),
"eta": get_detail(torrent, "eta"),
"save_path": get_detail(torrent, "save_path"),
"seeds": get_detail(torrent, "num_seeds"),
"state": get_detail(torrent, "state"),
"category": get_detail(torrent, "category"),
"tags": get_detail(torrent, "tags"),
"total_downloaded": get_detail(torrent, "completed"),
"total_size": get_detail(torrent, "size")
} for torrent in torrents
],
}
def shutdown(client: Client):
try:
client.shutdown()
except Exception as err:
LOGGER.error(err)
pass
return