From 3ca5dac8b636f954a3517c9507ffa7e61db8542c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Thu, 4 May 2023 15:10:47 +0200 Subject: [PATCH] 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 --- docs/configuration.rst | 12 ++++++++---- gallery_dl/extractor/common.py | 22 ++++++++++++++++------ 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/docs/configuration.rst b/docs/configuration.rst index 651275c5..5077a5e5 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -457,13 +457,17 @@ Description extractor.*.cookies-update -------------------------- Type - ``bool`` + * ``bool`` + * |Path|_ Default ``true`` Description - If `extractor.*.cookies`_ specifies the |Path|_ of a cookies.txt - file and it can be opened and parsed without errors, - update its contents with cookies received during data extraction. + Export session cookies in cookies.txt format. + + * If this is a |Path|_, write cookies to the given file path. + + * If this is ``true`` and `extractor.*.cookies`_ specifies the |Path|_ + of a valid cookies.txt file, update its contents. extractor.*.proxy diff --git a/gallery_dl/extractor/common.py b/gallery_dl/extractor/common.py index 78760e0a..09737ef9 100644 --- a/gallery_dl/extractor/common.py +++ b/gallery_dl/extractor/common.py @@ -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'"""