This commit is contained in:
2026-01-21 15:08:22 -08:00
parent 3c19109222
commit da2bff8cd4
4 changed files with 116 additions and 7 deletions

View File

@@ -165,6 +165,74 @@ def _parse_csv_list(value: Any) -> Optional[List[str]]:
return parts or None
_BROWSER_COOKIES_AVAILABLE: Optional[bool] = None
_BROWSER_COOKIE_WARNING_EMITTED = False
def _browser_cookie_candidate_paths() -> List[Path]:
try:
home = Path.home()
except Exception:
home = Path.cwd()
candidates: List[Path] = []
if os.name == "nt":
for env_value in (os.getenv("LOCALAPPDATA"), os.getenv("APPDATA")):
if not env_value:
continue
base_path = Path(env_value)
if not base_path:
continue
candidates.extend([
base_path / "Google" / "Chrome" / "User Data" / "Default" / "Cookies",
base_path / "Chromium" / "User Data" / "Default" / "Cookies",
base_path / "BraveSoftware" / "Brave-Browser" / "User Data" / "Default" / "Cookies",
])
else:
candidates.extend([
home / ".config" / "google-chrome" / "Default" / "Cookies",
home / ".config" / "chromium" / "Default" / "Cookies",
home / ".config" / "BraveSoftware" / "Brave-Browser" / "Default" / "Cookies",
])
if sys.platform == "darwin":
candidates.extend([
home / "Library" / "Application Support" / "Google" / "Chrome" / "Default" / "Cookies",
home / "Library" / "Application Support" / "Chromium" / "Default" / "Cookies",
home / "Library" / "Application Support" / "BraveSoftware" / "Brave-Browser" / "Default" / "Cookies",
])
return candidates
def _has_browser_cookie_database() -> bool:
global _BROWSER_COOKIES_AVAILABLE
if _BROWSER_COOKIES_AVAILABLE is not None:
return _BROWSER_COOKIES_AVAILABLE
for path in _browser_cookie_candidate_paths():
try:
if path.is_file():
_BROWSER_COOKIES_AVAILABLE = True
return True
except Exception:
continue
_BROWSER_COOKIES_AVAILABLE = False
return False
def _add_browser_cookies_if_available(options: Dict[str, Any]) -> None:
global _BROWSER_COOKIE_WARNING_EMITTED
if _has_browser_cookie_database():
options["cookiesfrombrowser"] = ["chrome"]
return
if not _BROWSER_COOKIE_WARNING_EMITTED:
log(
"Browser cookie extraction skipped because no Chrome-compatible cookie database was found. "
"Provide a cookies file via config or --cookies if authentication is required."
)
_BROWSER_COOKIE_WARNING_EMITTED = True
def ensure_yt_dlp_ready() -> None:
"""Verify yt-dlp is importable, raising DownloadError if missing."""
@@ -272,7 +340,7 @@ def list_formats(
ydl_opts["cookiefile"] = str(cookiefile)
else:
# Best effort attempt to use browser cookies if no file is explicitly passed
ydl_opts["cookiesfrombrowser"] = ["chrome"]
_add_browser_cookies_if_available(ydl_opts)
if no_playlist:
ydl_opts["noplaylist"] = True
@@ -365,7 +433,7 @@ def probe_url(
ydl_opts["cookiefile"] = str(cookiefile)
else:
# Best effort fallback
ydl_opts["cookiesfrombrowser"] = ["chrome"]
_add_browser_cookies_if_available(ydl_opts)
if no_playlist:
ydl_opts["noplaylist"] = True
@@ -708,7 +776,7 @@ class YtDlpTool:
# Add browser cookies support "just in case" if no file found (best effort)
# This uses yt-dlp's support for extracting from common browsers.
# Defaulting to 'chrome' as the most common path.
base_options["cookiesfrombrowser"] = ["chrome"]
_add_browser_cookies_if_available(base_options)
# Special handling for format keywords
if opts.ytdl_format == "audio":