-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.py
377 lines (305 loc) · 11.4 KB
/
functions.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import time
from tkinter import *
from tkinter import messagebox, filedialog
import os
import threading
import vlc
import customtkinter
import PIL.Image
import yt_dlp
import sqlite3
import uuid
def create_database():
connection = sqlite3.connect("data/setting.db")
cursor = connection.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS user_settings (
user_id TEXT NOT NULL,
setting_key TEXT NOT NULL,
setting_value TEXT NOT NULL,
PRIMARY KEY (user_id, setting_key)
)
''')
connection.commit()
connection.close()
def get_user_id():
name = "userID.txt"
if os.path.exists(name):
with open(name, "r") as file:
id = file.read().strip()
if id:
return id
else:
with open(name, "w") as file:
id = str(uuid.uuid4())
file.write(id)
return id
def is_table_empty(table_name):
connection = sqlite3.connect("data/setting.db")
cursor = connection.cursor()
cursor.execute(f'SELECT COUNT(*) FROM {table_name}')
count = cursor.fetchone()[0]
connection.close()
return count == 0
def default_setting(id, setting_dict):
if is_table_empty('user_settings'):
connection = sqlite3.connect("data/setting.db")
cursor = connection.cursor()
cursor.executemany('''
INSERT INTO user_settings (user_id, setting_key, setting_value)
VALUES (?, ?, ?)
''', [(id, key, value) for key, value in setting_dict.items()])
connection.commit()
connection.close()
def update_setting(id, setting_dict):
connection = sqlite3.connect("data/setting.db")
cursor = connection.cursor()
for key, value in setting_dict.items():
cursor.execute('''
UPDATE user_settings
SET setting_value = ?
WHERE user_id = ? AND setting_key = ?
''', (value, id, key))
connection.commit()
connection.close()
def get_setting(id, setting_key):
connection = sqlite3.connect("data/setting.db")
cursor = connection.cursor()
cursor.execute('''
SELECT setting_value FROM user_settings
WHERE user_id = ? AND setting_key = ?
''', (id, setting_key))
value = cursor.fetchone()
connection.close()
return value[0] if value else None
download_icon_con = False
download_icon = customtkinter.CTkImage(
PIL.Image.open("Icons/download_icon.png"), size=(20, 20)
)
download_icon_light = customtkinter.CTkImage(
PIL.Image.open("Icons/download_icon_light.png"), size=(20, 20)
)
total_mb = 0
class text_animation:
def __init__(self, label):
self.label = label
self.bool = True
self.value = "Download\nYoutube video\nfor free..."
self.animation = threading.Thread(target=self.skeleton, daemon=True)
self.animation.start()
def skeleton(self):
i = 0
while True:
self.label.configure(text=self.value[: i + 1])
time.sleep(0.2)
i += 1
if i == len(self.value):
self.label.configure(text="")
i = 0
if self.bool == False:
break
def stop(self):
if self.bool:
self.bool = False
self.label.configure(text=self.value)
def start(self):
if not self.bool:
self.bool = True
self.animation = threading.Thread(target=self.skeleton)
self.animation.daemon = True
self.animation.start()
def type(b):
b.entry.delete(0, END)
class Download_UI:
def __init__(self, parent, video_url, pv_frame):
self.id = get_user_id()
pv_frame.player.stop()
pv_frame.root.destroy()
self.parent = parent
self.video_url = video_url
self.cancel = False
self.download_frame = customtkinter.CTkFrame(
self.parent.tab, fg_color=("white", "black"), height=80, width=400
)
self.download_frame.place(relx=1, rely=0.08, anchor="e")
self.size = customtkinter.CTkLabel(self.download_frame, text=" / ")
self.size.grid(row=1, column=1)
self.download_label = customtkinter.CTkLabel(
self.download_frame,
text="Downloading...",
font=("Roboto mono", 15),
)
self.download_label.grid(row=0, column=2, sticky="s")
icon_download = customtkinter.CTkLabel(
self.download_frame,
text="",
)
icon_download.grid(row=1, column=0)
if get_setting(self.id, "theme") == "Light":
icon_download.configure(image=download_icon)
else:
icon_download.configure(image=download_icon_light)
self.progressBar = customtkinter.CTkProgressBar(
self.download_frame, width=200)
self.progressBar.grid(row=1, column=2)
self.progressBar.set(0)
self.percent = customtkinter.CTkLabel(self.download_frame, text=" % ")
self.percent.grid(row=1, column=3)
# cancel button
customtkinter.CTkButton(
self.download_frame,
text=" X ",
font=("", 15, "bold"),
width=15,
fg_color=("white", "black"),
hover_color=("#808080"),
text_color=("black", "white"),
command=self.cancel_download,
).grid(row=1, column=4)
threading.Thread(target=self.download).start()
def download(self):
try:
ql_data = get_setting(self.id, "ql")
location = get_setting(self.id, "location")
quality = ql_data.strip("p")
if quality[0] == "2":
quality = 2160
if ql_data == "1440p(2k)":
quality = 1440
if get_setting(self.id, "type") == "Mp4":
option = {
"progress_hooks": [self.progress_hook],
"format": f"bestvideo[height={quality}]+bestaudio/best",
"outtmpl": rf"{location}\({ql_data})%(title)s.%(ext)s",
}
else:
option = {
"progress_hooks": [self.progress_hook],
"format": "bestaudio/best",
"outtmpl": rf"{location}\(audio)%(title)s.%(ext)s",
}
with yt_dlp.YoutubeDL(option) as yld:
infos = yld.extract_info(self.video_url, download=True)
filename = yld.prepare_filename(infos)
messagebox.showinfo(
"Download Complete",
f"Your video was downloaded successfully at {filename}",
)
current_time = time.time()
os.utime(filename, (current_time, current_time))
self.download_frame.after(500, self.download_frame.destroy)
except Exception as e:
messagebox.showinfo("Download Cancelled", e)
if self.download_frame.winfo_exists():
self.download_frame.after(500, self.download_frame.destroy)
def progress_hook(self, d):
global total_mb
if self.cancel:
raise Exception("Download Cancelled")
if d["status"] == "downloading":
total_bytes = d.get("total_bytes")
downloaded_bytes = d.get("downloaded_bytes")
downloaded_mb = int(downloaded_bytes / 1000000)
total_mb = int(total_bytes / 1000000)
if total_bytes is not None and downloaded_bytes is not None:
progress = int(downloaded_bytes / total_bytes * 100)
self.size.configure(text=f" {downloaded_mb}/{total_mb} MB ")
self.percent.configure(text=f" {progress}% ")
self.progressBar.set(progress / 100)
self.parent.frame.update_idletasks()
elif d["status"] == "finished":
self.progressBar.set(1)
self.parent.frame.update_idletasks()
self.download_label.configure(text="Download Complete")
self.percent.configure(text=" 100% ")
def cancel_download(self):
self.cancel = True
self.download_frame.after(500, self.download_frame.destroy)
def search(c):
threading.Thread(target=Search, args=(c,)).start()
class Search:
def __init__(self, c):
self.id = get_user_id()
self.root = customtkinter.CTkToplevel()
self.root.protocol("WM_DELETE_WINDOW", self.on_closing)
self.root.after(10, self.root.lift)
self.root.title("Preview Video")
self.root.geometry("600x400")
self.is_playing = True
self.video_frame = customtkinter.CTkFrame(self.root)
self.video_frame.pack(fill="both", expand=True)
if get_setting(self.id, "theme") == "Light":
self.play = customtkinter.CTkImage(
PIL.Image.open("Icons/play-button_724963.png"), size=(20, 20)
)
self.pause_icon = customtkinter.CTkImage(
PIL.Image.open("Icons/pause _icon.png"), size=(20, 20)
)
else:
self.play = customtkinter.CTkImage(
PIL.Image.open("Icons/play-button-White.png"), size=(20, 20)
)
self.pause_icon = customtkinter.CTkImage(
PIL.Image.open("Icons/pause _icon_white.png"), size=(20, 20)
)
self.play_pause_button = customtkinter.CTkButton(
self.root,
text=None,
image=self.pause_icon,
fg_color=("#ebebeb", "#242424"),
hover_color=("#ebebeb", "#242424"),
state = DISABLED,
command=self.stop_video,
)
self.play_pause_button.pack()
download_button = customtkinter.CTkButton(
self.root,
text="Download",
image=(
download_icon
if get_setting(self.id, "theme") == "Light"
else download_icon_light
),
compound="left",
font=("Roboto mono", 15),
fg_color=("#D3D3D3", "#2d2d2d"),
hover_color=("white", "#383838"),
text_color=("black", "white"),
state = DISABLED,
command=lambda: Download(c, video_url, self),
)
download_button.pack(pady=5)
null = customtkinter.CTkLabel(
self.root, text=" ", height=10).pack()
video_url = c.entry.get()
player_url = link(video_url)
if player_url:
self.player = vlc.MediaPlayer(player_url)
self.player.set_hwnd(self.video_frame.winfo_id())
self.player.play()
self.play_pause_button.configure(state = NORMAL)
download_button.configure(state = NORMAL)
def stop_video(self):
if self.is_playing:
self.player.pause()
self.is_playing = False
self.play_pause_button.configure(image=self.play)
else:
self.play_pause_button.configure(image=self.pause_icon)
self.player.play()
self.is_playing = True
def on_closing(self):
self.player.stop()
self.root.destroy()
def link(url):
option = {"format": "best"}
with yt_dlp.YoutubeDL(option) as ydl:
try:
info = ydl.extract_info(url, download=False)
video_url = info.get("url", None)
return video_url
except Exception as e:
messagebox.showerror("Error", e)
return None
def Download(root, url, preview_frame):
Download_UI(root, url, preview_frame)