Skip to content

Commit

Permalink
[exhentai] stop extraction if image limit is exceeded (#141)
Browse files Browse the repository at this point in the history
can be turned off with the `exhentai.limits' option
  • Loading branch information
mikf committed Jan 26, 2019
1 parent e868fb4 commit 134487f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
10 changes: 10 additions & 0 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,16 @@ Description Minimum wait time in seconds before API requests.
=========== =====


extractor.exhentai.limits
-------------------------
=========== =====
Type ``bool``
Default ``true``
Description Check image download limits
and stop extraction when they are exceeded.
=========== =====


extractor.exhentai.original
---------------------------
=========== =====
Expand Down
32 changes: 32 additions & 0 deletions gallery_dl/extractor/exhentai.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ class ExhentaiExtractor(Extractor):

def __init__(self):
Extractor.__init__(self)
self.limits = self.config("limits", True)
self.original = self.config("original", True)
self.wait_min = self.config("wait-min", 3)
self.wait_max = self.config("wait-max", 6)

self._remaining = 0
if self.wait_max < self.wait_min:
self.wait_max = self.wait_min
self.session.headers["Referer"] = self.root + "/"
Expand Down Expand Up @@ -63,6 +66,7 @@ def login(self):
self.log.info("no username given; using e-hentai.org")
self.root = "https://e-hentai.org"
self.original = False
self.limits = False
self.session.cookies["nw"] = "1"
return
cookies = self._login_impl(username, password)
Expand Down Expand Up @@ -159,6 +163,8 @@ def items(self):
(self.image_from_page(ipage),), self.images_from_api())
for url, image in images:
data.update(image)
if self.limits:
self._check_limits(data)
if "/fullimg.php" in url:
data["extension"] = ""
self.wait(1.5)
Expand Down Expand Up @@ -271,6 +277,32 @@ def _image_page(self):
raise exception.NotFoundError("image page")
return page

def _check_limits(self, data):
if not self._remaining or 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)
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
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)

@staticmethod
def _parse_image_info(url):
parts = url.split("/")[4].split("-")
Expand Down

0 comments on commit 134487f

Please sign in to comment.