Skip to content

Commit

Permalink
[exhentai] improve limit and error handling (#360)
Browse files Browse the repository at this point in the history
- check image limit before opening the first gallery or image page
- prevent any further exhentai extractors from running after the image
  limit has been reached
  • Loading branch information
mikf committed Jul 30, 2019
1 parent 923e1bb commit beb4fab
Showing 1 changed file with 31 additions and 19 deletions.
50 changes: 31 additions & 19 deletions gallery_dl/extractor/exhentai.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class ExhentaiExtractor(Extractor):
cookienames = ("ipb_member_id", "ipb_pass_hash", "sk")
root = "https://e-hentai.org"

LIMIT = False

def __init__(self, match):
Extractor.__init__(self, match)
self.limits = self.config("limits", True)
Expand All @@ -54,6 +56,9 @@ def wait(self, waittime=None):

def login(self):
"""Login and set necessary cookies"""
if self.LIMIT:
self.log.error("Image limit reached!")
raise exception.StopExtraction()
if self._check_cookies(self.cookienames):
return
username, password = self._get_auth_info()
Expand Down Expand Up @@ -133,22 +138,25 @@ def __init__(self, match):
def items(self):
self.login()

if self.limits:
self._init_limits()

if self.gallery_token:
gpage = self._gallery_page()
self.image_token = text.extract(gpage, 'hentai.org/s/', '"')[0]
if "hentai.org/mpv/" in gpage:
self.log.warning("Extraction with Multi-Page Viewer "
"enabled is not supported")
if not self.image_token:
self.log.error("Failed to extract image token "
"from gallery page")
self.log.error("Failed to extract initial image token")
self.log.debug("Page content:\n%s", gpage)
return
self.wait()
ipage = self._image_page()
else:
ipage = self._image_page()
part = text.extract(ipage, 'hentai.org/g/', '"')[0]
if not part:
self.log.error("Failed to extract gallery token")
self.log.debug("Page content:\n%s", ipage)
return
self.gallery_token = part.split("/")[1]
self.wait()
gpage = self._gallery_page()
Expand Down Expand Up @@ -217,8 +225,9 @@ def image_from_page(self, page):
url = iurl
data = self._parse_image_info(url)
except IndexError:
self.log.warning("Unable to parse image info for '%s'", url)
self.log.error("Unable to parse image info for '%s'", url)
self.log.debug("Page content:\n%s", page)
raise exception.StopExtraction()

data["num"] = self.image_num
data["image_token"] = self.key["start"] = extr('var startkey="', '";')
Expand Down Expand Up @@ -253,8 +262,9 @@ def images_from_api(self):
url = imgurl
data = self._parse_image_info(url)
except IndexError:
self.log.warning("Unable to parse image info for '%s'", url)
self.log.error("Unable to parse image info for '%s'", url)
self.log.debug("Page content:\n%s", page)
raise exception.StopExtraction()

data["num"] = request["page"]
data["image_token"] = imgkey
Expand All @@ -272,6 +282,8 @@ def _gallery_page(self):
raise exception.AuthorizationError()
if page.startswith(("Key missing", "Gallery not found")):
raise exception.NotFoundError("gallery")
if "hentai.org/mpv/" in page:
self.log.warning("Enabled Multi-Page Viewer is not supported")
return page

def _image_page(self):
Expand All @@ -283,28 +295,28 @@ def _image_page(self):
raise exception.NotFoundError("image page")
return page

def _init_limits(self):
self._update_limits()
if self._remaining <= 0:
self.log.error("Image limit reached!")
ExhentaiExtractor.LIMIT = True
raise exception.StopExtraction()

def _check_limits(self, data):
if not self._remaining or data["num"] % 20 == 0:
if data["num"] % 20 == 0:
self._update_limits()
self._remaining -= data["cost"]

if self._remaining <= 0:
url = "{}/s/{}/{}-{}".format(
self.root, data["image_token"], self.gallery_id, data["num"])
self.log.error(
"Image limit reached! Reset it and continue with "
"'%s' as URL.", url)
self.log.error("Image limit reached! Continue with "
"'%s' as URL after resetting it.", url)
ExhentaiExtractor.LIMIT = True
raise exception.StopExtraction()

def _update_limits(self):
url = "https://e-hentai.org/home.php"
cookies = {
cookie.name: cookie.value
for cookie in self.session.cookies
if cookie.domain == self.cookiedomain and cookie.name != "igneous"
}

page = self.request(url, cookies=cookies).text
page = self.request(self.root + "/home.php").text
current, pos = text.extract(page, "<strong>", "</strong>")
maximum, pos = text.extract(page, "<strong>", "</strong>", pos)
self._remaining = text.parse_int(maximum) - text.parse_int(current)
Expand Down

0 comments on commit beb4fab

Please sign in to comment.