diff options
author | Jesús <heckyel@hyperbola.info> | 2019-12-11 12:39:19 -0500 |
---|---|---|
committer | Jesús <heckyel@hyperbola.info> | 2019-12-11 12:39:19 -0500 |
commit | 49987a31a436287e7b0cb1014a08fb940685f202 (patch) | |
tree | a5389de3a192f8dade6cdc86b879b560356a51b5 /hypervideo_gui.py | |
parent | 80d84737d51fcba4977fd4f18a7a6f17fca9589b (diff) | |
download | hypervideo-gui-49987a31a436287e7b0cb1014a08fb940685f202.tar.lz hypervideo-gui-49987a31a436287e7b0cb1014a08fb940685f202.tar.xz hypervideo-gui-49987a31a436287e7b0cb1014a08fb940685f202.zip |
refactoring check_url
Diffstat (limited to 'hypervideo_gui.py')
-rw-r--r-- | hypervideo_gui.py | 64 |
1 files changed, 37 insertions, 27 deletions
diff --git a/hypervideo_gui.py b/hypervideo_gui.py index 0b09433..d86af43 100644 --- a/hypervideo_gui.py +++ b/hypervideo_gui.py @@ -146,9 +146,17 @@ class App(QMainWindow): def url_entry(self): ''' Return URL EntryText''' url = self.urlEntryText.text() - url = url.strip() + url = str(url.strip()) return url + def url_check(self): + ''' Check valid URL ''' + if re.match(self._VALID_URL, self.url_entry()) is None: + check = False + else: + check = True + return check + def downloadVideo_callback(self): ''' Callback for the "Download Video" button ''' @@ -202,7 +210,7 @@ class App(QMainWindow): } # download the video in daemon thread - if re.match(self._VALID_URL, str(self.url_entry())) is None: + if self.url_check() is False: self.statusBar().showMessage('Please add a URL...') else: self.statusBar().showMessage('Downloading Video...') @@ -220,48 +228,50 @@ class App(QMainWindow): ''' self.options = { 'format': 'best', - 'noplaylist': True, # only download single song, not playlist + 'noplaylist': True, # only download single song, not playlist } - try: - with hypervideo.YoutubeDL(self.options) as ydl: - meta = ydl.extract_info(url, download=False) - formats = meta.get('formats', [meta]) - except TypeError: - self.statusBar().showMessage('Problem downloading %s' % url) - return None - - # add formats to combobox - if formats is None: - self.statusBar().showMessage('Formats not found') - else: - item_list = self.default_video_formats_menu_items[0:2] - data_list = [f['format_id'] + ' - ' + f['ext'] for f in formats] - item_list.extend(data_list) - self.populateVideoFormatCombobox(item_list) - self.statusBar().showMessage('Finished Downloading Video Formats', msecs=0) - self.videoFormatCombobox.setCurrentIndex(0) + if self.url_check() is True: + try: + with hypervideo.YoutubeDL(self.options) as ydl: + meta = ydl.extract_info(url, download=False) + formats = meta.get('formats', [meta]) + except TypeError: + self.statusBar().showMessage('Problem downloading %s' % url) + return None + + # add formats to combobox + if formats is None: + self.statusBar().showMessage('Formats not found') + else: + item_list = self.default_video_formats_menu_items[0:2] + data_list = [f['format_id'] + ' - ' + f['ext'] for f in formats] + item_list.extend(data_list) + self.populateVideoFormatCombobox(item_list) + self.statusBar().showMessage('Finished Downloading Video Formats', msecs=0) + self.videoFormatCombobox.setCurrentIndex(0) # check if is valid url # should probably be reworked to be compatible with non-YouTube websites - if re.match(self._VALID_URL, str(self.url_entry())) is None: + if self.url_check() is False: self.populateVideoFormatCombobox(self.default_video_formats_menu_items) return else: # valid url - fetch the video formats in background daemon thread self.statusBar().showMessage('Downloading Video Formats') - thread = threading.Thread(target=getVideoFormats_thread_helper, args=(self, str(self.url_entry()), )) + thread = threading.Thread(target=getVideoFormats_thread_helper, args=(self, self.url_entry(), )) thread.daemon = True thread.start() def videoFormatChange(self, text): if text == self.default_video_formats_menu_items[2]: # detect video formats was selected - # update statusbar to let user know something is happening - self.statusBar().showMessage('Loading available formats...') - # update video formats - self.updateVideoFormats() + if self.url_check() is False: + self.statusBar().showMessage('Please add a URL...') + else: + # update video formats + self.updateVideoFormats() def populateVideoFormatCombobox(self, labels): '''Populate the video format combobox with video formats. Clear the previous labels. |