-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapp.py
179 lines (143 loc) · 7.81 KB
/
app.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
import tkinter as tk
from tkinter import messagebox
from pytube import YouTube, Playlist
from customtkinter import *
import threading
import os
import time
import re
from PIL import Image, ImageTk
class DownloadApp:
def __init__(self, root):
self.root = root
self.root.title("TheVUnit Downloader")
self.root.iconbitmap(f"{os.getcwd()}/Assets/icon.ico")
self.root.geometry("920x600")
self.root.resizable(False, False)
self.dark_mode = tk.BooleanVar(value=False)
self.root.option_add("*Font", "HelveticaNeue 10")
self.buttons = []
self.widgets = []
self.create_widgets()
def create_widgets(self):
title = CTkLabel(self.root,text="TheVUnit Downloader", font=("HelveticaNeue", 30), bg_color="transparent")
title.pack(padx = 10, pady = 10)
# Entry for URL
url_entry = CTkEntry(self.root, width=500, font=("HelveticaNeue", 10), placeholder_text="Enter a YouTube URL")
self.buttons.append(url_entry)
url_entry.pack(pady=10)
# FileType Radio Buttons
file_type_frame = CTkFrame(self.root)
file_type_frame.pack(pady=10)
self.widgets.append(file_type_frame)
file_type_var = tk.StringVar(value="video")
audio_radio = CTkRadioButton(file_type_frame, text="Audio", variable=file_type_var, value="audio", fg_color="#1900FF")
audio_radio.grid(row=0, column=0, padx=10)
self.buttons.append(audio_radio)
video_radio = CTkRadioButton(file_type_frame, text="Video", variable=file_type_var, value="video", fg_color="#1900FF")
video_radio.grid(row=0, column=1, padx=10)
self.buttons.append(video_radio)
# LinkType Radio Buttons
link_type_frame = CTkFrame(self.root)
link_type_frame.pack(pady=10)
self.widgets.append(link_type_frame)
link_type_var = tk.StringVar(value="single")
single_radio = CTkRadioButton(link_type_frame, text="Single", variable=link_type_var, value="single", fg_color="#1900FF")
single_radio.grid(row=0, column=0, padx=10)
self.buttons.append(single_radio)
playlist_radio = CTkRadioButton(link_type_frame, text="Playlist", variable=link_type_var, value="playlist", fg_color="#1900FF")
playlist_radio.grid(row=0, column=1, padx=10)
self.buttons.append(playlist_radio)
# Start Download Button
start_button = CTkButton(self.root, text="Start Download", command=lambda: self.start_download(url_entry.get(), file_type_var.get(), link_type_var.get()), fg_color="#1900FF")
start_button.pack(pady=10)
self.buttons.append(start_button)
# Progress Bar
self.progress_var = tk.DoubleVar()
progress_bar = CTkProgressBar(self.root, variable=self.progress_var, mode="determinate", bg_color="#1900FF")
progress_bar.pack(pady=10)
# Status Label
self.status_var = tk.StringVar(value="Status: Idle")
status_label = CTkLabel(self.root, textvariable=self.status_var, bg_color="transparent", text_color_disabled="")
status_label.pack(pady=10)
self.buttons.append(status_label)
# Dark Mode Checkbox
self.dark_mode_checkbox = CTkSwitch(self.root, text="Theme Mode", variable=self.dark_mode, command=self.toggle_dark_mode, fg_color="#1900FF")
self.dark_mode_checkbox.pack(pady=10)
self.buttons.append(self.dark_mode_checkbox)
# Load and display the image
tvu_img_data = Image.open(f"{os.getcwd()}/Assets/thevunit.gif")
self.tvu_img = CTkImage(light_image=tvu_img_data, dark_image=tvu_img_data, size=(500, 200))
image_label = CTkLabel(self.root, text= "",image=self.tvu_img, corner_radius=8)
image_label.pack(side="top", padx=10, pady=10)
def start_download(self, url, file_type, link_type):
if not url:
messagebox.showerror("Error", "Please enter a valid URL.")
return
threading.Thread(target=self.download_thread, args=(url, file_type, link_type), daemon=True).start()
def download_thread(self, url, file_type, link_type):
try:
if link_type == "playlist":
playlist = Playlist(url)
for video_url in playlist.video_urls:
video_id_match = re.search(r"(?:v=|\/)([0-9A-Za-z_-]{11})", video_url)
if video_id_match:
video_id = video_id_match.group(1)
video_url = f"https://www.youtube.com/watch?v={video_id}"
yt = YouTube(video_url)
if file_type == "audio":
stream = yt.streams.filter(only_audio=True, abr="256kbps").first()
self.download_file(stream)
file_path = stream.download(f"{os.getcwd()}/Audios")
base, ext = os.path.splitext(file_path)
extenSion = base + '.mp3'
os.rename(file_path, extenSion)
self.status_var.set(f"Status: Downloaded to {file_path}")
elif file_type == "video":
stream = yt.streams.filter(progressive=True, file_extension="mp4").order_by('resolution').desc().first()
self.download_file(stream)
file_path = stream.download(f"{os.getcwd()}/Videos")
self.status_var.set(f"Status: Downloaded to {file_path}")
self.status_var.set("Status: Download Complete")
elif link_type == "single":
yt = YouTube(url)
if file_type == "audio":
stream = yt.streams.filter(only_audio=True, abr="256kbps").first()
self.download_file(stream)
file_path = stream.download(f"{os.getcwd()}/Audios")
base, ext = os.path.splitext(file_path)
extenSion = base + '.mp3'
os.rename(file_path, extenSion)
self.status_var.set(f"Status: Downloaded to {file_path}")
elif file_type == "video":
stream = yt.streams.filter(progressive=True, file_extension="mp4").order_by('resolution').desc().first()
self.download_file(stream)
file_path = stream.download(f"{os.getcwd()}/Videos")
self.status_var.set(f"Status: Downloaded to {file_path}")
self.status_var.set("Status: Download Complete")
except Exception as e:
self.status_var.set(f"Status: Error - {str(e)}")
print(f"An error occurred: {str(e)}")
def download_file(self, stream):
self.status_var.set("Status: Downloading...")
total_bytes = stream.filesize
downloaded_bytes = 0
start_time = time.time()
while downloaded_bytes < total_bytes:
downloaded_bytes = min(downloaded_bytes + 1024, total_bytes)
progress_percentage = int((downloaded_bytes / total_bytes) * 100)
elapsed_time = time.time() - start_time
download_speed = int(downloaded_bytes / (1024 * elapsed_time)) if elapsed_time > 0 else 0
self.progress_var.set(progress_percentage)
self.status_var.set(f"Status: Downloading ({stream.title}) {progress_percentage}% | Speed: {download_speed} KB/s | Time Elapsed: {int(elapsed_time)}s")
def toggle_dark_mode(self):
if self.dark_mode.get():
set_appearance_mode("light")
self.dark_mode_checkbox.configure(text="Dark Mode")
elif not self.dark_mode.get():
set_appearance_mode("dark")
self.dark_mode_checkbox.configure(text="Light Mode")
if __name__ == "__main__":
root = CTk()
app = DownloadApp(root)
root.mainloop()