A feature-rich command-line audio/video downloader
[cookies] Fix cookie load error handling (#11140)
Simon Sawickie59c82a74cda
diff --git a/yt_dlp/YoutubeDL.py b/yt_dlp/YoutubeDL.pyindex 9691a1ea7c..4f45d7faf6 100644--- a/yt_dlp/YoutubeDL.py+++ b/yt_dlp/YoutubeDL.py@@ -27,7 +27,7 @@ import unicodedatafrom .cache import Cachefrom .compat import urllib # isort: splitfrom .compat import compat_os_name, urllib_req_to_req-from .cookies import LenientSimpleCookie, load_cookies+from .cookies import CookieLoadError, LenientSimpleCookie, load_cookiesfrom .downloader import FFmpegFD, get_suitable_downloader, shorten_protocol_namefrom .downloader.rtmp import rtmpdump_versionfrom .extractor import gen_extractor_classes, get_info_extractor@@ -1624,7 +1624,7 @@ class YoutubeDL:while True:try:return func(self, *args, **kwargs)- except (DownloadCancelled, LazyList.IndexError, PagedList.IndexError):+ except (CookieLoadError, DownloadCancelled, LazyList.IndexError, PagedList.IndexError):raiseexcept ReExtractInfo as e:if e.expected:@@ -3580,6 +3580,8 @@ class YoutubeDL:def wrapper(*args, **kwargs):try:res = func(*args, **kwargs)+ except CookieLoadError:+ raiseexcept UnavailableVideoError as e:self.report_error(e)except DownloadCancelled as e:@@ -4113,8 +4115,13 @@ class YoutubeDL:@functools.cached_propertydef cookiejar(self):"""Global cookiejar instance"""- return load_cookies(- self.params.get('cookiefile'), self.params.get('cookiesfrombrowser'), self)+ try:+ return load_cookies(+ self.params.get('cookiefile'), self.params.get('cookiesfrombrowser'), self)+ except CookieLoadError as error:+ cause = error.__context__+ self.report_error(str(cause), tb=''.join(traceback.format_exception(cause)))+ raise@propertydef _opener(self):diff --git a/yt_dlp/__init__.py b/yt_dlp/__init__.pyindex c2d19f94a0..f598b6c2fe 100644--- a/yt_dlp/__init__.py+++ b/yt_dlp/__init__.py@@ -15,7 +15,7 @@ import reimport tracebackfrom .compat import compat_os_name-from .cookies import SUPPORTED_BROWSERS, SUPPORTED_KEYRINGS+from .cookies import SUPPORTED_BROWSERS, SUPPORTED_KEYRINGS, CookieLoadErrorfrom .downloader.external import get_external_downloaderfrom .extractor import list_extractor_classesfrom .extractor.adobepass import MSO_INFO@@ -1084,7 +1084,7 @@ def main(argv=None):_IN_CLI = Truetry:_exit(*variadic(_real_main(argv)))- except DownloadError:+ except (CookieLoadError, DownloadError):_exit(1)except SameFileError as e:_exit(f'ERROR: {e}')diff --git a/yt_dlp/cookies.py b/yt_dlp/cookies.pyindex cff8d74a74..4a69c576be 100644--- a/yt_dlp/cookies.py+++ b/yt_dlp/cookies.py@@ -34,6 +34,7 @@ from .dependencies import (from .minicurses import MultilinePrinter, QuietMultilinePrinterfrom .utils import (DownloadError,+ YoutubeDLError,Popen,error_to_str,expand_path,@@ -86,24 +87,31 @@ def _create_progress_bar(logger):return printer+class CookieLoadError(YoutubeDLError):+ pass++def load_cookies(cookie_file, browser_specification, ydl):- cookie_jars = []- if browser_specification is not None:- browser_name, profile, keyring, container = _parse_browser_specification(*browser_specification)- cookie_jars.append(- extract_cookies_from_browser(browser_name, profile, YDLLogger(ydl), keyring=keyring, container=container))-- if cookie_file is not None:- is_filename = is_path_like(cookie_file)- if is_filename:- cookie_file = expand_path(cookie_file)-- jar = YoutubeDLCookieJar(cookie_file)- if not is_filename or os.access(cookie_file, os.R_OK):- jar.load()- cookie_jars.append(jar)-- return _merge_cookie_jars(cookie_jars)+ try:+ cookie_jars = []+ if browser_specification is not None:+ browser_name, profile, keyring, container = _parse_browser_specification(*browser_specification)+ cookie_jars.append(+ extract_cookies_from_browser(browser_name, profile, YDLLogger(ydl), keyring=keyring, container=container))++ if cookie_file is not None:+ is_filename = is_path_like(cookie_file)+ if is_filename:+ cookie_file = expand_path(cookie_file)++ jar = YoutubeDLCookieJar(cookie_file)+ if not is_filename or os.access(cookie_file, os.R_OK):+ jar.load()+ cookie_jars.append(jar)++ return _merge_cookie_jars(cookie_jars)+ except Exception:+ raise CookieLoadError('failed to load cookies')def extract_cookies_from_browser(browser_name, profile=None, logger=YDLLogger(), *, keyring=None, container=None):