Skip to content

Commit

Permalink
[YoutubeDL] Introduce YoutubeDLCookieJar and clarify the rationale be…
Browse files Browse the repository at this point in the history
…hind session cookies (closes #12929)
  • Loading branch information
dstftw authored and haraldmartin committed Dec 12, 2018
1 parent 928101a commit 432e6de
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
12 changes: 3 additions & 9 deletions youtube_dl/YoutubeDL.py
Expand Up @@ -88,6 +88,7 @@
version_tuple,
write_json_file,
write_string,
YoutubeDLCookieJar,
YoutubeDLCookieProcessor,
YoutubeDLHandler,
)
Expand Down Expand Up @@ -558,7 +559,7 @@ def __exit__(self, *args):
self.restore_console_title()

if self.params.get('cookiefile') is not None:
self.cookiejar.save()
self.cookiejar.save(ignore_discard=True, ignore_expires=True)

def trouble(self, message=None, tb=None):
"""Determine action to take when a download problem appears.
Expand Down Expand Up @@ -2297,16 +2298,9 @@ def _setup_opener(self):
self.cookiejar = compat_cookiejar.CookieJar()
else:
opts_cookiefile = expand_path(opts_cookiefile)
self.cookiejar = compat_cookiejar.MozillaCookieJar(
opts_cookiefile)
self.cookiejar = YoutubeDLCookieJar(opts_cookiefile)
if os.access(opts_cookiefile, os.R_OK):
self.cookiejar.load(ignore_discard=True, ignore_expires=True)
# Force CookieJar to treat 'expires=0' cookies as session/discard cookies
# Fixes https://bugs.python.org/issue17164
for cookie in self.cookiejar:
if cookie.expires == 0:
cookie.expires = None
cookie.discard = True

cookie_processor = YoutubeDLCookieProcessor(self.cookiejar)
if opts_proxy is not None:
Expand Down
28 changes: 28 additions & 0 deletions youtube_dl/utils.py
Expand Up @@ -39,6 +39,7 @@
compat_HTMLParser,
compat_basestring,
compat_chr,
compat_cookiejar,
compat_ctypes_WINFUNCTYPE,
compat_etree_fromstring,
compat_expanduser,
Expand Down Expand Up @@ -1139,6 +1140,33 @@ def https_open(self, req):
req, **kwargs)


class YoutubeDLCookieJar(compat_cookiejar.MozillaCookieJar):
def save(self, filename=None, ignore_discard=False, ignore_expires=False):
# Store session cookies with `expires` set to 0 instead of an empty
# string
for cookie in self:
if cookie.expires is None:
cookie.expires = 0
compat_cookiejar.MozillaCookieJar.save(self, filename, ignore_discard, ignore_expires)

def load(self, filename=None, ignore_discard=False, ignore_expires=False):
compat_cookiejar.MozillaCookieJar.load(self, filename, ignore_discard, ignore_expires)
# Session cookies are denoted by either `expires` field set to
# an empty string or 0. MozillaCookieJar only recognizes the former
# (see [1]). So we need force the latter to be recognized as session
# cookies on our own.
# Session cookies may be important for cookies-based authentication,
# e.g. usually, when user does not check 'Remember me' check box while
# logging in on a site, some important cookies are stored as session
# cookies so that not recognizing them will result in failed login.
# 1. https://bugs.python.org/issue17164
for cookie in self:
# Treat `expires=0` cookies as session cookies
if cookie.expires == 0:
cookie.expires = None
cookie.discard = True


class YoutubeDLCookieProcessor(compat_urllib_request.HTTPCookieProcessor):
def __init__(self, cookiejar=None):
compat_urllib_request.HTTPCookieProcessor.__init__(self, cookiejar)
Expand Down

0 comments on commit 432e6de

Please sign in to comment.