-
Notifications
You must be signed in to change notification settings - Fork 5
/
main_backup.py
273 lines (234 loc) · 10.1 KB
/
main_backup.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
import sys
import os
from PyQt5 import QtGui
from PyQt5 import QtCore
from PyQt5 import QtWidgets
from UI.gui import Ui_MainWindow
from GUI.BatchAddUrls import BatchAddDialogue
from GUI.LicenseDialog import LicenseDialogue
from GUI.AboutDialog import AboutDialog
# Setting custom variables
desktop_path = os.path.join(os.path.expanduser('~'), "Desktop")
try:
app_root = os.path.dirname(os.path.abspath(__file__))
except NameError: # We are the main py2exe script, not a module
import sys
app_root = os.path.dirname(os.path.abspath(sys.argv[0]))
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
QtWidgets.QMainWindow.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
path = os.path.join(app_root, 'UI', 'images', 'icon.png')
self.setWindowIcon(QtGui.QIcon(path))
self.batch_dialog = BatchAddDialogue(self)
#self.ui.saveToLineEdit.setText(desktop_path)
self.ui.BrowseConvertToLineEdit.setText(os.getcwd())
self.ui.BrowseConvertLineEdit.files = []
self.set_connections()
self.url_list = []
self.complete_url_list = {}
self.convert_list = []
self.thread_pool = {}
# self.ui.tableWidget.horizontalHeader().setResizeMode(0, QtGui.QHeaderView.Stretch)
self.rowcount = 0
self.connect_menu_action()
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:
self.download_url(str(url))
else:
return
def set_destination(self):
file_name = str(QtGui.QFileDialog.getExistingDirectory(self, "Select Directory"))
if file_name is not '':
self.ui.saveToLineEdit.setText(file_name)
def browse_convert_destination(self):
file_name = str(QtGui.QFileDialog.getExistingDirectory(self, "Select Directory"))
if file_name is not '':
self.ui.BrowseConvertToLineEdit.setText(file_name)
def convert_button(self):
preferred_format = str(self.ui.ConvertMultipleComboBox.currentText())
out_path = str(self.ui.BrowseConvertToLineEdit.text())
delete_temp = self.ui.DeleteFileCheckBox.isChecked()
if len(self.ui.BrowseConvertLineEdit.files) < 1:
QtGui.QMessageBox.information(self, "Error!","No files given!")
return
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 = [str(file_n) for file_n in list(
QtGui.QFileDialog.getOpenFileNames(self, "Select files",
filter=QtCore.QString('Videos (*.mp4 *.ogg *.webm *.flv *.mkv)')
))
]
if len(file_names) > 1:
self.ui.BrowseConvertLineEdit.setText('{} Files selected'.format(len(file_names)))
self.ui.BrowseConvertLineEdit.files=file_names
elif len(file_names) == 1:
self.ui.BrowseConvertLineEdit.setText(file_names[0])
self.ui.BrowseConvertLineEdit.files=file_names
else:
self.ui.BrowseConvertLineEdit.files=file_names
def convert_file(self,file_path, out_path, preferred_format, delete_tmp=False):
if file_path.split('.')[-1] == 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,
}
self.thread_pool['thread{}'.format(self.rowcount)] = PostProcessor(options)
self.thread_pool['thread{}'.format(self.rowcount)].statusSignal.connect(self.ui.statusbar.showMessage)
self.thread_pool['thread{}'.format(self.rowcount)].list_Signal.connect(self.add_to_table)
self.thread_pool['thread{}'.format(self.rowcount)].row_Signal.connect(self.decrease_rowcount)
self.thread_pool['thread{}'.format(self.rowcount)].start()
self.ui.tabWidget.setCurrentIndex(2)
self.convert_list.append(file_path)
self.add_to_table([
self.rowcount,
os.path.split(file_path)[-1].split('.')[0],
'',
'00:00',
'-- KiB/s',
'Converting'
])
self.rowcount += 1
else:
self.ui.statusbar.showMessage('Already Converted')
def download_url(self, url, row = None):
if row >= 0:
row = row
elif row is None:
row = self.rowcount
directory = str(self.ui.saveToLineEdit.text())
quality = False
if self.ui.ConvertCheckBox.isChecked():
quality = str(self.ui.ConvertComboBox.currentText())
print (row, self.rowcount)
options = {
'url': url,
'directory': directory,
'rowcount': row,
'proxy': '',
'convert_format': quality,
'parent':self,
}
if not self.ui.DeleteFileCheckBox.isChecked():
options['keep_file'] = True
self.thread_pool['thread{}'.format(row)] = Download(options)
self.thread_pool['thread{}'.format(row)].status_bar_signal.connect(self.ui.statusbar.showMessage)
self.thread_pool['thread{}'.format(row)].remove_url_signal.connect(self.remove_url)
self.thread_pool['thread{}'.format(row)].add_update_list_signal.connect(self.add_to_table)
self.thread_pool['thread{}'.format(row)].remove_row_signal.connect(self.decrease_rowcount)
self.thread_pool['thread{}'.format(row)].start()
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) is not 0:
if len(self.url_list) < 2:
self.ui.statusbar.showMessage('Downloading {0} file'.format(len(self.url_list)))
else:
self.ui.statusbar.showMessage('Downloading {0} files'.format(len(self.url_list)))
else:
self.ui.statusbar.showMessage("done")
def handleButton(self):
url = str(self.ui.videoUrlLineEdit.text())
if url is '':
QtGui.QMessageBox.information(self, "Error!","No url given!")
return
can_download, rowcount = self.can_download(url)
if can_download:
self.download_url(url, rowcount)
else:
QtGui.QMessageBox.information(self, "Error!","This url is already being downloaded")
if len(self.url_list) is not 0:
if len(self.url_list) < 2:
self.ui.statusbar.showMessage('Downloading {0} file'.format(len(self.url_list)))
else:
self.ui.statusbar.showMessage('Downloading {0} files'.format(len(self.url_list)))
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.iteritems():
if url == _url:
print ("url already there:")
print (row, self.rowcount)
return True, row
print ("url not already there:")
print (self.rowcount, self.rowcount)
return True, self.rowcount
else:
return False, self.rowcount
def remove_url(self,url):
try:
self.url_list.remove(url)
except:
print(url)
print(self.url_list)
return
def add_to_table(self, values):
self.ui.tableWidget.setRowCount(self.rowcount)
row = values[0]
m = 0
for key in values[1:]:
new_item = QtGui.QTableWidgetItem(key)
self.ui.tableWidget.setItem(row, m, new_item)
m += 1
def decrease_rowcount(self):
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):
license = LicenseDialogue(self)
license.show()
def showAbout(self):
about = AboutDialog(self)
about.show()
def closeEvent(self, event):
if len(self.url_list) is not 0:
msgBox = QtGui.QMessageBox(self)
msgBox.setWindowTitle("Exit")
msgBox.setText("Some files are currently being downloaded.")
msgBox.setInformativeText("Do you really want to close?")
msgBox.setStandardButtons(QtGui.QMessageBox.Close | QtGui.QMessageBox.Cancel)
msgBox.setDefaultButton(QtGui.QMessageBox.Cancel)
ret = msgBox.exec_()
if ret == QtGui.QMessageBox.Cancel:
event.ignore()
else:
self.kill_all_threads()
else:
self.kill_all_threads()
def kill_all_threads(self):
for key, value in self.thread_pool.iteritems():
if value.done is False:
del value
else:
continue
self.close()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
myapp = MainWindow()
myapp.show()
sys.exit(app.exec_())