[cookies] Fix cookie load error handling (#11140)

Authored by: Grub4K
This commit is contained in:
Simon Sawicki 2024-10-01 02:13:48 +02:00 committed by GitHub
parent f91645acea
commit e59c82a74c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 35 additions and 20 deletions

View File

@ -27,7 +27,7 @@
from .cache import Cache from .cache import Cache
from .compat import urllib # isort: split from .compat import urllib # isort: split
from .compat import compat_os_name, urllib_req_to_req from .compat import compat_os_name, urllib_req_to_req
from .cookies import LenientSimpleCookie, load_cookies from .cookies import CookieLoadError, LenientSimpleCookie, load_cookies
from .downloader import FFmpegFD, get_suitable_downloader, shorten_protocol_name from .downloader import FFmpegFD, get_suitable_downloader, shorten_protocol_name
from .downloader.rtmp import rtmpdump_version from .downloader.rtmp import rtmpdump_version
from .extractor import gen_extractor_classes, get_info_extractor from .extractor import gen_extractor_classes, get_info_extractor
@ -1624,7 +1624,7 @@ def wrapper(self, *args, **kwargs):
while True: while True:
try: try:
return func(self, *args, **kwargs) return func(self, *args, **kwargs)
except (DownloadCancelled, LazyList.IndexError, PagedList.IndexError): except (CookieLoadError, DownloadCancelled, LazyList.IndexError, PagedList.IndexError):
raise raise
except ReExtractInfo as e: except ReExtractInfo as e:
if e.expected: if e.expected:
@ -3580,6 +3580,8 @@ def __download_wrapper(self, func):
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
try: try:
res = func(*args, **kwargs) res = func(*args, **kwargs)
except CookieLoadError:
raise
except UnavailableVideoError as e: except UnavailableVideoError as e:
self.report_error(e) self.report_error(e)
except DownloadCancelled as e: except DownloadCancelled as e:
@ -4113,8 +4115,13 @@ def proxies(self):
@functools.cached_property @functools.cached_property
def cookiejar(self): def cookiejar(self):
"""Global cookiejar instance""" """Global cookiejar instance"""
return load_cookies( try:
self.params.get('cookiefile'), self.params.get('cookiesfrombrowser'), self) 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
@property @property
def _opener(self): def _opener(self):

View File

@ -15,7 +15,7 @@
import traceback import traceback
from .compat import compat_os_name from .compat import compat_os_name
from .cookies import SUPPORTED_BROWSERS, SUPPORTED_KEYRINGS from .cookies import SUPPORTED_BROWSERS, SUPPORTED_KEYRINGS, CookieLoadError
from .downloader.external import get_external_downloader from .downloader.external import get_external_downloader
from .extractor import list_extractor_classes from .extractor import list_extractor_classes
from .extractor.adobepass import MSO_INFO from .extractor.adobepass import MSO_INFO
@ -1084,7 +1084,7 @@ def main(argv=None):
_IN_CLI = True _IN_CLI = True
try: try:
_exit(*variadic(_real_main(argv))) _exit(*variadic(_real_main(argv)))
except DownloadError: except (CookieLoadError, DownloadError):
_exit(1) _exit(1)
except SameFileError as e: except SameFileError as e:
_exit(f'ERROR: {e}') _exit(f'ERROR: {e}')

View File

@ -34,6 +34,7 @@
from .minicurses import MultilinePrinter, QuietMultilinePrinter from .minicurses import MultilinePrinter, QuietMultilinePrinter
from .utils import ( from .utils import (
DownloadError, DownloadError,
YoutubeDLError,
Popen, Popen,
error_to_str, error_to_str,
expand_path, expand_path,
@ -86,24 +87,31 @@ def _create_progress_bar(logger):
return printer return printer
class CookieLoadError(YoutubeDLError):
pass
def load_cookies(cookie_file, browser_specification, ydl): def load_cookies(cookie_file, browser_specification, ydl):
cookie_jars = [] try:
if browser_specification is not None: cookie_jars = []
browser_name, profile, keyring, container = _parse_browser_specification(*browser_specification) if browser_specification is not None:
cookie_jars.append( browser_name, profile, keyring, container = _parse_browser_specification(*browser_specification)
extract_cookies_from_browser(browser_name, profile, YDLLogger(ydl), keyring=keyring, container=container)) cookie_jars.append(
extract_cookies_from_browser(browser_name, profile, YDLLogger(ydl), keyring=keyring, container=container))
if cookie_file is not None: if cookie_file is not None:
is_filename = is_path_like(cookie_file) is_filename = is_path_like(cookie_file)
if is_filename: if is_filename:
cookie_file = expand_path(cookie_file) cookie_file = expand_path(cookie_file)
jar = YoutubeDLCookieJar(cookie_file) jar = YoutubeDLCookieJar(cookie_file)
if not is_filename or os.access(cookie_file, os.R_OK): if not is_filename or os.access(cookie_file, os.R_OK):
jar.load() jar.load()
cookie_jars.append(jar) cookie_jars.append(jar)
return _merge_cookie_jars(cookie_jars) 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): def extract_cookies_from_browser(browser_name, profile=None, logger=YDLLogger(), *, keyring=None, container=None):