about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbashonly <bashonly@bashonly.com>2023-07-04 14:03:39 -0500
committerdirkf <fieldhouse@gmx.net>2023-07-18 10:50:46 +0100
commit3801d36416d6e3e6031dc4fcac01891ce7ddb55b (patch)
tree8ee0ccc798b844a7d4e01ae3729c52f1ced52372
parentb383be98874d4dded67ee8a679fae30340722709 (diff)
downloadyoutube-dl-3801d36416d6e3e6031dc4fcac01891ce7ddb55b.tar.gz
youtube-dl-3801d36416d6e3e6031dc4fcac01891ce7ddb55b.tar.xz
youtube-dl-3801d36416d6e3e6031dc4fcac01891ce7ddb55b.zip
[utils] `YoutubeDLCookieJar`: Add `get_cookie_header` and `get_cookies_for_url` methods
-rw-r--r--youtube_dl/utils.py13
1 files changed, 13 insertions, 0 deletions
diff --git a/youtube_dl/utils.py b/youtube_dl/utils.py
index c21cd3687..ac6c81465 100644
--- a/youtube_dl/utils.py
+++ b/youtube_dl/utils.py
@@ -2912,6 +2912,19 @@ class YoutubeDLCookieJar(compat_cookiejar.MozillaCookieJar):
                 cookie.expires = None
                 cookie.discard = True
 
+    def get_cookie_header(self, url):
+        """Generate a Cookie HTTP header for a given url"""
+        cookie_req = sanitized_Request(url)
+        self.add_cookie_header(cookie_req)
+        return cookie_req.get_header('Cookie')
+
+    def get_cookies_for_url(self, url):
+        """Generate a list of Cookie objects for a given url"""
+        # Policy `_now` attribute must be set before calling `_cookies_for_request`
+        # Ref: https://github.com/python/cpython/blob/3.7/Lib/http/cookiejar.py#L1360
+        self._policy._now = self._now = int(time.time())
+        return self._cookies_for_request(sanitized_Request(url))
+
 
 class YoutubeDLCookieProcessor(compat_urllib_request.HTTPCookieProcessor):
     def __init__(self, cookiejar=None):