-
Notifications
You must be signed in to change notification settings - Fork 51
/
main.py
313 lines (257 loc) · 10.6 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
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
# Entry point to the app
import os
import sys
from pathlib import Path
app_root = None
if not hasattr(sys, "frozen"):
# direct call of __main__.py
PATH = Path(__file__).resolve().parent
app_root = str(PATH)
sys.path.insert(0, app_root)
from PyQt5 import QtCore, QtGui, QtWidgets
import youtube_dl
from GUI.AboutDialog import AboutDialog
from GUI.BatchAddUrls import BatchAddDialogue
from GUI.LicenseDialog import LicenseDialogue
from Threads.Download import Download
from Threads.PostProcessor import PostProcessor
from UI.gui import Ui_MainWindow
# Setting custom variables
desktop_path = str(Path().home() / Path("Desktop"))
if not Path(desktop_path).exists():
os.makedirs(desktop_path, exist_ok=True)
class CloseSignals(QtCore.QObject):
"Define the signals available for close"
closed = QtCore.pyqtSignal()
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None, *args, **kwargs):
super(MainWindow, self).__init__(parent, *args, **kwargs)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
# For getting the icon to work
try:
from PyQt5.QtWinExtras import QtWin
myappid = "my_company.my_product.sub_product.version"
QtWin.setCurrentProcessExplicitAppUserModelID(myappid) # type: ignore
except ImportError:
pass
global app_root
if app_root:
ico_path = str(Path(app_root) / Path("UI/images/icon.ico"))
else:
app_root = ":/"
ico_path = app_root + "icon.ico"
self.setWindowIcon(QtGui.QIcon(ico_path))
self.batch_dialog = BatchAddDialogue(self)
self.ui.saveToLineEdit.setText(desktop_path)
self.ui.BrowseConvertToLineEdit.setText(str(Path().cwd()))
self.ui.BrowseConvertLineEdit.files = []
self.ui.statusbar.showMessage("Ready.")
self.set_connections()
self.url_list = []
self.complete_url_list = {}
self.convert_list = []
self.threadpool = QtCore.QThreadPool()
self.ui.tableWidget.horizontalHeader().setSectionResizeMode(
0, QtWidgets.QHeaderView.Stretch
)
self.rowcount = 0
self.connect_menu_action()
self.about = AboutDialog(self)
self.license = LicenseDialogue(self)
self.closing = CloseSignals()
self.show()
def set_connections(self):
self.ui.download_btn.clicked.connect(self.handleButton)
self.ui.browse_btn.clicked.connect(self.set_destination)
self.ui.BatchAdd.clicked.connect(self.batch_file)
self.ui.BrowseConvertButton.clicked.connect(self.convert_file_browse)
self.ui.ConvertMultipleButton.clicked.connect(self.convert_button)
self.ui.BrowseConvertToButton.clicked.connect(self.browse_convert_destination)
def batch_file(self):
self.batch_dialog.exec_()
if self.batch_dialog.download is True:
urls = list(self.batch_dialog.ui.UrlList.toPlainText().split("\n"))
for url in urls:
if url.strip():
self.download_url(str(url))
def set_destination(self):
file_name: str = QtWidgets.QFileDialog.getExistingDirectory(
self, "Select Directory"
)
if file_name:
self.ui.saveToLineEdit.setText(str(Path(file_name).resolve()))
def browse_convert_destination(self):
file_name = QtWidgets.QFileDialog.getExistingDirectory(self, "Select Directory")
if file_name:
self.ui.BrowseConvertToLineEdit.setText(str(Path(file_name).resolve()))
def convert_button(self):
preferred_format = self.ui.ConvertMultipleComboBox.currentText()
out_path = self.ui.BrowseConvertToLineEdit.text()
delete_temp = self.ui.DeleteFileCheckBox.isChecked()
if len(self.ui.BrowseConvertLineEdit.files) < 1:
QtWidgets.QMessageBox.information(self, "Error!", "No files given!")
else:
for file_path in self.ui.BrowseConvertLineEdit.files:
self.convert_file(file_path, out_path, preferred_format, delete_temp)
def convert_file_browse(self):
file_names, _ = QtWidgets.QFileDialog.getOpenFileNames(
self, "Select files", filter="Videos (*.mp4 *.ogg *.webm *.flv *.mkv)"
)
if len(file_names) > 1:
self.ui.BrowseConvertLineEdit.setText(f"{len(file_names)} Files selected")
elif len(file_names) == 1:
file_name = str(Path(file_names[0]).resolve())
self.ui.BrowseConvertLineEdit.setText(file_name)
self.ui.BrowseConvertLineEdit.files = file_names
def convert_file(self, file_path, out_path, preferred_format, delete_tmp=False):
if Path(file_path).suffix == preferred_format:
self.ui.statusbar.showMessage("The source and destination formats are same")
return
if file_path not in self.convert_list:
options = {
"file_path": file_path,
"preferred_format": preferred_format,
"row_count": self.rowcount,
"delete_tmp": delete_tmp,
"parent": self,
"out_path": out_path,
}
# Using ThreadPool
pprocessorThread = PostProcessor(options)
pprocessorThread.signals.statusBar_Signal.connect(self.ui.statusbar.showMessage)
pprocessorThread.signals.list_Signal.connect(self.add_to_table)
pprocessorThread.signals.row_Signal.connect(self.decrease_rowcount)
self.threadpool.start(pprocessorThread)
self.ui.tabWidget.setCurrentIndex(2)
self.convert_list.append(file_path)
self.add_to_table(
[
self.rowcount,
Path(file_path).stem,
"",
"00:00",
"-- KiB/s",
"Converting",
]
)
self.rowcount += 1
else:
self.ui.statusbar.showMessage("Already Converted")
def download_url(self, url, row=None):
if row is None:
row = self.rowcount
directory = str(Path(self.ui.saveToLineEdit.text()).resolve())
quality = False
if self.ui.ConvertCheckBox.isChecked():
quality = self.ui.ConvertComboBox.currentText()
options = {
"url": url,
"directory": directory,
"rowcount": row,
"proxy": "",
"convert_format": quality,
"parent": self,
}
if not self.ui.DeleteFileCheckBox.isChecked():
options["keep_file"] = True
# Using ThreadPool
downloadThread = Download(options)
downloadThread.signals.status_bar_signal.connect(self.ui.statusbar.showMessage)
downloadThread.signals.remove_url_signal.connect(self.remove_url)
downloadThread.signals.add_update_list_signal.connect(self.add_to_table)
downloadThread.signals.remove_row_signal.connect(self.decrease_rowcount)
self.closing.closed.connect(downloadThread.stop)
self.threadpool.start(downloadThread)
self.ui.tabWidget.setCurrentIndex(2)
self.ui.statusbar.showMessage("Extracting information...")
self.url_list.append(url)
self.complete_url_list[row] = url
self.rowcount += 1
if len(self.url_list):
if len(self.url_list) == 1:
self.ui.statusbar.showMessage("Downloading 1 file")
else:
self.ui.statusbar.showMessage(f"Downloading {len(self.url_list)} files")
else:
self.ui.statusbar.showMessage("Done!")
def handleButton(self):
url = self.ui.videoUrlLineEdit.text()
if not url:
QtWidgets.QMessageBox.information(self, "Error!", "No url given!")
return
can_download, rowcount = self.can_download(url)
if can_download:
self.download_url(url, rowcount)
else:
QtWidgets.QMessageBox.information(
self, "Error!", "This url is already being downloaded"
)
if len(self.url_list):
if len(self.url_list) == 1:
self.ui.statusbar.showMessage("Downloading 1 file")
else:
self.ui.statusbar.showMessage(
f"Downloading {len(self.url_list)} files"
)
else:
self.ui.statusbar.showMessage("Done!")
def can_download(self, url):
if url not in self.url_list:
for row, _url in self.complete_url_list.items():
if url == _url:
return True, row
return True, self.rowcount
else:
return False, self.rowcount
def remove_url(self, url):
# TODO: Need synchronized List/Queue
try:
self.url_list.remove(url)
except:
pass
def add_to_table(self, values):
self.ui.tableWidget.setRowCount(self.rowcount)
row = values[0]
column = 0
for key in values[1:]:
new_item = QtWidgets.QTableWidgetItem(key)
self.ui.tableWidget.setItem(row, column, new_item)
column += 1
def decrease_rowcount(self):
# TODO: Check decrease >= 0
self.rowcount -= 1
def connect_menu_action(self):
self.ui.actionExit.triggered.connect(self.close)
self.ui.actionLicense.triggered.connect(self.showLicense)
self.ui.actionAbout.triggered.connect(self.showAbout)
def showLicense(self):
self.license.show()
def showAbout(self):
self.about.show()
def closeEvent(self, event):
if len(self.url_list):
msgBox = QtWidgets.QMessageBox(self)
msgBox.setWindowTitle("Exit")
msgBox.setText("Some files are currently being downloaded.")
msgBox.setInformativeText("Do you really want to close?")
msgBox.setStandardButtons(QtWidgets.QMessageBox.Close | QtWidgets.QMessageBox.Cancel)
msgBox.setDefaultButton(QtWidgets.QMessageBox.Cancel)
ret = msgBox.exec_()
if ret == QtWidgets.QMessageBox.Cancel:
event.ignore()
else:
self.kill_all_threads()
else:
self.kill_all_threads()
def kill_all_threads(self):
self.closing.closed.emit()
self.threadpool.waitForDone()
self.close()
def main():
app = QtWidgets.QApplication(sys.argv)
myapp = MainWindow()
myapp.show()
sys.exit(app.exec_())
if __name__ == "__main__":
sys.exit(main())