mirror of
https://github.com/yt-dlp/yt-dlp.git
synced 2024-12-13 23:02:34 +00:00
[cookies] Report progress when importing cookies
This commit is contained in:
parent
a25bca9f89
commit
97ec5bc550
3 changed files with 96 additions and 46 deletions
|
@ -643,6 +643,11 @@ class YoutubeDL(object):
|
||||||
else:
|
else:
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
if auto_init:
|
||||||
|
if auto_init != 'no_verbose_header':
|
||||||
|
self.print_debug_header()
|
||||||
|
self.add_default_info_extractors()
|
||||||
|
|
||||||
if (sys.platform != 'win32'
|
if (sys.platform != 'win32'
|
||||||
and sys.getfilesystemencoding() in ['ascii', 'ANSI_X3.4-1968']
|
and sys.getfilesystemencoding() in ['ascii', 'ANSI_X3.4-1968']
|
||||||
and not self.params.get('restrictfilenames', False)):
|
and not self.params.get('restrictfilenames', False)):
|
||||||
|
@ -664,13 +669,6 @@ class YoutubeDL(object):
|
||||||
# Set http_headers defaults according to std_headers
|
# Set http_headers defaults according to std_headers
|
||||||
self.params['http_headers'] = merge_headers(std_headers, self.params.get('http_headers', {}))
|
self.params['http_headers'] = merge_headers(std_headers, self.params.get('http_headers', {}))
|
||||||
|
|
||||||
self._setup_opener()
|
|
||||||
|
|
||||||
if auto_init:
|
|
||||||
if auto_init != 'no_verbose_header':
|
|
||||||
self.print_debug_header()
|
|
||||||
self.add_default_info_extractors()
|
|
||||||
|
|
||||||
hooks = {
|
hooks = {
|
||||||
'post_hooks': self.add_post_hook,
|
'post_hooks': self.add_post_hook,
|
||||||
'progress_hooks': self.add_progress_hook,
|
'progress_hooks': self.add_progress_hook,
|
||||||
|
@ -687,6 +685,7 @@ class YoutubeDL(object):
|
||||||
get_postprocessor(pp_def.pop('key'))(self, **compat_kwargs(pp_def)),
|
get_postprocessor(pp_def.pop('key'))(self, **compat_kwargs(pp_def)),
|
||||||
when=when)
|
when=when)
|
||||||
|
|
||||||
|
self._setup_opener()
|
||||||
register_socks_protocols()
|
register_socks_protocols()
|
||||||
|
|
||||||
def preload_download_archive(fn):
|
def preload_download_archive(fn):
|
||||||
|
@ -3698,6 +3697,7 @@ class YoutubeDL(object):
|
||||||
delim=', ') or 'none'
|
delim=', ') or 'none'
|
||||||
write_debug('Optional libraries: %s' % lib_str)
|
write_debug('Optional libraries: %s' % lib_str)
|
||||||
|
|
||||||
|
self._setup_opener()
|
||||||
proxy_map = {}
|
proxy_map = {}
|
||||||
for handler in self._opener.handlers:
|
for handler in self._opener.handlers:
|
||||||
if hasattr(handler, 'proxies'):
|
if hasattr(handler, 'proxies'):
|
||||||
|
@ -3717,6 +3717,8 @@ class YoutubeDL(object):
|
||||||
latest_version)
|
latest_version)
|
||||||
|
|
||||||
def _setup_opener(self):
|
def _setup_opener(self):
|
||||||
|
if hasattr(self, '_opener'):
|
||||||
|
return
|
||||||
timeout_val = self.params.get('socket_timeout')
|
timeout_val = self.params.get('socket_timeout')
|
||||||
self._socket_timeout = 20 if timeout_val is None else float(timeout_val)
|
self._socket_timeout = 20 if timeout_val is None else float(timeout_val)
|
||||||
|
|
||||||
|
|
|
@ -20,6 +20,7 @@ from .compat import (
|
||||||
compat_b64decode,
|
compat_b64decode,
|
||||||
compat_cookiejar_Cookie,
|
compat_cookiejar_Cookie,
|
||||||
)
|
)
|
||||||
|
from .minicurses import MultilinePrinter, QuietMultilinePrinter
|
||||||
from .utils import (
|
from .utils import (
|
||||||
error_to_str,
|
error_to_str,
|
||||||
expand_path,
|
expand_path,
|
||||||
|
@ -73,6 +74,32 @@ class YDLLogger:
|
||||||
if self._ydl:
|
if self._ydl:
|
||||||
self._ydl.report_error(message)
|
self._ydl.report_error(message)
|
||||||
|
|
||||||
|
def progress_bar(self):
|
||||||
|
"""Return a context manager with a print method. (Optional)"""
|
||||||
|
# Do not print to files/pipes, loggers, or when --no-progress is used
|
||||||
|
if not self._ydl or self._ydl.params.get('noprogress') or self._ydl.params.get('logger'):
|
||||||
|
return
|
||||||
|
file = self._ydl._out_files['error']
|
||||||
|
try:
|
||||||
|
if not file.isatty():
|
||||||
|
return
|
||||||
|
except BaseException:
|
||||||
|
return
|
||||||
|
|
||||||
|
printer = MultilinePrinter(file, preserve_output=False)
|
||||||
|
printer.print = lambda message: printer.print_at_line(f'[Cookies] {message}', 0)
|
||||||
|
return printer
|
||||||
|
|
||||||
|
|
||||||
|
def _create_progress_bar(logger):
|
||||||
|
if hasattr(logger, 'progress_bar'):
|
||||||
|
printer = logger.progress_bar()
|
||||||
|
if printer:
|
||||||
|
return printer
|
||||||
|
printer = QuietMultilinePrinter()
|
||||||
|
printer.print = lambda _: None
|
||||||
|
return printer
|
||||||
|
|
||||||
|
|
||||||
def load_cookies(cookie_file, browser_specification, ydl):
|
def load_cookies(cookie_file, browser_specification, ydl):
|
||||||
cookie_jars = []
|
cookie_jars = []
|
||||||
|
@ -115,7 +142,7 @@ def _extract_firefox_cookies(profile, logger):
|
||||||
else:
|
else:
|
||||||
search_root = os.path.join(_firefox_browser_dir(), profile)
|
search_root = os.path.join(_firefox_browser_dir(), profile)
|
||||||
|
|
||||||
cookie_database_path = _find_most_recently_used_file(search_root, 'cookies.sqlite')
|
cookie_database_path = _find_most_recently_used_file(search_root, 'cookies.sqlite', logger)
|
||||||
if cookie_database_path is None:
|
if cookie_database_path is None:
|
||||||
raise FileNotFoundError('could not find firefox cookies database in {}'.format(search_root))
|
raise FileNotFoundError('could not find firefox cookies database in {}'.format(search_root))
|
||||||
logger.debug('Extracting cookies from: "{}"'.format(cookie_database_path))
|
logger.debug('Extracting cookies from: "{}"'.format(cookie_database_path))
|
||||||
|
@ -126,7 +153,11 @@ def _extract_firefox_cookies(profile, logger):
|
||||||
cursor = _open_database_copy(cookie_database_path, tmpdir)
|
cursor = _open_database_copy(cookie_database_path, tmpdir)
|
||||||
cursor.execute('SELECT host, name, value, path, expiry, isSecure FROM moz_cookies')
|
cursor.execute('SELECT host, name, value, path, expiry, isSecure FROM moz_cookies')
|
||||||
jar = YoutubeDLCookieJar()
|
jar = YoutubeDLCookieJar()
|
||||||
for host, name, value, path, expiry, is_secure in cursor.fetchall():
|
with _create_progress_bar(logger) as progress_bar:
|
||||||
|
table = cursor.fetchall()
|
||||||
|
total_cookie_count = len(table)
|
||||||
|
for i, (host, name, value, path, expiry, is_secure) in enumerate(table):
|
||||||
|
progress_bar.print(f'Loading cookie {i: 6d}/{total_cookie_count: 6d}')
|
||||||
cookie = compat_cookiejar_Cookie(
|
cookie = compat_cookiejar_Cookie(
|
||||||
version=0, name=name, value=value, port=None, port_specified=False,
|
version=0, name=name, value=value, port=None, port_specified=False,
|
||||||
domain=host, domain_specified=bool(host), domain_initial_dot=host.startswith('.'),
|
domain=host, domain_specified=bool(host), domain_initial_dot=host.startswith('.'),
|
||||||
|
@ -232,7 +263,7 @@ def _extract_chrome_cookies(browser_name, profile, keyring, logger):
|
||||||
logger.error('{} does not support profiles'.format(browser_name))
|
logger.error('{} does not support profiles'.format(browser_name))
|
||||||
search_root = config['browser_dir']
|
search_root = config['browser_dir']
|
||||||
|
|
||||||
cookie_database_path = _find_most_recently_used_file(search_root, 'Cookies')
|
cookie_database_path = _find_most_recently_used_file(search_root, 'Cookies', logger)
|
||||||
if cookie_database_path is None:
|
if cookie_database_path is None:
|
||||||
raise FileNotFoundError('could not find {} cookies database in "{}"'.format(browser_name, search_root))
|
raise FileNotFoundError('could not find {} cookies database in "{}"'.format(browser_name, search_root))
|
||||||
logger.debug('Extracting cookies from: "{}"'.format(cookie_database_path))
|
logger.debug('Extracting cookies from: "{}"'.format(cookie_database_path))
|
||||||
|
@ -251,25 +282,17 @@ def _extract_chrome_cookies(browser_name, profile, keyring, logger):
|
||||||
jar = YoutubeDLCookieJar()
|
jar = YoutubeDLCookieJar()
|
||||||
failed_cookies = 0
|
failed_cookies = 0
|
||||||
unencrypted_cookies = 0
|
unencrypted_cookies = 0
|
||||||
for host_key, name, value, encrypted_value, path, expires_utc, is_secure in cursor.fetchall():
|
with _create_progress_bar(logger) as progress_bar:
|
||||||
host_key = host_key.decode('utf-8')
|
table = cursor.fetchall()
|
||||||
name = name.decode('utf-8')
|
total_cookie_count = len(table)
|
||||||
value = value.decode('utf-8')
|
for i, line in enumerate(table):
|
||||||
path = path.decode('utf-8')
|
progress_bar.print(f'Loading cookie {i: 6d}/{total_cookie_count: 6d}')
|
||||||
|
is_encrypted, cookie = _process_chrome_cookie(decryptor, *line)
|
||||||
if not value and encrypted_value:
|
if not cookie:
|
||||||
value = decryptor.decrypt(encrypted_value)
|
|
||||||
if value is None:
|
|
||||||
failed_cookies += 1
|
failed_cookies += 1
|
||||||
continue
|
continue
|
||||||
else:
|
elif not is_encrypted:
|
||||||
unencrypted_cookies += 1
|
unencrypted_cookies += 1
|
||||||
|
|
||||||
cookie = compat_cookiejar_Cookie(
|
|
||||||
version=0, name=name, value=value, port=None, port_specified=False,
|
|
||||||
domain=host_key, domain_specified=bool(host_key), domain_initial_dot=host_key.startswith('.'),
|
|
||||||
path=path, path_specified=bool(path), secure=is_secure, expires=expires_utc, discard=False,
|
|
||||||
comment=None, comment_url=None, rest={})
|
|
||||||
jar.set_cookie(cookie)
|
jar.set_cookie(cookie)
|
||||||
if failed_cookies > 0:
|
if failed_cookies > 0:
|
||||||
failed_message = ' ({} could not be decrypted)'.format(failed_cookies)
|
failed_message = ' ({} could not be decrypted)'.format(failed_cookies)
|
||||||
|
@ -285,6 +308,25 @@ def _extract_chrome_cookies(browser_name, profile, keyring, logger):
|
||||||
cursor.connection.close()
|
cursor.connection.close()
|
||||||
|
|
||||||
|
|
||||||
|
def _process_chrome_cookie(decryptor, host_key, name, value, encrypted_value, path, expires_utc, is_secure):
|
||||||
|
host_key = host_key.decode('utf-8')
|
||||||
|
name = name.decode('utf-8')
|
||||||
|
value = value.decode('utf-8')
|
||||||
|
path = path.decode('utf-8')
|
||||||
|
is_encrypted = not value and encrypted_value
|
||||||
|
|
||||||
|
if is_encrypted:
|
||||||
|
value = decryptor.decrypt(encrypted_value)
|
||||||
|
if value is None:
|
||||||
|
return is_encrypted, None
|
||||||
|
|
||||||
|
return is_encrypted, compat_cookiejar_Cookie(
|
||||||
|
version=0, name=name, value=value, port=None, port_specified=False,
|
||||||
|
domain=host_key, domain_specified=bool(host_key), domain_initial_dot=host_key.startswith('.'),
|
||||||
|
path=path, path_specified=bool(path), secure=is_secure, expires=expires_utc, discard=False,
|
||||||
|
comment=None, comment_url=None, rest={})
|
||||||
|
|
||||||
|
|
||||||
class ChromeCookieDecryptor:
|
class ChromeCookieDecryptor:
|
||||||
"""
|
"""
|
||||||
Overview:
|
Overview:
|
||||||
|
@ -547,7 +589,9 @@ def _parse_safari_cookies_page(data, jar, logger):
|
||||||
|
|
||||||
p.skip_to(record_offsets[0], 'unknown page header field')
|
p.skip_to(record_offsets[0], 'unknown page header field')
|
||||||
|
|
||||||
for record_offset in record_offsets:
|
with _create_progress_bar(logger) as progress_bar:
|
||||||
|
for i, record_offset in enumerate(record_offsets):
|
||||||
|
progress_bar.print(f'Loading cookie {i: 6d}/{number_of_cookies: 6d}')
|
||||||
p.skip_to(record_offset, 'space between records')
|
p.skip_to(record_offset, 'space between records')
|
||||||
record_length = _parse_safari_cookies_record(data[record_offset:], jar, logger)
|
record_length = _parse_safari_cookies_record(data[record_offset:], jar, logger)
|
||||||
p.read_bytes(record_length)
|
p.read_bytes(record_length)
|
||||||
|
@ -830,10 +874,11 @@ def _get_mac_keyring_password(browser_keyring_name, logger):
|
||||||
|
|
||||||
|
|
||||||
def _get_windows_v10_key(browser_root, logger):
|
def _get_windows_v10_key(browser_root, logger):
|
||||||
path = _find_most_recently_used_file(browser_root, 'Local State')
|
path = _find_most_recently_used_file(browser_root, 'Local State', logger)
|
||||||
if path is None:
|
if path is None:
|
||||||
logger.error('could not find local state file')
|
logger.error('could not find local state file')
|
||||||
return None
|
return None
|
||||||
|
logger.debug(f'Found local state file at "{path}"')
|
||||||
with open(path, 'r', encoding='utf8') as f:
|
with open(path, 'r', encoding='utf8') as f:
|
||||||
data = json.load(f)
|
data = json.load(f)
|
||||||
try:
|
try:
|
||||||
|
@ -925,13 +970,16 @@ def _get_column_names(cursor, table_name):
|
||||||
return [row[1].decode('utf-8') for row in table_info]
|
return [row[1].decode('utf-8') for row in table_info]
|
||||||
|
|
||||||
|
|
||||||
def _find_most_recently_used_file(root, filename):
|
def _find_most_recently_used_file(root, filename, logger):
|
||||||
# if there are multiple browser profiles, take the most recently used one
|
# if there are multiple browser profiles, take the most recently used one
|
||||||
paths = []
|
i, paths = 0, []
|
||||||
for root, dirs, files in os.walk(root):
|
with _create_progress_bar(logger) as progress_bar:
|
||||||
|
for curr_root, dirs, files in os.walk(root):
|
||||||
for file in files:
|
for file in files:
|
||||||
|
i += 1
|
||||||
|
progress_bar.print(f'Searching for "{filename}": {i: 6d} files searched')
|
||||||
if file == filename:
|
if file == filename:
|
||||||
paths.append(os.path.join(root, file))
|
paths.append(os.path.join(curr_root, file))
|
||||||
return None if not paths else max(paths, key=lambda path: os.lstat(path).st_mtime)
|
return None if not paths else max(paths, key=lambda path: os.lstat(path).st_mtime)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -178,4 +178,4 @@ class MultilinePrinter(MultilinePrinterBase):
|
||||||
*text, CONTROL_SEQUENCES['ERASE_LINE'],
|
*text, CONTROL_SEQUENCES['ERASE_LINE'],
|
||||||
f'{CONTROL_SEQUENCES["UP"]}{CONTROL_SEQUENCES["ERASE_LINE"]}' * self.maximum)
|
f'{CONTROL_SEQUENCES["UP"]}{CONTROL_SEQUENCES["ERASE_LINE"]}' * self.maximum)
|
||||||
else:
|
else:
|
||||||
self.write(*text, ' ' * self._lastlength)
|
self.write('\r', ' ' * self._lastlength, '\r')
|
||||||
|
|
Loading…
Reference in a new issue