A feature-rich command-line audio/video downloader
[utils] Add `network_exceptions`
pukkandan3158150cb758
diff --git a/yt_dlp/YoutubeDL.py b/yt_dlp/YoutubeDL.pyindex 0791cfd9c3..9d29afb9a6 100644--- a/yt_dlp/YoutubeDL.py+++ b/yt_dlp/YoutubeDL.py@@ -19,7 +19,6 @@ import platformimport reimport shutilimport subprocess-import socketimport sysimport timeimport tokenize@@ -33,7 +32,6 @@ from .compat import (compat_basestring,compat_cookiejar,compat_get_terminal_size,- compat_http_client,compat_kwargs,compat_numeric_types,compat_os_name,@@ -77,6 +75,7 @@ from .utils import (make_dir,make_HTTPS_handler,MaxDownloadsReached,+ network_exceptions,orderedSet,PagedList,parse_filesize,@@ -2271,7 +2270,7 @@ class YoutubeDL(object):dl(sub_filename, sub_info.copy(), subtitle=True)sub_info['filepath'] = sub_filenamefiles_to_move[sub_filename] = sub_filename_final- except (ExtractorError, IOError, OSError, ValueError, compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:+ except tuple([ExtractorError, IOError, OSError, ValueError] + network_exceptions) as err:self.report_warning('Unable to download subtitle for "%s": %s' %(sub_lang, error_to_compat_str(err)))continue@@ -2475,7 +2474,7 @@ class YoutubeDL(object):dl_filename = dl_filename or temp_filenameinfo_dict['__finaldir'] = os.path.dirname(os.path.abspath(encodeFilename(full_filename)))- except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:+ except network_exceptions as err:self.report_error('unable to download video data: %s' % error_to_compat_str(err))returnexcept (OSError, IOError) as err:@@ -3070,7 +3069,7 @@ class YoutubeDL(object):ret.append(suffix + thumb_ext)self.to_screen('[%s] %s: Writing thumbnail %sto: %s' %(info_dict['extractor'], info_dict['id'], thumb_display_id, thumb_filename))- except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:+ except network_exceptions as err:self.report_warning('Unable to download thumbnail "%s": %s' %(t['url'], error_to_compat_str(err)))if ret and not write_all:diff --git a/yt_dlp/extractor/common.py b/yt_dlp/extractor/common.pyindex 642c949301..e2a9a3801f 100644--- a/yt_dlp/extractor/common.py+++ b/yt_dlp/extractor/common.py@@ -9,8 +9,6 @@ import netrcimport osimport randomimport re-import socket-import sslimport sysimport timeimport math@@ -58,6 +56,7 @@ from ..utils import (js_to_json,JSON_LD_RE,mimetype2ext,+ network_exceptions,orderedSet,parse_bitrate,parse_codecs,@@ -659,12 +658,9 @@ class InfoExtractor(object):url_or_request = update_url_query(url_or_request, query)if data is not None or headers:url_or_request = sanitized_Request(url_or_request, data, headers)- exceptions = [compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error]- if hasattr(ssl, 'CertificateError'):- exceptions.append(ssl.CertificateError)try:return self._downloader.urlopen(url_or_request)- except tuple(exceptions) as err:+ except network_exceptions as err:if isinstance(err, compat_urllib_error.HTTPError):if self.__can_accept_status_code(err, expected_status):# Retain reference to error to prevent file object fromdiff --git a/yt_dlp/extractor/facebook.py b/yt_dlp/extractor/facebook.pyindex b68b90bd31..d29b9aab90 100644--- a/yt_dlp/extractor/facebook.py+++ b/yt_dlp/extractor/facebook.py@@ -3,14 +3,11 @@ from __future__ import unicode_literalsimport jsonimport re-import socketfrom .common import InfoExtractorfrom ..compat import (compat_etree_fromstring,- compat_http_client,compat_str,- compat_urllib_error,compat_urllib_parse_unquote,compat_urllib_parse_unquote_plus,)@@ -23,6 +20,7 @@ from ..utils import (int_or_none,js_to_json,limit_length,+ network_exceptions,parse_count,qualities,sanitized_Request,@@ -370,7 +368,7 @@ class FacebookIE(InfoExtractor):note='Confirming login')if re.search(r'id="checkpointSubmitButton"', check_response) is not None:self.report_warning('Unable to confirm login, you have to login in your browser and authorize the login.')- except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:+ except network_exceptions as err:self.report_warning('unable to log in: %s' % error_to_compat_str(err))returndiff --git a/yt_dlp/utils.py b/yt_dlp/utils.pyindex eb266dda73..08e2d19d27 100644--- a/yt_dlp/utils.py+++ b/yt_dlp/utils.py@@ -2361,6 +2361,12 @@ class YoutubeDLError(Exception):pass+network_exceptions = [compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error]+if hasattr(ssl, 'CertificateError'):+ network_exceptions.append(ssl.CertificateError)+network_exceptions = tuple(network_exceptions)++class ExtractorError(YoutubeDLError):"""Error during info extraction."""@@ -2369,7 +2375,7 @@ class ExtractorError(YoutubeDLError):If expected is set, this is a normal error message and most likely not a bug in yt-dlp."""- if sys.exc_info()[0] in (compat_urllib_error.URLError, socket.timeout, UnavailableVideoError):+ if sys.exc_info()[0] in network_exceptions:expected = Trueif video_id is not None:msg = video_id + ': ' + msg