-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
260 lines (212 loc) · 8.04 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
from PyQt5 import QtWidgets, uic, QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import sys
import qdarkstyle
import os
from concurrent.futures import ThreadPoolExecutor
import time
import appdirs
from subprocess import Popen, PIPE
from pathlib import Path
tabnum = 1
def exists(var):
return var in globals()
# Allows live logging of shell commands
def run(command):
process = Popen(command, stdout=PIPE, shell=True)
while True:
line = process.stdout.readline().rstrip()
if not line:
break
yield line
finished = 0
# Backup channel with yt-dlp
def backup(channel, currentindex):
global finished
global channels
quality = str(window.qualityBox.currentText())
window._worker.data.emit([f"Job {str(currentindex)} -> Started archiving: {channel}\n"])
for path in run(f"cd {foldpath} && yt-dlp -f 'bv*[height<={quality[:-1]}]+ba' {channel} -o '%(channel)s/%(title)s.%(ext)s'"):
resp = path.decode()+"\n"
if resp[:5] == "ERROR":
window._worker.data.emit(["", "", "", resp])
else:
window._worker.data.emit(["", [currentindex, resp]])
finished += 1
window._worker.data.emit([f"Job {str(currentindex)} -> Finished archiving: {channel}\n{finished}/{len(channels)} archived\n"])
class Archive(QtCore.QThread):
global window
data = QtCore.pyqtSignal(list)
def __init__(self, parent=None):
super(Archive, self).__init__(parent)
self._stopped = True
self._mutex = QtCore.QMutex()
def stop(self):
self._mutex.lock()
self._stopped = True
self._mutex.unlock()
def run(self):
global channels
global finished
global foldpath
# Update archive txt
Path(foldpath).mkdir(parents = True, exist_ok = True)
with open(foldpath+"/archive.txt", 'w+') as f:
f.write(window.urlBox.toPlainText())
channels = window.urlBox.toPlainText().split("\n")
# Archive all channels
with ThreadPoolExecutor(max_workers=100) as executor:
for i in range(len(channels)):
if i > 0:
self.data.emit(['', '', True])
executor.submit(backup, channels[i], i)
while finished < len(channels):
time.sleep(1)
finished = 0
tabnum = 1
self.data.emit(['Done'])
class ErrorWindow(QDialog):
def __init__(self, parent):
super(ErrorWindow, self).__init__(parent)
uic.loadUi('Assets/error.ui', self)
class ArchiveWindow(QDialog):
def __init__(self, parent):
super(ArchiveWindow, self).__init__(parent)
uic.loadUi('Assets/archive.ui', self)
class TabInit(QtWidgets.QWidget):
def __init__(self, parent=None):
global tabnum
global window
super(TabInit, self).__init__(parent)
name = "t"+str(tabnum)
lay = QtWidgets.QVBoxLayout(self)
wid = QPlainTextEdit(objectName = name)
wid.setReadOnly(True)
setattr(window.transferw, name, wid)
wid = getattr(window.transferw, name)
lay.addWidget(wid)
class Ui(QtWidgets.QMainWindow):
def __init__(self):
super(Ui, self).__init__()
# Load UI and Fonts
self.fontDB = QtGui.QFontDatabase()
self.fontDB.addApplicationFont("Assets/font.ttf")
uic.loadUi('Assets/main.ui', self)
self.qualityBox.addItems(["1080p","720p","480p","360p","240p","144p"])
self.get_path()
self.get_data()
# Reveal window
self.show()
# Load threads
self._worker = Archive()
self._worker.started.connect(self.worker_started_callback)
self._worker.finished.connect(self.worker_finished_callback)
self._worker.data.connect(self.worker_data_callback)
# Load UI
self.startButton.clicked.connect(self.start_transfer)
self.folderButton.clicked.connect(self.choose_folder)
self.folderBox.textChanged.connect(self.get_data)
def worker_data_callback(self, data):
global tabnum
# Update console
self.transferw.console.setPlainText(self.transferw.console.toPlainText()+data[0])
try:
tabname = "tab"+str(data[1][0])
textname = "t"+str(data[1][0])
textobject = getattr(self.transferw, textname)
textobject.setPlainText(textobject.toPlainText()+data[1][1])
if (self.transferw.autoBox.isChecked()):
textobject.moveCursor(QtGui.QTextCursor.End)
except Exception as e:
pass
try:
data[2]
makeNewTab = True
except Exception as e:
makeNewTab = False
pass
if makeNewTab == True:
name = "tab"+str(tabnum)
text = "t"+str(tabnum)
tabobject = TabInit()
setattr(self.transferw, name, tabobject)
tabobject = getattr(self.transferw, name)
self.transferw.jobs.addTab(tabobject, name)
self.transferw.jobs.setTabText(tabnum, "Job "+str(tabnum))
tabnum += 1
try:
data[3]
error = True
except Exception as e:
error = False
pass
if error == True:
self.transferw.errorsbox.setPlainText(self.transferw.errorsbox.toPlainText()+f"Job -> {tabnum}"+data[3])
# Autoscroll
if (self.transferw.autoBox.isChecked()):
self.transferw.console.moveCursor(QtGui.QTextCursor.End)
def worker_started_callback(self):
# Disable UI
self.startButton.setEnabled(False)
self.urlBox.setEnabled(False)
self.folderBox.setEnabled(False)
self.qualityBox.setEnabled(False)
self.folderButton.setEnabled(False)
self.startButton.setText("Running...")
def worker_finished_callback(self):
# Re Enable UI
self.startButton.setEnabled(True)
self.urlBox.setEnabled(True)
self.folderBox.setEnabled(True)
self.qualityBox.setEnabled(True)
self.folderButton.setEnabled(True)
self.startButton.setText("Update archive!")
self.transferw.label.setText("Done")
self.transferw.setWindowTitle("Completed archival")
def start_transfer(self):
# Save folder to be default next time
if (self.urlBox.toPlainText() != '' and self.folderBox.text() != ''):
dirpath = appdirs.user_config_dir("DownTube", "Bigweld")
Path(dirpath).mkdir(parents = True, exist_ok = True)
path = dirpath+"/path.txt"
with open(path, 'w+') as f:
f.write(self.folderBox.text())
# Start thread
self._worker.start()
self.transferw = ArchiveWindow(self)
self.transferw.show()
else:
# Not enough info
self.errorw = ErrorWindow(self)
self.errorw.errorText.setText("Please choose an archive folder and at least one channel")
self.errorw.show()
def get_data(self):
global foldpath
# Attempt to get archive data from previous archive
try:
foldpath = self.folderBox.text()
text_file = open(foldpath+"/archive.txt", "r")
self.urlBox.setPlainText(text_file.read())
text_file.close()
except:
pass
def get_path(self):
# Attempt to get last path used
try:
dirpath = appdirs.user_config_dir("DownTube", "Bigweld")
Path(dirpath).mkdir(parents = True, exist_ok = True)
text_file = open(dirpath+"/path.txt", "r")
self.folderBox.setText(text_file.read())
text_file.close()
except:
pass
def choose_folder(self):
# Pick folder
name = QtWidgets.QFileDialog.getExistingDirectory(None, 'Select a folder:')
self.folderBox.setText(name)
app = QtWidgets.QApplication(sys.argv)
window = Ui()
app.setStyleSheet(qdarkstyle.load_stylesheet())
app.exec_()