From 98af5a04099bbbfffb73feb68c7dd64943221f0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Fri, 29 Jul 2022 12:49:04 +0200 Subject: [PATCH] [zerochan] implement login with username & password (#1434) --- README.rst | 3 ++- docs/configuration.rst | 1 + docs/gallery-dl.conf | 5 +++++ gallery_dl/extractor/zerochan.py | 35 +++++++++++++++++++++++++++++++- 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index 1d25a833..c798341e 100644 --- a/README.rst +++ b/README.rst @@ -218,7 +218,8 @@ and optional for ``subscribestar``, ``tapas``, ``tsumino``, -and ``twitter``. +``twitter``, +and ``zerochan``. You can set the necessary information in your configuration file (cf. gallery-dl.conf_) diff --git a/docs/configuration.rst b/docs/configuration.rst index d95dc3f7..177c651f 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -377,6 +377,7 @@ Description * ``tapas`` * ``tsumino`` * ``twitter`` + * ``zerochan`` These values can also be specified via the ``-u/--username`` and ``-p/--password`` command-line options or diff --git a/docs/gallery-dl.conf b/docs/gallery-dl.conf index fc27f557..29eb3b87 100644 --- a/docs/gallery-dl.conf +++ b/docs/gallery-dl.conf @@ -329,6 +329,11 @@ "module": null, "raw-options": null }, + "zerochan": + { + "username": null, + "password": null + }, "booru": { "tags": false, diff --git a/gallery_dl/extractor/zerochan.py b/gallery_dl/extractor/zerochan.py index 256eb8e4..c6011c04 100644 --- a/gallery_dl/extractor/zerochan.py +++ b/gallery_dl/extractor/zerochan.py @@ -9,7 +9,8 @@ """Extractors for https://www.zerochan.net/""" from .booru import BooruExtractor -from .. import text +from ..cache import cache +from .. import text, exception BASE_PATTERN = r"(?:https?://)?(?:www\.)?zerochan\.net" @@ -20,6 +21,38 @@ class ZerochanExtractor(BooruExtractor): root = "https://www.zerochan.net" filename_fmt = "{id}.{extension}" archive_fmt = "{id}" + cookiedomain = ".zerochan.net" + cookienames = ("z_id", "z_hash") + + def login(self): + if not self._check_cookies(self.cookienames): + username, password = self._get_auth_info() + if username: + self._update_cookies(self._login_impl(username, password)) + # force legacy layout + self.session.cookies.set("v3", "0", domain=self.cookiedomain) + + @cache(maxage=90*86400, keyarg=1) + def _login_impl(self, username, password): + self.log.info("Logging in as %s", username) + + url = self.root + "/login" + headers = { + "Origin" : self.root, + "Referer" : url, + } + data = { + "ref" : "/", + "name" : username, + "password": password, + "login" : "Login", + } + + response = self.request(url, method="POST", headers=headers, data=data) + if not response.history: + raise exception.AuthenticationError() + + return response.cookies def _parse_entry_page(self, entry_id): url = "{}/{}".format(self.root, entry_id)