Skip to content

Commit

Permalink
Add option --cookies-from-browser to load cookies from a browser (y…
Browse files Browse the repository at this point in the history
…t-dlp#488)

* also adds `--no-cookies-from-browser`

Original PR: ytdl-org/youtube-dl#29201
Authored by: mbway
  • Loading branch information
mbway authored Jul 21, 2021
1 parent 86c25bb commit 5be8ce9
Show file tree
Hide file tree
Showing 5 changed files with 767 additions and 12 deletions.
15 changes: 6 additions & 9 deletions YoutubeDL.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

from .compat import (
compat_basestring,
compat_cookiejar,
compat_get_terminal_size,
compat_kwargs,
compat_numeric_types,
Expand All @@ -42,6 +41,7 @@
compat_urllib_request,
compat_urllib_request_DataHandler,
)
from .cookies import load_cookies
from .utils import (
age_restricted,
args_to_str,
Expand Down Expand Up @@ -110,7 +110,6 @@
version_tuple,
write_json_file,
write_string,
YoutubeDLCookieJar,
YoutubeDLCookieProcessor,
YoutubeDLHandler,
YoutubeDLRedirectHandler,
Expand Down Expand Up @@ -290,6 +289,9 @@ class YoutubeDL(object):
break_on_reject: Stop the download process when encountering a video that
has been filtered out.
cookiefile: File name where cookies should be read from and dumped to
cookiesfrombrowser: A tuple containing the name of the browser and the profile
name/path from where cookies are loaded.
Eg: ('chrome', ) or (vivaldi, 'default')
nocheckcertificate:Do not verify SSL certificates
prefer_insecure: Use HTTP instead of HTTPS to retrieve information.
At the moment, this is only supported by YouTube.
Expand Down Expand Up @@ -3211,16 +3213,11 @@ def _setup_opener(self):
timeout_val = self.params.get('socket_timeout')
self._socket_timeout = 600 if timeout_val is None else float(timeout_val)

opts_cookiesfrombrowser = self.params.get('cookiesfrombrowser')
opts_cookiefile = self.params.get('cookiefile')
opts_proxy = self.params.get('proxy')

if opts_cookiefile is None:
self.cookiejar = compat_cookiejar.CookieJar()
else:
opts_cookiefile = expand_path(opts_cookiefile)
self.cookiejar = YoutubeDLCookieJar(opts_cookiefile)
if os.access(opts_cookiefile, os.R_OK):
self.cookiejar.load(ignore_discard=True, ignore_expires=True)
self.cookiejar = load_cookies(opts_cookiefile, opts_cookiesfrombrowser, self)

cookie_processor = YoutubeDLCookieProcessor(self.cookiejar)
if opts_proxy is not None:
Expand Down
8 changes: 8 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
compat_getpass,
workaround_optparse_bug9161,
)
from .cookies import SUPPORTED_BROWSERS
from .utils import (
DateRange,
decodeOption,
Expand Down Expand Up @@ -242,6 +243,12 @@ def parse_retries(retries, name=''):
if opts.convertthumbnails not in FFmpegThumbnailsConvertorPP.SUPPORTED_EXTS:
parser.error('invalid thumbnail format specified')

if opts.cookiesfrombrowser is not None:
opts.cookiesfrombrowser = [
part.strip() or None for part in opts.cookiesfrombrowser.split(':', 1)]
if opts.cookiesfrombrowser[0] not in SUPPORTED_BROWSERS:
parser.error('unsupported browser specified for cookies')

if opts.date is not None:
date = DateRange.day(opts.date)
else:
Expand Down Expand Up @@ -628,6 +635,7 @@ def report_args_compat(arg, name):
'break_on_reject': opts.break_on_reject,
'skip_playlist_after_errors': opts.skip_playlist_after_errors,
'cookiefile': opts.cookiefile,
'cookiesfrombrowser': opts.cookiesfrombrowser,
'nocheckcertificate': opts.no_check_certificate,
'prefer_insecure': opts.prefer_insecure,
'proxy': opts.proxy,
Expand Down
Loading

0 comments on commit 5be8ce9

Please sign in to comment.