skip login if cookies are present

This commit is contained in:
Mike Fährmann
2017-07-17 10:33:36 +02:00
parent 726c6f01ae
commit 0610ae5000
6 changed files with 22 additions and 12 deletions

View File

@@ -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):

View File

@@ -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):

View File

@@ -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}

View File

@@ -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)

View File

@@ -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)

View File

@@ -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"