1
0
mirror of https://github.com/yt-dlp/yt-dlp.git synced 2025-06-28 09:28:33 +00:00

[cookies] Add --cookies-from-browser support for Zen

This commit is contained in:
Roland Crosby 2025-05-27 22:57:37 -04:00
parent cc749a8a3b
commit ccf2fc2795
No known key found for this signature in database

View File

@ -47,7 +47,8 @@
from .utils.networking import normalize_url from .utils.networking import normalize_url
CHROMIUM_BASED_BROWSERS = {'brave', 'chrome', 'chromium', 'edge', 'opera', 'vivaldi', 'whale'} CHROMIUM_BASED_BROWSERS = {'brave', 'chrome', 'chromium', 'edge', 'opera', 'vivaldi', 'whale'}
SUPPORTED_BROWSERS = CHROMIUM_BASED_BROWSERS | {'firefox', 'safari'} FIREFOX_BASED_BROWSERS = {'firefox', 'zen'}
SUPPORTED_BROWSERS = CHROMIUM_BASED_BROWSERS | FIREFOX_BASED_BROWSERS | {'safari'}
class YDLLogger(_YDLLogger): class YDLLogger(_YDLLogger):
@ -114,8 +115,8 @@ def load_cookies(cookie_file, browser_specification, ydl):
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):
if browser_name == 'firefox': if browser_name in FIREFOX_BASED_BROWSERS:
return _extract_firefox_cookies(profile, container, logger) return _extract_firefox_cookies(browser_name, profile, container, logger)
elif browser_name == 'safari': elif browser_name == 'safari':
return _extract_safari_cookies(profile, logger) return _extract_safari_cookies(profile, logger)
elif browser_name in CHROMIUM_BASED_BROWSERS: elif browser_name in CHROMIUM_BASED_BROWSERS:
@ -124,24 +125,24 @@ def extract_cookies_from_browser(browser_name, profile=None, logger=YDLLogger(),
raise ValueError(f'unknown browser: {browser_name}') raise ValueError(f'unknown browser: {browser_name}')
def _extract_firefox_cookies(profile, container, logger): def _extract_firefox_cookies(browser_name, profile, container, logger):
logger.info('Extracting cookies from firefox') logger.info(f'Extracting cookies from {browser_name}')
if not sqlite3: if not sqlite3:
logger.warning('Cannot extract cookies from firefox without sqlite3 support. ' logger.warning(f'Cannot extract cookies from {browser_name} without sqlite3 support. '
'Please use a Python interpreter compiled with sqlite3 support') 'Please use a Python interpreter compiled with sqlite3 support')
return YoutubeDLCookieJar() return YoutubeDLCookieJar()
if profile is None: if profile is None:
search_roots = list(_firefox_browser_dirs()) search_roots = list(_firefox_browser_dirs(browser_name))
elif _is_path(profile): elif _is_path(profile):
search_roots = [profile] search_roots = [profile]
else: else:
search_roots = [os.path.join(path, profile) for path in _firefox_browser_dirs()] search_roots = [os.path.join(path, profile) for path in _firefox_browser_dirs(browser_name)]
search_root = ', '.join(map(repr, search_roots)) search_root = ', '.join(map(repr, search_roots))
cookie_database_path = _newest(_firefox_cookie_dbs(search_roots)) cookie_database_path = _newest(_firefox_cookie_dbs(search_roots))
if cookie_database_path is None: if cookie_database_path is None:
raise FileNotFoundError(f'could not find firefox cookies database in {search_root}') raise FileNotFoundError(f'could not find {browser_name} cookies database in {search_root}')
logger.debug(f'Extracting cookies from: "{cookie_database_path}"') logger.debug(f'Extracting cookies from: "{cookie_database_path}"')
container_id = None container_id = None
@ -156,7 +157,7 @@ def _extract_firefox_cookies(profile, container, logger):
try_call(lambda: re.fullmatch(r'userContext([^\.]+)\.label', context['l10nID']).group()), try_call(lambda: re.fullmatch(r'userContext([^\.]+)\.label', context['l10nID']).group()),
)), None) )), None)
if not isinstance(container_id, int): if not isinstance(container_id, int):
raise ValueError(f'could not find firefox container "{container}" in containers.json') raise ValueError(f'could not find {browser_name} container "{container}" in containers.json')
with tempfile.TemporaryDirectory(prefix='yt_dlp') as tmpdir: with tempfile.TemporaryDirectory(prefix='yt_dlp') as tmpdir:
cursor = None cursor = None
@ -164,7 +165,7 @@ def _extract_firefox_cookies(profile, container, logger):
cursor = _open_database_copy(cookie_database_path, tmpdir) cursor = _open_database_copy(cookie_database_path, tmpdir)
if isinstance(container_id, int): if isinstance(container_id, int):
logger.debug( logger.debug(
f'Only loading cookies from firefox container "{container}", ID {container_id}') f'Only loading cookies from {browser_name} container "{container}", ID {container_id}')
cursor.execute( cursor.execute(
'SELECT host, name, value, path, expiry, isSecure FROM moz_cookies WHERE originAttributes LIKE ? OR originAttributes LIKE ?', 'SELECT host, name, value, path, expiry, isSecure FROM moz_cookies WHERE originAttributes LIKE ? OR originAttributes LIKE ?',
(f'%userContextId={container_id}', f'%userContextId={container_id}&%')) (f'%userContextId={container_id}', f'%userContextId={container_id}&%'))
@ -186,29 +187,44 @@ def _extract_firefox_cookies(profile, container, logger):
path=path, path_specified=bool(path), secure=is_secure, expires=expiry, discard=False, path=path, path_specified=bool(path), secure=is_secure, expires=expiry, discard=False,
comment=None, comment_url=None, rest={}) comment=None, comment_url=None, rest={})
jar.set_cookie(cookie) jar.set_cookie(cookie)
logger.info(f'Extracted {len(jar)} cookies from firefox') logger.info(f'Extracted {len(jar)} cookies from {browser_name}')
return jar return jar
finally: finally:
if cursor is not None: if cursor is not None:
cursor.connection.close() cursor.connection.close()
def _firefox_browser_dirs(): def _firefox_browser_dirs(browser_name):
if sys.platform in ('cygwin', 'win32'): if sys.platform in ('cygwin', 'win32'):
yield from map(os.path.expandvars, ( browser_dirs = {
R'%APPDATA%\Mozilla\Firefox\Profiles', 'firefox': [
R'%LOCALAPPDATA%\Packages\Mozilla.Firefox_n80bbvh6b1yt2\LocalCache\Roaming\Mozilla\Firefox\Profiles', R'%APPDATA%\Mozilla\Firefox\Profiles',
)) R'%LOCALAPPDATA%\Packages\Mozilla.Firefox_n80bbvh6b1yt2\LocalCache\Roaming\Mozilla\Firefox\Profiles',
],
'zen': [R'%APPDATA%\zen\Profiles'],
}[browser_name]
yield from map(os.path.expandvars, browser_dirs)
elif sys.platform == 'darwin': elif sys.platform == 'darwin':
yield os.path.expanduser('~/Library/Application Support/Firefox/Profiles') browser_dir = {
'firefox': '~/Library/Application Support/Firefox/Profiles',
'zen': '~/Library/Application Support/Zen/Profiles',
}[browser_name]
yield os.path.expanduser(browser_dir)
else: else:
yield from map(os.path.expanduser, ( browser_dirs = {
'~/.mozilla/firefox', 'firefox': [
'~/snap/firefox/common/.mozilla/firefox', '~/.mozilla/firefox',
'~/.var/app/org.mozilla.firefox/.mozilla/firefox', '~/snap/firefox/common/.mozilla/firefox',
)) '~/.var/app/org.mozilla.firefox/.mozilla/firefox',
],
'zen': [
'~/.zen',
'~/.var/app/app.zen_browser.zen/zen',
],
}[browser_name]
yield from map(os.path.expanduser, browser_dirs)
def _firefox_cookie_dbs(roots): def _firefox_cookie_dbs(roots):