-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
267 lines (203 loc) · 7.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
# coding: utf-8
import fnmatch
import os
import shutil
import subprocess
import sys
from PyQt5 import QtCore
from PyQt5.QtWidgets import *
from main_ui import Ui_Dialog
from about_ui import Ui_About
# added find-replace history:
settings = QtCore.QSettings('m3u8-converter-settings')
print('Settings path:')
print(settings.fileName())
def countFiles(directory, filePattern):
for path, dirs, files in os.walk(os.path.abspath(directory)):
count = 0
for filename in fnmatch.filter(files, filePattern):
print(filename)
count += 1
print('Found: '+str(count)+' m3us')
return count
def create_folder(directory):
path = directory + "/Converted"
print(path)
path = path.replace(' ', '\\ ')
command = 'mkdir ' + path
subprocess.call(command, shell=True)
def convert_utf8(directory):
path_converted = directory + "/Converted"
path_converted = path_converted.replace(' ', '\\ ')
command = 'find . -name "*.m3u8" -exec bash -c \'iconv -f utf-8-mac -t utf-8 -c "{}" > ' + path_converted + '/''\"{}\"\' \\;'
subprocess.call(command, shell=True, cwd=directory)
def findReplace(directory, find, replace, filePattern):
for path, dirs, files in os.walk(os.path.abspath(directory)):
print(path)
print(files)
for filename in fnmatch.filter(files, filePattern):
filepath = os.path.join(path, filename)
print(filepath)
with open(filepath, 'r', encoding='utf-8') as f:
s = f.read()
f.close()
s = s.replace(find, replace)
with open(filepath, 'w', encoding='utf-8') as f:
f.write(s)
f.close()
def copyFiles(source, destination):
src_dir = source
dest_dir = destination
shutil.copytree(src_dir, dest_dir)
def convert(directory, find, replace, utf8_check, find_replace_check):
if utf8_check is True and find_replace_check is True:
create_folder(directory)
convert_utf8(directory)
findReplace(directory + "/Converted", find, replace, "*.m3u8")
if utf8_check is True and find_replace_check is False:
create_folder(directory)
convert_utf8(directory)
if find_replace_check is True and utf8_check is False:
copyFiles(directory, directory + "/Converted")
findReplace(directory + "/Converted", find, replace, "*.m3u8")
def check_appearance():
"""Checks DARK/LIGHT mode of macos."""
cmd = 'defaults read -g AppleInterfaceStyle'
p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True)
return bool(p.communicate()[0])
class About(QDialog):
def __init__(self):
super(About, self).__init__()
self.ui = Ui_About()
self.ui.setupUi(self)
self.center()
def center(self):
qr = self.frameGeometry()
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
qr.moveTop(300)
self.move(qr.topLeft())
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
button_action = QAction("M3U8 Converter", self)
button_action.triggered.connect(self.onMyToolBarButtonClick)
self.dialog = About()
menu = self.menuBar()
about_menu = menu.addMenu("About")
about_menu.addAction(button_action)
self.form_widget = Dialog()
self.setCentralWidget(self.form_widget)
Dialog.setMinimumSize(self, 526, 216)
Dialog.setMaximumSize(self, 526, 216)
self.center()
self.setWindowTitle('M3U8 Converter')
self.show()
def center(self):
qr = self.frameGeometry()
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
qr.moveTop(300)
self.move(qr.topLeft())
def onMyToolBarButtonClick(self):
self.dialog.show()
class Dialog(QDialog):
convert_ok = False
def __init__(self):
super(Dialog, self).__init__()
self.ui = Ui_Dialog()
self.ui.setupUi(self)
self.setAcceptDrops(True)
self.ui.label_count.setText('') # clear text label
# added find-replace history:
find_start = settings.value("find")
replace_start = settings.value("replace")
self.ui.lineEdit_find.setText(find_start)
self.ui.lineEdit_replace.setText(replace_start)
def dragEnterEvent(self, event):
if event.mimeData().hasUrls():
event.accept()
else:
event.ignore()
def dropEvent(self, event):
files = [u.toLocalFile() for u in event.mimeData().urls()]
for f in files:
print(f)
result = f.endswith('/')
if result is True:
f = f[:-1]
print(f)
count = countFiles(f, "*.m3u8")
if count != 0:
count = 'Found: '+str(count)+' m3u8 '
self.convert_ok = True
else:
count = 'No m3u8 '
self.convert_ok = False
try:
dark_mode = check_appearance()
if dark_mode is True:
count = "<html><head/><body><p><span style=\" color:#37e735;\">" + count + "</span></p></body></html>"
finally:
self.ui.label_count.setText(count)
self.ui.lineEdit_folder.setText(f)
def select_button_clicked(self):
dirName = QFileDialog.getExistingDirectory(self, "Select Directory")
if dirName:
print(dirName)
self.ui.lineEdit_folder.setText(dirName)
count = countFiles(dirName, "*.m3u8")
if count != 0:
count = 'Found: '+str(count)+' m3u8 '
self.convert_ok = True
else:
count = 'No m3u8 '
self.convert_ok = False
try:
dark_mode = check_appearance()
if dark_mode is True:
count = "<html><head/><body><p><span style=\" color:#37e735;\">" + count + "</span></p></body></html>"
finally:
self.ui.label_count.setText(count)
def quit_button_clicked(self):
print('exit')
sys.exit(app.exec_())
def convert_button_clicked(self):
directory = self.ui.lineEdit_folder.text()
print(directory)
find = self.ui.lineEdit_find.text()
print(find)
settings.setValue('find', find) # added find-replace history
replace = self.ui.lineEdit_replace.text()
print(replace)
settings.setValue('replace', replace) # added find-replace history
utf8_check = self.ui.checkBox_utf8.isChecked()
print(utf8_check)
find_replace_check = self.ui.checkBox_find_replace.isChecked()
print(find_replace_check)
if self.convert_ok is True:
convert(directory, find, replace, utf8_check, find_replace_check)
def utf8_checkbox(self):
if self.ui.checkBox_utf8.isChecked():
print('checked')
else:
print('unchecked')
def find_replace_checkbox(self):
if self.ui.checkBox_find_replace.isChecked():
print('checked')
else:
print('unchecked')
def line_folder(self):
options = QFileDialog.Options()
def line_find(self):
options = QFileDialog.Options()
def line_replace(self):
options = QFileDialog.Options()
def label_count(self):
options = QFileDialog.Options()
if __name__ == '__main__':
app = QApplication([])
main = MainWindow()
main.show()
sys.exit(app.exec_())