From 0610ae50006047eaeb49c7aecc6254d870fc3d2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Mon, 17 Jul 2017 10:33:36 +0200 Subject: [PATCH] skip login if cookies are present --- gallery_dl/extractor/batoto.py | 5 ++++- gallery_dl/extractor/common.py | 9 +++++++++ gallery_dl/extractor/exhentai.py | 14 ++++---------- gallery_dl/extractor/nijie.py | 2 ++ gallery_dl/extractor/seiga.py | 2 ++ gallery_dl/util.py | 2 +- 6 files changed, 22 insertions(+), 12 deletions(-) diff --git a/gallery_dl/extractor/batoto.py b/gallery_dl/extractor/batoto.py index 00d9a6d4..88ad06a9 100644 --- a/gallery_dl/extractor/batoto.py +++ b/gallery_dl/extractor/batoto.py @@ -19,9 +19,12 @@ class BatotoExtractor(): category = "batoto" scheme = "https" root = "https://bato.to" + cookienames = ("member_id", "pass_hash") def login(self): """Login and set necessary cookies""" + if self._check_cookies(self.cookienames, ".bato.to"): + return username, password = self.auth_info() if username: cookies = self._login_impl(username, password) @@ -53,7 +56,7 @@ class BatotoExtractor(): method="POST", params=params, data=data) if "Sign In - " in response.text: raise exception.AuthenticationError() - return {c: response.cookies[c] for c in ("member_id", "pass_hash")} + return {c: response.cookies[c] for c in self.cookienames} class BatotoMangaExtractor(BatotoExtractor, MangaExtractor): diff --git a/gallery_dl/extractor/common.py b/gallery_dl/extractor/common.py index d2355701..4926dbf9 100644 --- a/gallery_dl/extractor/common.py +++ b/gallery_dl/extractor/common.py @@ -80,6 +80,15 @@ class Extractor(): response.encoding = encoding return response + def _check_cookies(self, cookienames, domain=None): + """Return True if all 'cookienames' exist in the current session""" + for name in cookienames: + try: + self.session.cookies._find(name, domain) + except KeyError: + return False + return True + class AsynchronousExtractor(Extractor): diff --git a/gallery_dl/extractor/exhentai.py b/gallery_dl/extractor/exhentai.py index f5507989..30df13e8 100644 --- a/gallery_dl/extractor/exhentai.py +++ b/gallery_dl/extractor/exhentai.py @@ -36,6 +36,7 @@ class ExhentaiGalleryExtractor(Extractor): }), ] root = "https://exhentai.org" + cookienames = ("ipb_member_id", "ipb_pass_hash") def __init__(self, match): Extractor.__init__(self) @@ -176,6 +177,8 @@ class ExhentaiGalleryExtractor(Extractor): def login(self): """Login and set necessary cookies""" + if self._check_cookies(self.cookienames, ".exhentai.org"): + return username, password = self.auth_info() if not username: self.log.info("no username given; using e-hentai.org") @@ -191,15 +194,6 @@ class ExhentaiGalleryExtractor(Extractor): def _login_impl(self, username, password): """Actual login implementation""" self.log.info("Logging in as %s", username) - cnames = ["ipb_member_id", "ipb_pass_hash"] - - try: - cookies = self.config("cookies") - if isinstance(cookies, dict) and all(c in cookies for c in cnames): - return cookies - except TypeError: - pass - url = "https://forums.e-hentai.org/index.php?act=Login&CODE=01" params = { "CookieDate": "1", @@ -215,4 +209,4 @@ class ExhentaiGalleryExtractor(Extractor): if "You are now logged in as:" not in response.text: raise exception.AuthenticationError() - return {c: response.cookies[c] for c in cnames} + return {c: response.cookies[c] for c in self.cookienames} diff --git a/gallery_dl/extractor/nijie.py b/gallery_dl/extractor/nijie.py index 8da28f72..7a59f0c0 100644 --- a/gallery_dl/extractor/nijie.py +++ b/gallery_dl/extractor/nijie.py @@ -62,6 +62,8 @@ class NijieExtractor(AsynchronousExtractor): def login(self): """Login and obtain session cookie""" + if self._check_cookies(("nemail", "nlogin"), "nijie.info"): + return username, password = self.auth_info() self.session.cookies = self._login_impl(username, password) diff --git a/gallery_dl/extractor/seiga.py b/gallery_dl/extractor/seiga.py index d2b2483d..adb65443 100644 --- a/gallery_dl/extractor/seiga.py +++ b/gallery_dl/extractor/seiga.py @@ -47,6 +47,8 @@ class SeigaExtractor(Extractor): def login(self): """Login and set necessary cookies""" + if self._check_cookies(("user_session",), ".nicovideo.jp"): + return username, password = self.auth_info() self.session.cookies = self._login_impl(username, password) diff --git a/gallery_dl/util.py b/gallery_dl/util.py index c51c2349..9cd223b9 100644 --- a/gallery_dl/util.py +++ b/gallery_dl/util.py @@ -242,7 +242,7 @@ class OAuthSession(): self.session = session self.consumer_secret = consumer_secret self.token_secret = token_secret or "" - self.params = session.params + self.params = {} self.params["oauth_consumer_key"] = consumer_key self.params["oauth_token"] = token self.params["oauth_signature_method"] = "HMAC-SHA1"