-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
executable file
·225 lines (176 loc) · 7.7 KB
/
main.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
import logging
from typing import List
import asyncio
import yaml
import qdarktheme
from PyQt5.QtWidgets import QApplication, QMessageBox, QWidget, QLineEdit, QHeaderView
from PyQt5.QtCore import QModelIndex, QThread
from WebAPI import Nyaa
from TorrentHandler import TorrentHandler, Torrent
from UI import MainWindow
from Models import SearchTableModel, EpisodeTableModel, FilterProxyModel
from Workers import DownloadWorker
class Config:
def __init__(self):
self.__load_config()
def __load_config(self):
with open("config.yaml") as file:
self.__config = yaml.safe_load(file)
def __save_config(self):
with open("config.yaml", "w") as file:
yaml.dump(self.__config, file)
def __getitem__(self, key: str):
return self.__config[key]
def __setitem__(self, key: str, value):
self.__config[key] = value
self.__save_config()
@property
def config(self):
return self.__config
class App(MainWindow):
def __init__(self, config):
super().__init__(config)
self.config = config
self.nyaa = Nyaa()
self.handler = TorrentHandler(self.config.config)
#! save .torrents.pkl file if closed during download, holding the torrent objects
self.download_worker = DownloadWorker(self.handler)
self.download_worker.update_signal.connect(
self.downloads_widget.update_progress
)
self.download_worker.finished_signal.connect(
lambda ts: self.downloads_completed(ts)
)
self.download_worker.start()
# configure connections
self.button1.clicked.connect(lambda: self.stacked_widget.setCurrentIndex(0))
self.button2.clicked.connect(lambda: self.stacked_widget.setCurrentIndex(1))
self.button3.clicked.connect(lambda: self.stacked_widget.setCurrentIndex(2))
self.button4.clicked.connect(lambda: self.stacked_widget.setCurrentIndex(3))
self.search_widget.search_bar.returnPressed.connect(
lambda: self.search(self.search_widget.search_bar.text())
)
self.search_widget.search_button.clicked.connect(
lambda: self.search(self.search_widget.search_bar.text())
)
self.search_widget.table_view.clicked.connect(self.update_results_table)
self.results_widget.button_filter.clicked.connect(
lambda: self.filter_results(False)
)
self.results_widget.button_reset.clicked.connect(
lambda: self.filter_results(True)
)
self.results_widget.button_download.clicked.connect(self.download)
self.settings_window.save_setts_button.clicked.connect(self.save_settings)
self.settings_window.cancel_setts_button.clicked.connect(self.cancel_settings)
def search(self, term: str):
fansub = "subsplease"
language = "english"
loop = asyncio.get_event_loop()
torrents = loop.run_until_complete(self.nyaa.search(term, fansub, language))
if not torrents:
self.info_box("No torrents found", f"No torrents found for '{term}'.")
return
series = list(
set([torrent.series for torrent in torrents if torrent is not None])
)
self.update_search_table(series, fansub, language)
def update_search_table(self, results: List[str], fansub: str, language: str):
self.model_search = SearchTableModel(results, fansub, language)
self.search_widget.table_view.setModel(self.model_search)
self.search_widget.table_view.horizontalHeader().setSectionResizeMode(
0, QHeaderView.Stretch
)
def update_results_table(self, selected: QModelIndex):
self.stacked_widget.setCurrentIndex(1)
series = self.model_search.return_selected(selected)
loop = asyncio.get_event_loop()
torrents = loop.run_until_complete(
self.nyaa.search(series, "subsplease", "english")
)
torrents = [torrent for torrent in torrents if torrent is not None]
if not torrents:
self.info_box("No torrents found", f"No torrents found for this series.")
return
self.episodes_model = EpisodeTableModel(torrents)
self.episodes_proxymodel = FilterProxyModel()
self.episodes_proxymodel.setSourceModel(self.episodes_model)
self.results_widget.table_view.setModel(self.episodes_proxymodel)
self.results_widget.table_view.horizontalHeader().setSectionResizeMode(
0, QHeaderView.Stretch
)
def filter_results(self, reset: bool):
self.episodes_proxymodel.clearFilters()
self.episodes_model.refresh()
if reset:
self.results_widget.combo_box.setCurrentIndex(0)
self.results_widget.checkBox_batch.setChecked(False)
return
quality = self.results_widget.combo_box.currentText()
filter_quality = quality if quality != "Select" else ""
filter_batch = "true" if self.results_widget.checkBox_batch.isChecked() else ""
if filter_quality:
self.episodes_proxymodel.setFilterByColumn(2, filter_quality)
if filter_batch:
self.episodes_proxymodel.setFilterByColumn(3, filter_batch)
def download(self, _: QModelIndex):
self.stacked_widget.setCurrentIndex(2)
torrents: list[Torrent] = self.episodes_model.return_selected()
for torrent in torrents:
self.handler.add_torrent(torrent)
def downloads_completed(self, torrents: List[Torrent]):
if not torrents:
return
for torrent in torrents:
torrent.process()
self.info_box(
"Downloads Completed",
"\n".join(
[
f"Downloaded \n{torrent.series} - {torrent.episode}"
for torrent in torrents
]
),
)
def save_settings(self):
dialog = QMessageBox(self)
dialog.setWindowTitle("Confirm Action")
dialog.setText("Are you sure you want to save?")
dialog.setIcon(QMessageBox.Question)
dialog.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
dialog.setDefaultButton(QMessageBox.No)
if dialog.exec_() == QMessageBox.Yes:
# iterate through all widgets in settings window
for widget in self.settings_window.findChildren(QWidget):
# if widget is a line edit
if isinstance(widget, QLineEdit):
key = widget.objectName().replace("line_", "")
value = widget.text()
self.settings[key] = value
return
# reset all line edits
self.cancel_settings()
def cancel_settings(self):
# reset all line edits
for widget in self.settings_window.findChildren(QWidget):
if isinstance(widget, QLineEdit):
key = widget.objectName().replace("line_", "")
value = self.settings[key]
widget.setText(value)
def info_box(self, title, message):
qm = QMessageBox()
qm.information(self, title, message)
def onClose(self):
# self.handler.save_torrents()
self.download_worker.stop()
super().close()
if __name__ == "__main__":
config = Config()
with open("UI/qss.style") as file:
qss = file.read()
qdarktheme.enable_hi_dpi()
app = QApplication([])
qdarktheme.setup_theme("light", additional_qss=qss)
window = App(config)
window.show()
app.exec_()