-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenList.py
362 lines (287 loc) · 10 KB
/
openList.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
"""openList
This program downloads an entire playlist from YouTube
and converts it to the more portable MP3 format.
Autor: Jefferson Lopes
Version: v1.0
Email: [email protected]
"""
import moviepy_build_fix # needed to build on windows
import customtkinter as ctk
from pytube import YouTube
from pytube import Playlist
import moviepy.editor as mp
from threading import Thread
from time import sleep
import os
import re
# set style
ctk.set_appearance_mode('dark')
ctk.set_default_color_theme('dark-blue')
class App(ctk.CTk):
def __init__(self : ctk.CTk):
"""config app window"""
super().__init__()
# create variables
self.playlist = None
self.path = None
self.link = ''
self.size = None
self.id = ''
# setting window dimensions
self.geometry("700x540")
# setting app title
self.title("openList")
# do not resize
self.resizable(False, False)
# define fonts
self.title_font = ctk.CTkFont(family='Alphamalemodern', size=56)
self.label_font = ctk.CTkFont(family='Brion Light', size=16, weight='bold')
self.text_font = ctk.CTkFont(family='Brion Light', size=12, weight='bold')
# create widgets
self.__create_widgets()
def __create_widgets(self):
"""create widgets on the frame
:return: if successful, true; otherwise, false
"""
# base values
PADX = 30
PADY = 20
RADIUS = 7
# main frame
self.frame = ctk.CTkFrame(self)
self.frame.pack(pady=20, padx=50, expand=True)
# label - title
self.title_label = ctk.CTkLabel(
self.frame,
text='openList',
font=self.title_font
)
self.title_label.grid(column=0, row=0, columnspan=2, pady=2*PADY, padx=PADX)
# entry - add playlist link
self.link_entry = ctk.CTkEntry(
self.frame,
placeholder_text='playlist link',
justify='center',
height=30,
width=300,
corner_radius=RADIUS,
font=self.label_font
)
self.link_entry.grid(column=0, row=1, pady=PADY/2, padx=PADX)
# button - choose directory
self.get_path_button = ctk.CTkButton(
self.frame,
text='choose download folder',
command=self.get_path_callback,
height=30,
width=220,
corner_radius=RADIUS,
state=ctk.NORMAL,
font=self.label_font
)
self.get_path_button.grid(column=0, row=2, pady=PADY/2, padx=PADX)
# button - start download
self.start_button = ctk.CTkButton(
self.frame,
text='start download',
command=self.start_callback,
height=60,
width=160,
corner_radius=2*RADIUS,
state=ctk.NORMAL,
font=self.label_font
)
self.start_button.grid(column=1, row=1, rowspan=2, pady=PADY, padx=PADX)
# text box - progress messages
self.progress_text = ctk.CTkTextbox(
self.frame,
height=140,
width=400,
corner_radius=2*RADIUS,
font=self.text_font
)
self.progress_text.grid(column=0, row=3, columnspan=2, sticky='wens', pady=PADY, padx=PADX)
self.progress_bar = ctk.CTkProgressBar(
self.frame,
height=7,
width=300,
border_width=0,
corner_radius=RADIUS,
orientation='horizontal',
mode='indeterminate',
indeterminate_speed=1
)
self.progress_bar.grid(column=0, row=4, columnspan=2, pady=PADY, padx=PADX)
self.progress_bar.start()
return True
def get_path_callback(self):
"""button get path callback
:return: path string
"""
# get path
path = ctk.filedialog.askdirectory()
if len(path) != 0:
# convert string
self.path = path.replace('C:', '')
# update progress text box
self.print(F'Download path : {self.path}\n')
return True
return False
def start_callback(self):
"""button start download callback, check
for input errors then starts the download
:return: if successful, true; otherwise, false
"""
# get link string from entry widget
link = self.link_entry.get()
# check for empty link
if link == '':
self.print(F'ERROR: empty link\n')
return False
else:
self.link = link
# check for empty path
if self.path is None or self.path == "":
self.print(F'ERROR: empty path\n')
return False
else:
# check for valid link id
if self.get_id():
# create download separate thread
t1 = Thread(target=self.start)
t1.start()
else:
self.print(F'ERROR: playlist id not found\n')
return True
def get_id(self):
# find playlist ID inside the link
for peace in re.split('[&?]', self.link):
if 'list=' in peace:
self.id = peace.split('=')[1] # remove 'list=' from string
return True
else:
self.id = ''
return False
def get_size(self):
if self.playlist is not None:
self.size = len(self.playlist)
return True
else:
self.size = None
return False
def update_bar(self, percent):
if percent < 0:
#undefined mode
self.progress_bar.configure(mode='indeterminate')
self.progress_bar.start()
return False
elif percent < 100:
# print value
self.progress_bar.stop()
self.progress_bar.configure(mode='determinate')
self.progress_bar.set(percent)
return True
else:
#print 100%
self.progress_bar.stop()
self.progress_bar.configure(mode='determinate')
self.progress_bar.set(100)
return True
def clean(self) -> None:
"""clean global variables"""
self.path = None
self.link = ''
self.id = ''
def start(self):
"""start multithread process
:return: if successful, true; otherwise, false
"""
# disable start button
self.start_button.configure(state=ctk.DISABLED)
self.get_path_button.configure(state=ctk.DISABLED)
self.print('\n------------------------- start downloading -------------------------\n')
self.download()
self.print('\n\n-------------------------- start converting --------------------------\n')
self.convert()
self.print('\n\n-------------------------- download finish --------------------------\n')
self.clean()
self.thanks()
# enable start button
self.start_button.configure(state=ctk.NORMAL)
self.get_path_button.configure(state=ctk.NORMAL)
return True
def download(self):
"""download the playlist as a MP4 audio only file
:return: if successful, true; otherwise, false
"""
# start download
self.playlist = Playlist("https://www.youtube.com/playlist?list=" + self.id)
counter = 0
self.get_size()
# check link
try:
title = self.playlist.title
except:
self.print('ERROR: broken link\n')
return False
else:
self.print(F'Playlist: {title}\n')
# start downloading audio as MP4
self.update_bar(0)
for url, video in zip(self.playlist, self.playlist.videos):
video_title = video.title
counter += 1
percent = counter / (self.size * 2)
try:
YouTube(url).streams.filter(only_audio=True).first().download(self.path)
except:
self.print(F'\nERROR on {video_title} - download skipped\n')
else:
self.print(F'\n[{counter}/{self.size * 2}] {video_title}')
self.update_bar(percent)
return True
def convert(self):
"""convert MP4 files to MP3
:return: if successful, true; otherwise, false
"""
counter = self.size
# search for mp4 files then convert to mp3
for file in os.listdir(self.path):
if re.search('mp4', file):
counter += 1
percent = counter / (self.size * 2)
self.print(F'\n[{counter}/{self.size * 2}] {file} to MP3')
self.update_bar(percent)
mp4_path = os.path.join(self.path, file)
mp3_path = os.path.join(self.path, os.path.splitext(file)[0]+'.mp3')
new_file = mp.AudioFileClip(mp4_path)
new_file.write_audiofile(mp3_path)
os.remove(mp4_path)
self.update_bar(-1)
return True
def thanks(self):
"""print thanks message on the GUI
:return: if successful, true; otherwise, false
"""
# thanks message
sleep(0.5)
self.print('\nFollow me on github.com/jefferson-lopes')
sleep(0.5)
self.print('\nand linkedin.com/in/lopes-jefferson\n')
sleep(0.5)
self.print('\nPlease let me know if you spotted any bugs or have any')
sleep(0.5)
self.print('\nrecommendations at github.com/jefferson-lopes/openList')
return True
def print(self, msg : str):
"""print's wrapper to GUI
:return: if successful, true; otherwise, false
"""
self.progress_text.insert('end', msg)
self.progress_text.see("end")
return True
if __name__ == "__main__":
# create app object
app = App()
# mainloop to run application infinitely
app.mainloop()