extend 'cookies-update' functionality

Allow writing cookies to a different file than a given cookies.txt,
making it possible to export cookies imported with --cookies-from-browser

To convert browser cookies to cookies.txt format:
  gallery-dl --cookies-fr chromium \
             -o cookies-update=cookies.txt \
             --no-download \
             http://example.org/file.jpg
This commit is contained in:
Mike Fährmann
2023-05-04 15:10:47 +02:00
parent bc6d65d203
commit 3ca5dac8b6
2 changed files with 24 additions and 10 deletions

View File

@@ -379,12 +379,22 @@ class Extractor():
def _store_cookies(self):
"""Store the session's cookiejar in a cookies.txt file"""
if self._cookiefile and self.config("cookies-update", True):
try:
with open(self._cookiefile, "w") as fp:
util.cookiestxt_store(fp, self._cookiejar)
except OSError as exc:
self.log.warning("cookies: %s", exc)
export = self.config("cookies-update", True)
if not export:
return
if isinstance(export, str):
path = util.expand_path(export)
else:
path = self._cookiefile
if not path:
return
try:
with open(path, "w") as fp:
util.cookiestxt_store(fp, self._cookiejar)
except OSError as exc:
self.log.warning("cookies: %s", exc)
def _update_cookies(self, cookies, domain=""):
"""Update the session's cookiejar with 'cookies'"""