From 3158150cb758f4b0a582a2bccf2bc88385e27a27 Mon Sep 17 00:00:00 2001 From: pukkandan Date: Tue, 4 May 2021 22:36:18 +0530 Subject: [PATCH] [utils] Add `network_exceptions` --- yt_dlp/YoutubeDL.py | 9 ++++----- yt_dlp/extractor/common.py | 8 ++------ yt_dlp/extractor/facebook.py | 6 ++---- yt_dlp/utils.py | 8 +++++++- 4 files changed, 15 insertions(+), 16 deletions(-) diff --git a/yt_dlp/YoutubeDL.py b/yt_dlp/YoutubeDL.py index 0791cfd9c..9d29afb9a 100644 --- a/yt_dlp/YoutubeDL.py +++ b/yt_dlp/YoutubeDL.py @@ -19,7 +19,6 @@ import re import shutil import subprocess -import socket import sys import time import tokenize @@ -33,7 +32,6 @@ compat_basestring, compat_cookiejar, compat_get_terminal_size, - compat_http_client, compat_kwargs, compat_numeric_types, compat_os_name, @@ -77,6 +75,7 @@ make_dir, make_HTTPS_handler, MaxDownloadsReached, + network_exceptions, orderedSet, PagedList, parse_filesize, @@ -2271,7 +2270,7 @@ def dl(name, info, subtitle=False): dl(sub_filename, sub_info.copy(), subtitle=True) sub_info['filepath'] = sub_filename files_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 @@ def correct_ext(filename): dl_filename = dl_filename or temp_filename info_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)) return except (OSError, IOError) as err: @@ -3070,7 +3069,7 @@ def _write_thumbnails(self, info_dict, filename): # return the extensions 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.py index 642c94930..e2a9a3801 100644 --- a/yt_dlp/extractor/common.py +++ b/yt_dlp/extractor/common.py @@ -9,8 +9,6 @@ import os import random import re -import socket -import ssl import sys import time import math @@ -58,6 +56,7 @@ js_to_json, JSON_LD_RE, mimetype2ext, + network_exceptions, orderedSet, parse_bitrate, parse_codecs, @@ -659,12 +658,9 @@ def _request_webpage(self, url_or_request, video_id, note=None, errnote=None, fa 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 from diff --git a/yt_dlp/extractor/facebook.py b/yt_dlp/extractor/facebook.py index b68b90bd3..d29b9aab9 100644 --- a/yt_dlp/extractor/facebook.py +++ b/yt_dlp/extractor/facebook.py @@ -3,14 +3,11 @@ import json import re -import socket from .common import InfoExtractor from ..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 @@ int_or_none, js_to_json, limit_length, + network_exceptions, parse_count, qualities, sanitized_Request, @@ -370,7 +368,7 @@ def _login(self): 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)) return diff --git a/yt_dlp/utils.py b/yt_dlp/utils.py index eb266dda7..08e2d19d2 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 @@ def __init__(self, msg, tb=None, expected=False, cause=None, video_id=None): 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 = True if video_id is not None: msg = video_id + ': ' + msg