summaryrefslogtreecommitdiffstats
path: root/yt_dlp/cookies.py
diff options
context:
space:
mode:
Diffstat (limited to 'yt_dlp/cookies.py')
-rw-r--r--yt_dlp/cookies.py125
1 files changed, 61 insertions, 64 deletions
diff --git a/yt_dlp/cookies.py b/yt_dlp/cookies.py
index 815897d..070d2fc 100644
--- a/yt_dlp/cookies.py
+++ b/yt_dlp/cookies.py
@@ -2,7 +2,9 @@ import base64
import collections
import contextlib
import datetime as dt
+import functools
import glob
+import hashlib
import http.cookiejar
import http.cookies
import io
@@ -17,14 +19,12 @@ import tempfile
import time
import urllib.request
from enum import Enum, auto
-from hashlib import pbkdf2_hmac
from .aes import (
aes_cbc_decrypt_bytes,
aes_gcm_decrypt_and_verify_bytes,
unpad_pkcs7,
)
-from .compat import functools # isort: split
from .compat import compat_os_name
from .dependencies import (
_SECRETSTORAGE_UNAVAILABLE_REASON,
@@ -146,7 +146,7 @@ def _extract_firefox_cookies(profile, container, logger):
identities = json.load(containers).get('identities', [])
container_id = next((context.get('userContextId') for context in identities if container in (
context.get('name'),
- try_call(lambda: re.fullmatch(r'userContext([^\.]+)\.label', context['l10nID']).group())
+ 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')
@@ -263,7 +263,7 @@ def _get_chromium_based_browser_settings(browser_name):
return {
'browser_dir': browser_dir,
'keyring_name': keyring_name,
- 'supports_profiles': browser_name not in browsers_without_profiles
+ 'supports_profiles': browser_name not in browsers_without_profiles,
}
@@ -740,40 +740,38 @@ def _get_linux_desktop_environment(env, logger):
xdg_current_desktop = env.get('XDG_CURRENT_DESKTOP', None)
desktop_session = env.get('DESKTOP_SESSION', None)
if xdg_current_desktop is not None:
- xdg_current_desktop = xdg_current_desktop.split(':')[0].strip()
-
- if xdg_current_desktop == 'Unity':
- if desktop_session is not None and 'gnome-fallback' in desktop_session:
+ for part in map(str.strip, xdg_current_desktop.split(':')):
+ if part == 'Unity':
+ if desktop_session is not None and 'gnome-fallback' in desktop_session:
+ return _LinuxDesktopEnvironment.GNOME
+ else:
+ return _LinuxDesktopEnvironment.UNITY
+ elif part == 'Deepin':
+ return _LinuxDesktopEnvironment.DEEPIN
+ elif part == 'GNOME':
return _LinuxDesktopEnvironment.GNOME
- else:
- return _LinuxDesktopEnvironment.UNITY
- elif xdg_current_desktop == 'Deepin':
- return _LinuxDesktopEnvironment.DEEPIN
- elif xdg_current_desktop == 'GNOME':
- return _LinuxDesktopEnvironment.GNOME
- elif xdg_current_desktop == 'X-Cinnamon':
- return _LinuxDesktopEnvironment.CINNAMON
- elif xdg_current_desktop == 'KDE':
- kde_version = env.get('KDE_SESSION_VERSION', None)
- if kde_version == '5':
- return _LinuxDesktopEnvironment.KDE5
- elif kde_version == '6':
- return _LinuxDesktopEnvironment.KDE6
- elif kde_version == '4':
- return _LinuxDesktopEnvironment.KDE4
- else:
- logger.info(f'unknown KDE version: "{kde_version}". Assuming KDE4')
- return _LinuxDesktopEnvironment.KDE4
- elif xdg_current_desktop == 'Pantheon':
- return _LinuxDesktopEnvironment.PANTHEON
- elif xdg_current_desktop == 'XFCE':
- return _LinuxDesktopEnvironment.XFCE
- elif xdg_current_desktop == 'UKUI':
- return _LinuxDesktopEnvironment.UKUI
- elif xdg_current_desktop == 'LXQt':
- return _LinuxDesktopEnvironment.LXQT
- else:
- logger.info(f'XDG_CURRENT_DESKTOP is set to an unknown value: "{xdg_current_desktop}"')
+ elif part == 'X-Cinnamon':
+ return _LinuxDesktopEnvironment.CINNAMON
+ elif part == 'KDE':
+ kde_version = env.get('KDE_SESSION_VERSION', None)
+ if kde_version == '5':
+ return _LinuxDesktopEnvironment.KDE5
+ elif kde_version == '6':
+ return _LinuxDesktopEnvironment.KDE6
+ elif kde_version == '4':
+ return _LinuxDesktopEnvironment.KDE4
+ else:
+ logger.info(f'unknown KDE version: "{kde_version}". Assuming KDE4')
+ return _LinuxDesktopEnvironment.KDE4
+ elif part == 'Pantheon':
+ return _LinuxDesktopEnvironment.PANTHEON
+ elif part == 'XFCE':
+ return _LinuxDesktopEnvironment.XFCE
+ elif part == 'UKUI':
+ return _LinuxDesktopEnvironment.UKUI
+ elif part == 'LXQt':
+ return _LinuxDesktopEnvironment.LXQT
+ logger.info(f'XDG_CURRENT_DESKTOP is set to an unknown value: "{xdg_current_desktop}"')
elif desktop_session is not None:
if desktop_session == 'deepin':
@@ -826,7 +824,7 @@ def _choose_linux_keyring(logger):
elif desktop_environment == _LinuxDesktopEnvironment.KDE6:
linux_keyring = _LinuxKeyring.KWALLET6
elif desktop_environment in (
- _LinuxDesktopEnvironment.KDE3, _LinuxDesktopEnvironment.LXQT, _LinuxDesktopEnvironment.OTHER
+ _LinuxDesktopEnvironment.KDE3, _LinuxDesktopEnvironment.LXQT, _LinuxDesktopEnvironment.OTHER,
):
linux_keyring = _LinuxKeyring.BASICTEXT
else:
@@ -861,7 +859,7 @@ def _get_kwallet_network_wallet(keyring, logger):
'dbus-send', '--session', '--print-reply=literal',
f'--dest={service_name}',
wallet_path,
- 'org.kde.KWallet.networkWallet'
+ 'org.kde.KWallet.networkWallet',
], text=True, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
if returncode:
@@ -891,7 +889,7 @@ def _get_kwallet_password(browser_keyring_name, keyring, logger):
'kwallet-query',
'--read-password', f'{browser_keyring_name} Safe Storage',
'--folder', f'{browser_keyring_name} Keys',
- network_wallet
+ network_wallet,
], stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
if returncode:
@@ -931,9 +929,8 @@ def _get_gnome_keyring_password(browser_keyring_name, logger):
for item in col.get_all_items():
if item.get_label() == f'{browser_keyring_name} Safe Storage':
return item.get_secret()
- else:
- logger.error('failed to read from keyring')
- return b''
+ logger.error('failed to read from keyring')
+ return b''
def _get_linux_keyring_password(browser_keyring_name, keyring, logger):
@@ -1002,7 +999,7 @@ def _get_windows_v10_key(browser_root, logger):
def pbkdf2_sha1(password, salt, iterations, key_length):
- return pbkdf2_hmac('sha1', password, salt, iterations, key_length)
+ return hashlib.pbkdf2_hmac('sha1', password, salt, iterations, key_length)
def _decrypt_aes_cbc_multi(ciphertext, keys, logger, initialization_vector=b' ' * 16):
@@ -1053,7 +1050,7 @@ def _decrypt_windows_dpapi(ciphertext, logger):
None, # pvReserved: must be NULL
None, # pPromptStruct: information about prompts to display
0, # dwFlags
- ctypes.byref(blob_out) # pDataOut
+ ctypes.byref(blob_out), # pDataOut
)
if not ret:
logger.warning('failed to decrypt with DPAPI', only_once=True)
@@ -1129,24 +1126,24 @@ class LenientSimpleCookie(http.cookies.SimpleCookie):
_LEGAL_VALUE_CHARS = _LEGAL_KEY_CHARS + re.escape('(),/<=>?@[]{}')
_RESERVED = {
- "expires",
- "path",
- "comment",
- "domain",
- "max-age",
- "secure",
- "httponly",
- "version",
- "samesite",
+ 'expires',
+ 'path',
+ 'comment',
+ 'domain',
+ 'max-age',
+ 'secure',
+ 'httponly',
+ 'version',
+ 'samesite',
}
- _FLAGS = {"secure", "httponly"}
+ _FLAGS = {'secure', 'httponly'}
# Added 'bad' group to catch the remaining value
- _COOKIE_PATTERN = re.compile(r"""
+ _COOKIE_PATTERN = re.compile(r'''
\s* # Optional whitespace at start of cookie
(?P<key> # Start of group 'key'
- [""" + _LEGAL_KEY_CHARS + r"""]+?# Any word of at least one letter
+ [''' + _LEGAL_KEY_CHARS + r''']+?# Any word of at least one letter
) # End of group 'key'
( # Optional group: there may not be a value.
\s*=\s* # Equal Sign
@@ -1156,7 +1153,7 @@ class LenientSimpleCookie(http.cookies.SimpleCookie):
| # or
\w{3},\s[\w\d\s-]{9,11}\s[\d:]{8}\sGMT # Special case for "expires" attr
| # or
- [""" + _LEGAL_VALUE_CHARS + r"""]* # Any word or empty string
+ [''' + _LEGAL_VALUE_CHARS + r''']* # Any word or empty string
) # End of group 'val'
| # or
(?P<bad>(?:\\;|[^;])*?) # 'bad' group fallback for invalid values
@@ -1164,7 +1161,7 @@ class LenientSimpleCookie(http.cookies.SimpleCookie):
)? # End of optional value group
\s* # Any number of spaces.
(\s+|;|$) # Ending either at space, semicolon, or EOS.
- """, re.ASCII | re.VERBOSE)
+ ''', re.ASCII | re.VERBOSE)
def load(self, data):
# Workaround for https://github.com/yt-dlp/yt-dlp/issues/4776
@@ -1260,14 +1257,14 @@ class YoutubeDLCookieJar(http.cookiejar.MozillaCookieJar):
# with no name, whereas http.cookiejar regards it as a
# cookie with no value.
name, value = '', name
- f.write('%s\n' % '\t'.join((
+ f.write('{}\n'.format('\t'.join((
cookie.domain,
self._true_or_false(cookie.domain.startswith('.')),
cookie.path,
self._true_or_false(cookie.secure),
str_or_none(cookie.expires, default=''),
- name, value
- )))
+ name, value,
+ ))))
def save(self, filename=None, ignore_discard=True, ignore_expires=True):
"""
@@ -1306,10 +1303,10 @@ class YoutubeDLCookieJar(http.cookiejar.MozillaCookieJar):
return line
cookie_list = line.split('\t')
if len(cookie_list) != self._ENTRY_LEN:
- raise http.cookiejar.LoadError('invalid length %d' % len(cookie_list))
+ raise http.cookiejar.LoadError(f'invalid length {len(cookie_list)}')
cookie = self._CookieFileEntry(*cookie_list)
if cookie.expires_at and not cookie.expires_at.isdigit():
- raise http.cookiejar.LoadError('invalid expires at %s' % cookie.expires_at)
+ raise http.cookiejar.LoadError(f'invalid expires at {cookie.expires_at}')
return line
cf = io.StringIO()