From 7aa9fa796a754d5ee44a1a2dd4c2583b57638a04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Tue, 25 Jul 2017 14:59:41 +0200 Subject: [PATCH] code cleanup and fixes --- gallery_dl/extractor/batoto.py | 2 +- gallery_dl/extractor/chan.py | 1 - gallery_dl/extractor/common.py | 44 ++++++++++++++++--------------- gallery_dl/extractor/exhentai.py | 2 +- gallery_dl/extractor/mangahere.py | 2 +- gallery_dl/extractor/nijie.py | 7 +++-- gallery_dl/extractor/pixiv.py | 2 +- gallery_dl/extractor/seiga.py | 7 +++-- test/test_cookies.py | 7 +++++ test/test_extractors.py | 3 +++ 10 files changed, 43 insertions(+), 34 deletions(-) diff --git a/gallery_dl/extractor/batoto.py b/gallery_dl/extractor/batoto.py index e91a5aeb..d90de328 100644 --- a/gallery_dl/extractor/batoto.py +++ b/gallery_dl/extractor/batoto.py @@ -26,7 +26,7 @@ class BatotoExtractor(): """Login and set necessary cookies""" if self._check_cookies(self.cookienames): return - username, password = self.auth_info() + username, password = self._get_auth_info() if username: cookies = self._login_impl(username, password) for key, value in cookies.items(): diff --git a/gallery_dl/extractor/chan.py b/gallery_dl/extractor/chan.py index f359ec6b..8a9c5745 100644 --- a/gallery_dl/extractor/chan.py +++ b/gallery_dl/extractor/chan.py @@ -79,7 +79,6 @@ class FoolfuukaThreadExtractor(Extractor): def items(self): op = True yield Message.Version, 1 - yield Message.Headers, self.session.headers for post in self.posts(): if op: yield Message.Directory, post diff --git a/gallery_dl/extractor/common.py b/gallery_dl/extractor/common.py index b43a2edd..31e32c05 100644 --- a/gallery_dl/extractor/common.py +++ b/gallery_dl/extractor/common.py @@ -32,20 +32,7 @@ class Extractor(): def __init__(self): self.session = requests.Session() self.log = logging.getLogger(self.category) - - cookies = self.config("cookies") - if cookies: - if isinstance(cookies, dict): - setcookie = self.session.cookies.set - for name, value in cookies.items(): - setcookie(name, value, domain=self.cookiedomain) - else: - try: - cj = http.cookiejar.MozillaCookieJar() - cj.load(cookies) - self.session.cookies.update(cj) - except OSError as exc: - self.log.warning("cookies: %s", exc) + self._set_cookies(self.config("cookies")) def __iter__(self): return self.items() @@ -60,7 +47,13 @@ class Extractor(): return config.interpolate( ("extractor", self.category, self.subcategory, key), default) - def auth_info(self): + def request(self, url, encoding=None, *args, **kwargs): + response = safe_request(self.session, url, *args, **kwargs) + if encoding: + response.encoding = encoding + return response + + def _get_auth_info(self): """Return authentication information as (username, password) tuple""" username = self.config("username") password = None @@ -78,14 +71,23 @@ class Extractor(): return username, password - def request(self, url, encoding=None, *args, **kwargs): - response = safe_request(self.session, url, *args, **kwargs) - if encoding: - response.encoding = encoding - return response + def _set_cookies(self, cookies): + """Populate the cookiejar with 'cookies'""" + if cookies: + if isinstance(cookies, dict): + setcookie = self.session.cookies.set + for name, value in cookies.items(): + setcookie(name, value, domain=self.cookiedomain) + else: + try: + cj = http.cookiejar.MozillaCookieJar() + cj.load(cookies) + self.session.cookies.update(cj) + except OSError as exc: + self.log.warning("cookies: %s", exc) def _check_cookies(self, cookienames, domain=None): - """Return True if all 'cookienames' exist in the current session""" + """Check if all 'cookienames' are in the session's cookiejar""" if not domain and self.cookiedomain: domain = self.cookiedomain for name in cookienames: diff --git a/gallery_dl/extractor/exhentai.py b/gallery_dl/extractor/exhentai.py index 2acfa6c5..1c0257d4 100644 --- a/gallery_dl/extractor/exhentai.py +++ b/gallery_dl/extractor/exhentai.py @@ -180,7 +180,7 @@ class ExhentaiGalleryExtractor(Extractor): """Login and set necessary cookies""" if self._check_cookies(self.cookienames): return - username, password = self.auth_info() + username, password = self._get_auth_info() if not username: self.log.info("no username given; using e-hentai.org") self.root = "https://e-hentai.org" diff --git a/gallery_dl/extractor/mangahere.py b/gallery_dl/extractor/mangahere.py index 1d59211a..f6078baa 100644 --- a/gallery_dl/extractor/mangahere.py +++ b/gallery_dl/extractor/mangahere.py @@ -63,7 +63,7 @@ class MangahereChapterExtractor(AsynchronousExtractor): def get_job_metadata(self, page): """Collect metadata for extractor-job""" manga, pos = text.extract(page, '', '') - chid , pos = text.extract(page, 'h.mhcdn.net/store/manga/', '/', pos) + chid , pos = text.extract(page, '.mhcdn.net/store/manga/', '/', pos) _ , pos = text.extract(page, '