Skip to content
This repository was archived by the owner on Jun 30, 2024. It is now read-only.

Commit 325a946

Browse files
committed
made the code more readable
1 parent 2df9668 commit 325a946

File tree

4 files changed

+133
-121
lines changed

4 files changed

+133
-121
lines changed

scripts/YTDownloader.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: UTF-8 -*-
3+
import PySimpleGUI as sg
4+
from pytube.exceptions import RegexMatchError
5+
from windows import VideoDownloadWindow
6+
7+
8+
sg.theme('Darkred1')
9+
10+
11+
def main() -> None:
12+
# -------------------- defining layouts
13+
start_layout = [
14+
[sg.Input(key='-LINKINPUT-'), sg.Button('Submit')],
15+
]
16+
start_window = sg.Window('Youtube Downloader', start_layout)
17+
18+
# -------------------- main event loop
19+
while True:
20+
try:
21+
event, values = start_window.read()
22+
if event == sg.WIN_CLOSED:
23+
break
24+
25+
if event == 'Submit':
26+
download_window = VideoDownloadWindow(values['-LINKINPUT-'])
27+
download_window.create()
28+
29+
except RegexMatchError as rmx:
30+
if not values['-LINKINPUT-']:
31+
sg.Popup(f'Error: {type(rmx).__name__}', custom_text='Please provide link', title='Error')
32+
else:
33+
sg.Popup(f'Error: {type(rmx).__name__}', custom_text='Invalid link', title='Error')
34+
35+
except Exception as x:
36+
sg.Popup(f'{type(x).__name__} at line {x.__traceback__.tb_lineno} of {__file__}: {x}', custom_text='Unexpected error', title='Error')
37+
break
38+
39+
start_window.close()
40+
41+
42+
43+
44+
if __name__ == '__main__':
45+
main()

scripts/layouts.py

Lines changed: 0 additions & 35 deletions
This file was deleted.

scripts/main.py

Lines changed: 0 additions & 86 deletions
This file was deleted.

scripts/windows.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# -*- coding: UTF-8 -*-
2+
import PySimpleGUI as sg
3+
from pytube import YouTube
4+
import webbrowser
5+
from typing import Any
6+
7+
8+
class VideoDownloadWindow:
9+
def __init__(self, link: str) -> None:
10+
self.__link: str = link
11+
self.video_obj: YouTube = YouTube(self.link, on_progress_callback=self.__progress_check, on_complete_callback=self.__on_complete)
12+
13+
# -------------------- defining layouts
14+
info_tab = [
15+
[sg.Text('Link:'), sg.Text(self.link, enable_events=True, key='-LINK-')],
16+
[sg.Text('Title:'), sg.Text(self.video_obj.title)],
17+
[sg.Text('Length:'), sg.Text(f'{round(self.video_obj.length / 60,2)} minutes')],
18+
[sg.Text('Views:'), sg.Text(self.video_obj.views)],
19+
[sg.Text('Creator:'), sg.Text(self.video_obj.author)],
20+
[sg.Text('Description:'), sg.Multiline(self.video_obj.description, size = (40, 20), no_scrollbar=True, disabled=True)]
21+
]
22+
23+
download_tab = [
24+
[sg.Text('Download Folder'), sg.Input(size=(27, 1), enable_events=True, key='-FOLDER-'), sg.FolderBrowse()],
25+
[sg.Frame('Best Quality', [[sg.Button('Download', key='-BEST-'), sg.Text(self.video_obj.streams.get_highest_resolution().resolution), sg.Text(f'{round(self.video_obj.streams.get_highest_resolution().filesize / 1048576,1)} MB')]])],
26+
[sg.Frame('Worst Quality', [[sg.Button('Download', key='-WORST-'), sg.Text(self.video_obj.streams.get_lowest_resolution().resolution), sg.Text(f'{round(self.video_obj.streams.get_lowest_resolution().filesize / 1048576,1)} MB')]])],
27+
[sg.Frame('Audio', [[sg.Button('Download', key='-AUDIO-'), sg.Text(f'{round(self.video_obj.streams.get_audio_only().filesize / 1048576,1)} MB')]])],
28+
[sg.VPush()],
29+
[sg.Text('', key='-COMPLETED-', size=(40, 1), justification='c', font='underline')],
30+
[sg.Progress(100, orientation='h', size=(20, 20), key='-DOWNLOADPROGRESS-', expand_x=True, bar_color='Black')]
31+
]
32+
33+
self.main_layout = [
34+
[sg.TabGroup([
35+
[sg.Tab('info', info_tab), sg.Tab('download', download_tab)]
36+
])
37+
]
38+
]
39+
40+
# -------------------- defining windows and popups
41+
self.download_window: sg.Window = sg.Window('Youtube Downloader', self.main_layout, modal=True)
42+
self.download_dir_popup = lambda: sg.Popup('Please select a download directory', title='Info')
43+
44+
45+
def create(self) -> None:
46+
# -------------------- download event loop
47+
while True:
48+
self.event, self.values = self.download_window.read()
49+
if self.event == sg.WIN_CLOSED:
50+
break
51+
52+
if self.event == '-LINK-':
53+
webbrowser.open(self.link)
54+
55+
if self.event == '-BEST-':
56+
self.download(self.video_obj.streams.get_highest_resolution().download)
57+
58+
if self.event == '-WORST-':
59+
self.download(self.video_obj.streams.get_lowest_resolution().download)
60+
61+
if self.event == '-AUDIO-':
62+
self.download(self.video_obj.streams.get_audio_only().download)
63+
self.download_window.close()
64+
65+
66+
def download(self, option: Any) -> None:
67+
if not self.values['-FOLDER-']:
68+
self.download_dir_popup()
69+
else:
70+
option(self.values['-FOLDER-'])
71+
72+
@property
73+
def link(self) -> str:
74+
return self.__link
75+
76+
77+
def __progress_check(self, stream: Any, chunk: Any, bytes_remaining: Any) -> None:
78+
self.download_window['-DOWNLOADPROGRESS-'].update(100 - round(bytes_remaining / stream.filesize * 100))
79+
self.download_window['-COMPLETED-'].update(f'{100 - round(bytes_remaining / stream.filesize * 100)}% completed')
80+
81+
82+
def __on_complete(self, stream: Any, file_path: Any) -> None:
83+
self.download_window['-DOWNLOADPROGRESS-'].update(0)
84+
self.download_window['-COMPLETED-'].update('')
85+
sg.Popup('Download completed')
86+
87+
88+

0 commit comments

Comments
 (0)