-
Notifications
You must be signed in to change notification settings - Fork 0
/
qbwrapper.py
executable file
·67 lines (58 loc) · 2.12 KB
/
qbwrapper.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
import qbittorrent
import json
from utils import ROOT_DIR
import os
CONFIG_PATH = os.path.join(ROOT_DIR, 'qb_pref.json')
class QbWrapper(qbittorrent.Client):
def __init__(self):
super().__init__(url="http://127.0.0.1:8080/")
# = Client("http://127.0.0.1:8080/")
self.login("admin", "adminadmin")
self.torrent_index = ""
self.active_torrent = ""
self.progress = ""
with open(CONFIG_PATH, 'r') as preferences:
self.set_preferences(**json.load(preferences))
def download(self, magnet):
self.download_from_link(magnet)
hash = self.get_hash(magnet)
# self.toggle_sequential_download([hash])
torrents = self.torrents()
active_torrent_index=""
for i, torrent in enumerate(self.torrents()):
if self.get_hash(torrent["magnet_uri"]) == self.get_hash(magnet):
self.torrent_index=i
# self.toggle_sequential_download(self.torrents()[i]["infohash_v1"])
self.toggle_first_last_piece_priority(self.torrents()[i]["infohash_v1"])
break
def stop(self):
print("stopping torrent")
self.pause_all()
def is_complete(self):
return (
self.torrents()[self.torrent_index]["completed"]
/ self.torrents()[self.torrent_index]["total_size"]
) == 1
def get_raw_progress(self):
return self.progress
def show_progress(self):
self.active_torrent = self.torrents()[int(self.torrent_index)]
progress = (
self.active_torrent["completed"] / self.active_torrent["total_size"]
) * 100
bar = list("[")
for i in range(1, 101):
if i <= progress:
bar.append("#")
else:
bar.append("_")
bar.append("]")
bar[round(progress)] = "#"
print(
"Download {0} is at {1:.0f}% \n {2}".format(
self.active_torrent["name"], progress, "".join(bar)
),
)
self.progress = progress
def get_hash(self, magnet):
return magnet.split("&")[0].upper()