-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
75 lines (67 loc) · 2.63 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
import os
import urllib.request
from os import path
import pytube
import PySimpleGUI as gui
from PIL import Image
from pytube.exceptions import RegexMatchError
def download_thumbnail(image_url):
file_name = "temp_thumbnail"
urllib.request.urlretrieve(image_url, file_name)
im = Image.open(file_name).convert("RGB")
im.thumbnail((200, 100))
im.save(file_name + ".png", "png")
os.remove("temp_thumbnail")
if __name__ == '__main__':
layout = [
[
gui.Column([
[
gui.Text("URL:", size=(10, 1)),
gui.In(size=(40, 1), key="-URL-"),
gui.Button("Retrieve video", size=(10, 1), key="-RETRIEVE-")
],
[
gui.Text("Save location:", size=(10, 1)),
gui.In(size=(40, 1), key="-SAVEDIR-"),
gui.FolderBrowse(size=(10, 1))
],
[
gui.Image(size=(100, 100), pad=(155, 10), key="-THUMBNAIL-"),
],
[
gui.Text("", size=(40, 1), pad=(82, 10), justification="center", key="-TITLE-")
],
[
gui.Button("Download", size=(20, 1), pad=(160, 20), key="-DOWNLOAD-", disabled=True)
]
])
]
]
window = gui.Window("Youtube Downloader", layout)
while True:
event, values = window.read()
if event == gui.WIN_CLOSED:
break
if event == "-RETRIEVE-":
try:
youtube = pytube.YouTube(values["-URL-"])
window.find_element("-DOWNLOAD-").Update(disabled=False)
window.find_element("-TITLE-").Update(youtube.title.title())
download_thumbnail(youtube.thumbnail_url)
window.find_element("-THUMBNAIL-").Update(filename="./temp_thumbnail.png")
except RegexMatchError:
window.find_element("-DOWNLOAD-").Update(disabled=True)
print("ERROR: URL not found")
if event == "-DOWNLOAD-":
try:
if os.path.isdir(values["-SAVEDIR-"]):
savedir = values["-SAVEDIR-"]
else:
raise NameError
video = youtube.streams.filter(progressive=True).first().download(savedir)
print("Video Downloaded!")
except NameError:
print("ERROR: Directory does not exist")
if path.exists("./temp_thumbnail.png"):
os.remove("./temp_thumbnail.png")