mirror of
https://github.com/yt-dlp/yt-dlp.git
synced 2025-06-27 17:08:32 +00:00
[cookies] Add --cookies-from-browser
support for Zen
This commit is contained in:
parent
cc749a8a3b
commit
ccf2fc2795
@ -47,7 +47,8 @@
|
||||
from .utils.networking import normalize_url
|
||||
|
||||
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):
|
||||
@ -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):
|
||||
if browser_name == 'firefox':
|
||||
return _extract_firefox_cookies(profile, container, logger)
|
||||
if browser_name in FIREFOX_BASED_BROWSERS:
|
||||
return _extract_firefox_cookies(browser_name, profile, container, logger)
|
||||
elif browser_name == 'safari':
|
||||
return _extract_safari_cookies(profile, logger)
|
||||
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}')
|
||||
|
||||
|
||||
def _extract_firefox_cookies(profile, container, logger):
|
||||
logger.info('Extracting cookies from firefox')
|
||||
def _extract_firefox_cookies(browser_name, profile, container, logger):
|
||||
logger.info(f'Extracting cookies from {browser_name}')
|
||||
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')
|
||||
return YoutubeDLCookieJar()
|
||||
|
||||
if profile is None:
|
||||
search_roots = list(_firefox_browser_dirs())
|
||||
search_roots = list(_firefox_browser_dirs(browser_name))
|
||||
elif _is_path(profile):
|
||||
search_roots = [profile]
|
||||
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))
|
||||
|
||||
cookie_database_path = _newest(_firefox_cookie_dbs(search_roots))
|
||||
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}"')
|
||||
|
||||
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()),
|
||||
)), None)
|
||||
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:
|
||||
cursor = None
|
||||
@ -164,7 +165,7 @@ def _extract_firefox_cookies(profile, container, logger):
|
||||
cursor = _open_database_copy(cookie_database_path, tmpdir)
|
||||
if isinstance(container_id, int):
|
||||
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(
|
||||
'SELECT host, name, value, path, expiry, isSecure FROM moz_cookies WHERE originAttributes LIKE ? OR originAttributes LIKE ?',
|
||||
(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,
|
||||
comment=None, comment_url=None, rest={})
|
||||
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
|
||||
finally:
|
||||
if cursor is not None:
|
||||
cursor.connection.close()
|
||||
|
||||
|
||||
def _firefox_browser_dirs():
|
||||
def _firefox_browser_dirs(browser_name):
|
||||
if sys.platform in ('cygwin', 'win32'):
|
||||
yield from map(os.path.expandvars, (
|
||||
R'%APPDATA%\Mozilla\Firefox\Profiles',
|
||||
R'%LOCALAPPDATA%\Packages\Mozilla.Firefox_n80bbvh6b1yt2\LocalCache\Roaming\Mozilla\Firefox\Profiles',
|
||||
))
|
||||
browser_dirs = {
|
||||
'firefox': [
|
||||
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':
|
||||
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:
|
||||
yield from map(os.path.expanduser, (
|
||||
'~/.mozilla/firefox',
|
||||
'~/snap/firefox/common/.mozilla/firefox',
|
||||
'~/.var/app/org.mozilla.firefox/.mozilla/firefox',
|
||||
))
|
||||
browser_dirs = {
|
||||
'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):
|
||||
|
Loading…
Reference in New Issue
Block a user