-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqbmain.py
353 lines (336 loc) · 11.2 KB
/
qbmain.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#!/usr/bin/python
"""
This program is a client for qbittorrent to operate headlessly. This is
is a non-official program and comes with absolute no warranty. Feel free
to use or modify. Keep in mind that the imported library is only supported
for versions above 4.1 (as the library documentation says)
Copyright (C) 2020 pri-pro
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import time
from qbittorrent import Client
from IPython.display import clear_output
qb = Client('http://127.0.0.1:6784/')
qb.login('admin', 'adminadmin')
torrents = qb.torrents()
def get_size_format(b, factor=1024, suffix="B"):
"""
Scale bytes to its proper byte format
e.g:
1253656 => '1.20MB'
1253656678 => '1.17GB'
"""
for unit in ["", "K", "M", "G", "T", "P", "E", "Z"]:
if b < factor:
return f"{b:.2f}{unit}{suffix}"
b /= factor
return f"{b:.2f}Y{suffix}"
def percent(*args):
a = float(str(args[0]))
b = a*100
result = str(b)[:5] + "%"
return result
def hash_list():
info_hash_list = []
print('enter \'q\' to exit loop')
num = 1
while True:
print(str(num) + '. infohash:', end="")
k = input()
if k == "q":
break
elif k == "all":
info_hash_list = "all"
break
elif len(k) == 40:
info_hash_list.append(k)
num +=1
else:
continue
return info_hash_list
def content_break():
print("--------------------")
def loop_break():
print("=========================================================")
print("=========================================================")
class torrent():
def __init__(self):
# print("please give any args")
print(
"""
1. Download by torrent file
2. Download by magnet
3. Status all active
4. Progress(filter)
5. Recheck (all or multiple)
6. Pause (all or multiple)
7. Resume (all or multiple)
8. Remove (all or multiple)
9. Focus (only one)
10. Focus all downloading
11. Files view
12. Max priority
"""
)
opts = int(input(">>"))
if opts == 1:
print("Downloading from file:")
location = []
path = input("Folder path:")
location.append(path)
self.down_file(location=location)
elif opts == 2:
print("Downloading from magnet:")
location = []
path = input("Folder path:")
location.append(path)
qb.download_from_link(self.link_list(), savepath=location[0])
# self.down_link(self.link_list(), location=location)
elif opts == 3:
print("Status of all actives:")
self.status()
elif opts == 4:
print(
"""progress of-
1."downloading"
2."paused"
3."active"
4."stalled"
5."seeding"
6."completed"
7."resumed"
8."errored"
9."all"
"""
)
opts = int(input(">>"))
if opts == 1:
self.progress("downloading")
elif opts == 2:
self.progress("paused")
elif opts == 3:
self.progress("active")
elif opts == 4:
self.progress("stalled")
elif opts == 5:
self.progress("seeding")
elif opts == 6:
self.progress("completed")
elif opts == 7:
self.progress("resumed")
elif opts == 8:
self.progress("errored")
elif opts == 9:
self.progress("all")
else:
print("please give a valid input")
exit()
elif opts == 5:
print("Recheck:(if all type \"all\")")
q = []
q.append(hash_list())
if q[0] == "all":
print("rechecking all!")
qb.recheck_all()
else:
qb.recheck(q[0])
elif opts == 6:
print("Pause:(if all type \"all\")")
q = []
q.append(hash_list())
if q[0] == "all":
print("pause all!")
qb.pause_all()
else:
qb.pause_multiple(q[0])
elif opts == 7:
print("Resume:(if all type \"all\")")
q = []
q.append(hash_list())
if q[0] == "all":
print("resuming all!")
qb.resume_all()
else:
qb.resume_multiple(q[0])
elif opts == 8:
print("Remove:(if all type \"all\")")
q = []
q.append(hash_list())
if q[0] == "all":
print("please don't attemp to remove all")
qb.delete_all()
else:
qb.delete(q[0])
elif opts == 9:
print("Focusing:")
thash = input("infohash:")
if len(int(thash)) == 40:
self.focus(thash)
else:
print("please try again")
exit()
elif opts == 10:
self.focus_all()
elif opts == 11:
print("Viewing file")
thash = input("infohash:")
self.file_view(thash)
elif opts == 12:
self.max_priority(hash_list())
def status(self):
for torrent in torrents:
print("Torrent name:", torrent["name"])
print("hash:", torrent["hash"])
print("Seeds:", torrent["num_seeds"])
print("File size:", get_size_format(torrent["size"]))
print("Total size:", get_size_format(torrent["total_size"]))
print("Download speed:", get_size_format(torrent["dlspeed"]) + "/s")
# print(torrent)
print("Progress", percent(torrent["progress"]))
content_break()
pass
def progress(self, *args):
"""
the filters are:
1."downloading"
2."paused"
3."active"
4."stalled"
5."seeding"
6."completed"
7."resumed"
8."errored"
9."all"
"""
torrents_filtered = qb.torrents(filter=args[0])
for torrent in torrents_filtered:
print("Torrent name:", torrent["name"])
print("Progress", percent(torrent["progress"]))
content_break()
pass
def recheck(self, *args):
"""takes infohash"""
if args[0] == "all":
print("rechecking all!")
qb.recheck_all()
else:
for thash in args[0]:
print("rechecking:", thash)
qb.recheck(thash)
pass
def down_file(self, **kwargs):
"""takes location"""
# print(kwargs['location'])
qb.download_from_file(self.file_list(), savepath=kwargs['location'])
pass
def file_list(self):
torrent_file_list = []
num = 1
print('enter \'q\' to exit loop')
while True:
print(str(num) + '. Address:', end="")
k = input()
if k == "q":
break
else:
torrent_file_list.append(open(k, 'rb'))
num +=1
return torrent_file_list
def link_list(self):
link_list = []
print('enter \'q\' to exit loop')
num = 1
while True:
print(str(num) + '. link:', end="")
k = input()
if k == "q":
break
else:
link_list.append(k)
num +=1
continue
return link_list
def remove(self, *args):
"""takes infohash"""
for thash in args[0]:
print("deleting:", thash)
qb.delete(thash)
def pause(self, *args):
"""takes infohash"""
if args[0] == "all":
print("pause all!")
qb.pause_all()
else:
for thash in args[0]:
print("pausing:", thash)
qb.pause_multiple(thash)
def resume(self, *args):
if args[0] == "all":
print("resuming all!")
qb.resume_all()
else:
for thash in args[0]:
print("resuming:", thash)
qb.resume_multiple(thash)
pass
def focus(self, thash):
"""takes infohash"""
for torrent in torrents:
if torrent["hash"] == thash:
while True:
clear_output()
print("(terminate to close)")
print("Torrent name:", torrent["name"])
print("Seeds:", torrent["num_seeds"])
print("File size:", get_size_format(torrent["size"]))
# print("Download speed:", str(torrent["dlspeed"]) + "/s")
print("Download speed:", get_size_format(torrent["dlspeed"]) + "/s")
# print(torrent)
print("Progress", percent(torrent["progress"]))
time.sleep(1)
def focus_all(self):
while True:
torrents = qb.torrents(filter="downloading", sort="dlspeed", limit=9) # limit=5
clear_output()
for torrent in torrents:
print("(terminate to close)")
print("Torrent name:", torrent["name"])
print("Seeds:", torrent["num_seeds"])
print("File size:", get_size_format(torrent["size"]))
# print("Download speed:", str(torrent["dlspeed"]) + "/s")
print("Download speed:", get_size_format(torrent["dlspeed"]) + "/s")
# print(torrent)
print("Progress", percent(torrent["progress"]))
content_break()
# loop_break()
time.sleep(1)
def file_view(self, thash):
"""takes infohash"""
file_list = qb.get_torrent_files(thash)
num = 1
for file in file_list:
print(str(num) + ".File name:", file["name"])
if int(file["priority"]) == 0:
print("Not downloading")
print("Size:", get_size_format(file["size"]))
print("Progress:", percent(file["progress"]))
num +=1
content_break()
def max_priority(self, *args):
for t in args[0]:
print("Setting max priority to:", t)
qb.set_max_priority(t)
if __name__ == "__main__":
try:
torrent()
except:
pass