-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
69 lines (55 loc) · 1.96 KB
/
models.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
from qbittorrent import Client
import os
from data import update_or_get_parser_data
from utilitss import resource_path
from plyer import notification
from const import NAME, LOCALHOST
import time
class Torrent:
__slots__ = ('name', 'link', 'number', 'title')
def __init__(self, name: str, link: str) -> None:
self.name = name
self.link = link
self.number = None
self.title = None
def set_number_and_title(self, title: str) -> None:
self.number = self.name.replace(title, '').split()[1]
self.title = title
def update_db(self, author: str) -> None:
update_or_get_parser_data(author, self.name)
def __str__(self) -> str:
return self.name
def __repr__(self) -> str:
return self.__str__()
class Downloader:
def __init__(self) -> None:
self.client = Client(LOCALHOST)
self.client.login()
def download(self, torrent: Torrent) -> None:
data = update_or_get_parser_data(get=True)
kwargs = {'link': torrent.link}
if torrent.title in data['downloads']:
path = f'{data["downloads"][torrent.title]}'f'/{torrent.number}'
os.makedirs(path, exist_ok=True)
kwargs['savepath'] = path
self.client.download_from_link(**kwargs)
def send_notification(self, torrent: Torrent):
notification.notify(
app_name=NAME,
title=NAME,
message=f'Качаю {torrent.name}',
timeout=10,
app_icon=resource_path('ico.ico')
)
def remove_torrent(self, torrent: Torrent):
time.sleep(1)
flag = False
while not flag:
for i in self.client.torrents():
if torrent.title.strip('?') in i['name']:
if i['progress'] != 1:
break
else:
self.client.delete(i['hash'])
flag = True
time.sleep(3)