-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththumbnail_preview_window_loader.py
executable file
·98 lines (85 loc) · 4.42 KB
/
thumbnail_preview_window_loader.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
import urllib.request
from resolution_downloader import *
from link_parser import *
from thumbnail_preview_window import *
from invalid_link_win_loader import *
from age_restr_win_loader import *
import sys
class ThumbnailPreviewWindowLoader(QMainWindow):
def __init__(self, video_url):
QMainWindow.__init__(self)
self.ui = Ui_Thumbnail_Preview_Window()
self.ui.setupUi(self)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint) #Removes toolbar from window
self.centerWindow()
self.click_position = QtCore.QPoint()
self.video_url = video_url
self.fetch_thumbnail() #Fetching thumbnail url
self.display_thumbnail()
self.loadStreamsToComboboxes(self.video_url)
self.resolution_downloader_thread = QThreadPool()
self.app_icon = QIcon(u":/icons/assets/CT_app_icon.ico")
self.setWindowIcon(self.app_icon)
#Toolbar functions
self.ui.exit_button.clicked.connect(lambda: self.close())
self.ui.download_button.clicked.connect(lambda: self.startResolutionDownloaderThread(self.video_url))
#Display thumbnail
def display_thumbnail(self):
if sys.platform == "win32":
self.ui.thumbnail_preview.setPixmap(QPixmap(os.path.expanduser("~") + u"\\appdata\\Local\\Temp\\thumbnail.png"))
elif sys.platform == "linux" or sys.platform == "linux2":
self.ui.thumbnail_preview.setPixmap(QPixmap(os.path.expanduser("/tmp/thumbnail.png")))
#Fetching thumbnail
def fetch_thumbnail(self):
try:
self.video_thumbnail = pytube.YouTube(self.video_url).thumbnail_url #Querying thumbnail url
# If program fails to query thumbnail url
except pytube.exceptions.RegexMatchError:
self.invalid_link_window = InvalidLinkWinLoader() #Invalid Link Erorr Window
self.invalid_link_window.show()
if sys.platform == "win32":
self.thumbnail_image = urllib.request.urlretrieve(self.video_thumbnail, os.path.expanduser("~\\appdata\\Local\\Temp\\thumbnail.png"))
elif sys.platform == "linux" or sys.platform == "linux2":
self.thumbnail_image = urllib.request.urlretrieve(self.video_thumbnail, os.path.expanduser("/tmp/thumbnail.png"))
#Center the window on the screen
def centerWindow(self):
self.alignment_handler = self.frameGeometry()
self.cp = QDesktopWidget().availableGeometry().center() #Center declaration
self.alignment_handler.moveCenter(self.cp) #Action of moving the window. Function is deprecated and no longer works on it's own. It needs the self.move() function in order to work now..
self.move(self.alignment_handler.topLeft()) #Makes alignment_handler.moveCenter() work. This method is what actually centers the window
#Load available resolutions to the resolution_selector(right) combobox
def loadStreamsToComboboxes(self, video_url):
self.video_url = video_url
self.entered_link = [self.video_url]
self.video = YouTube(str(self.entered_link))
self.video_resolutions = [] #List of available resolutions
try:
#Loop through all of the available reoslutions and codecs
for res in self.video.streams.filter(progressive=True).all():
self.video_resolutions.append(res.resolution)
print(self.video_resolutions)
self.ui.resolution_selector.addItems(self.video_resolutions)
except KeyError:
self.agerestrwin = AgeRestrWinLoader() #Age Restricted Window
self.agerestrwin.show()
if self.video.streams.get_by_itag(133):
self.ui.resolution_selector.addItem("240p") #Remove this
else:
pass
if self.video.streams.get_by_itag(137):
self.ui.resolution_selector.addItem("1080p")
else:
pass
if self.video.streams.get_by_itag(400):
self.ui.resolution_selector.addItem("1440p")
else:
pass
if self.video.streams.get_by_itag(401):
self.ui.resolution_selector.addItem("2160p")
else:
pass
#Starts video downloading thread
def startResolutionDownloaderThread(self, video_url):
self.close()
self.resolution_downloader = ResolutionDownloader(video_url, self.ui.resolution_selector.currentText(), self.ui.format_selector.currentText())
self.resolution_downloader_thread.start(self.resolution_downloader)