Skip to content

Commit

Permalink
[common] add 'sleep-429' option (mikf#5160)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikf committed Apr 16, 2024
1 parent 923c6f3 commit 566472f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
13 changes: 12 additions & 1 deletion docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -358,13 +358,24 @@ Description
i.e. before starting a new extractor.


extractor.*.sleep-429
---------------------
Type
|Duration|_
Default
``60``
Description
Number of seconds to sleep when receiving a `429 Too Many Requests`
response before `retrying <extractor.*.retries_>`__ the request.


extractor.*.sleep-request
-------------------------
Type
|Duration|_
Default
* ``"0.5-1.5"``
``[Danbooru]``, ``[E621]``, ``[foolfuuka]``, ``itaku``,
``[Danbooru]``, ``[E621]``, ``[foolfuuka]:search``, ``itaku``,
``newgrounds``, ``[philomena]``, ``pixiv:novel``, ``plurk``,
``poipiku`` , ``pornpics``, ``soundgasm``, ``urlgalleries``,
``vk``, ``zerochan``
Expand Down
24 changes: 17 additions & 7 deletions gallery_dl/extractor/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,10 @@ def request(self, url, method="GET", session=None,
if b'name="captcha-bypass"' in content:
self.log.warning("Cloudflare CAPTCHA")
break
if code not in retry_codes and code < 500:

if code == 429 and self._interval_429:
pass
elif code not in retry_codes and code < 500:
break

finally:
Expand All @@ -208,14 +211,18 @@ def request(self, url, method="GET", session=None,
if tries > retries:
break

seconds = tries
if self._interval:
seconds = self._interval()
if seconds < tries:
seconds = tries
s = self._interval()
if seconds < s:
seconds = s
if code == 429 and self._interval_429:
s = self._interval_429()
if seconds < s:
seconds = s
self.wait(seconds=seconds, reason="429 Too Many Requests")
else:
seconds = tries

self.sleep(seconds, "retry")
self.sleep(seconds, "retry")
tries += 1

raise exception.HttpError(msg, response)
Expand Down Expand Up @@ -293,6 +300,9 @@ def _init_options(self):
self.config("sleep-request", self.request_interval),
self.request_interval_min,
)
self._interval_429 = util.build_duration_func(
self.config("sleep-429", 60),
)

if self._retries < 0:
self._retries = float("inf")
Expand Down

0 comments on commit 566472f

Please sign in to comment.