-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.py
91 lines (75 loc) · 2.76 KB
/
controller.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
from model import Model
from view import View
from exceptions import *
import http.client as httplib
import threading
class Controller:
"""
Controller class of "Youtube thumbnail finder" app.
TBD:
Attributes
----------
name : str
first name of the person
Methods
-------
info(additional=""):
Prints the person's name and age.
"""
def __init__(self) -> None:
self.model = Model()
self.view = View(self)
self.processing_video = False
def main(self):
# Check internet_connectivity.
# Show error message in case there is no internet.
if self._check_internet_connectivity() == False:
message = "No internet connection available. "\
"To use application, connect computer to the internet."
self.view.set_status_bar_msg("Error.")
self.view.show_messagebox("Error", message)
self.view.set_status_bar_msg("")
self.view.main()
def button_on_click(self):
if self.processing_video:
return
self.view.process_video_btn.state(["disabled"]) # Disable the button.
self.processing_video = True
t = threading.Thread(target=self._start_processing_video)
t.start()
self.view.set_status_bar_msg("Processing the video...")
# Function to be run in separate thread
def _start_processing_video(self):
self.model.input_url = self.view.input_url.get()
try:
output_url = self.model.process_video()
self.view.output_url.set(output_url)
self.view.show_thumbnail()
self.view.set_status_bar_msg("Program has finished.")
except(InternetConnectionException) as e:
self.view.set_status_bar_msg("Error.")
self.view.show_messagebox("Error", e)
self.view.set_status_bar_msg("")
except(InvalidVideoUrlException) as e:
self.view.set_status_bar_msg("Info.")
self.view.show_messagebox("Info", e)
self.view.set_status_bar_msg("")
except(PytubeStreamException) as e:
self.view.set_status_bar_msg("Error.")
self.view.show_messagebox("Error", e)
self.view.set_status_bar_msg("")
finally:
self.processing_video = False
self.view.process_video_btn.state(["!disabled"]) # Enable the button.
def _check_internet_connectivity(self) -> bool:
conn = httplib.HTTPSConnection("8.8.8.8", timeout=5)
try:
conn.request("HEAD", "/")
return True
except Exception:
return False
finally:
conn.close()
if __name__ == "__main__":
controller = Controller()
controller.main()