Skip to content

Commit

Permalink
cache extracted browser cookies
Browse files Browse the repository at this point in the history
(in memory, for as long as gallery-dl is running)

Extracting encrypted cookies from a chromium-based browser can take a
long time, so repeating this process for each extractor should be
avoided.

Same goes for creating a temporary copy of the entire cookie database.
  • Loading branch information
mikf committed Jun 4, 2022
1 parent 541a61d commit 535cbcb
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions gallery_dl/extractor/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ def _init_cookies(self):
if cookies:
if isinstance(cookies, dict):
self._update_cookies_dict(cookies, self.cookiedomain)

elif isinstance(cookies, str):
cookiefile = util.expand_path(cookies)
try:
Expand All @@ -311,12 +312,27 @@ def _init_cookies(self):
self.log.warning("cookies: %s", exc)
else:
self._cookiefile = cookiefile

elif isinstance(cookies, (list, tuple)):
from ..cookies import load_cookies
try:
load_cookies(self._cookiejar, cookies)
except Exception as exc:
self.log.warning("cookies: %s", exc)
key = tuple(cookies)
cookiejar = _browser_cookies.get(key)

if cookiejar is None:
from ..cookies import load_cookies
cookiejar = self._cookiejar.__class__()
try:
load_cookies(cookiejar, cookies)
except Exception as exc:
self.log.warning("cookies: %s", exc)
else:
_browser_cookies[key] = cookiejar
else:
self.log.debug("Using cached cookies from %s", key)

setcookie = self._cookiejar.set_cookie
for cookie in cookiejar:
setcookie(cookie)

else:
self.log.warning(
"Expected 'dict', 'list', or 'str' value for 'cookies' "
Expand Down Expand Up @@ -692,6 +708,7 @@ def _build_requests_adapter(ssl_options, ssl_ciphers, source_address):


_adapter_cache = {}
_browser_cookies = {}


HTTP_HEADERS = {
Expand Down

0 comments on commit 535cbcb

Please sign in to comment.