Skip to content

Commit

Permalink
[tumblr] implement 'ratelimit' option (#2919)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikf committed Sep 17, 2022
1 parent d0b73fe commit 46fe469
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
13 changes: 13 additions & 0 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2274,6 +2274,19 @@ Description
use an extra HTTP request to find the URL to its full-resolution version.


extractor.tumblr.ratelimit
--------------------------
Type
``string``
Default
``"abort"``
Description
Selects how to handle exceeding the daily API rate limit.

* ``"abort"``: Raise an error and stop extraction
* ``"wait"``: Wait until rate limit reset


extractor.tumblr.reblogs
------------------------
Type
Expand Down
9 changes: 6 additions & 3 deletions gallery_dl/extractor/tumblr.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,10 +464,8 @@ def _call(self, blog, endpoint, params, **kwargs):

# daily rate limit
if response.headers.get("x-ratelimit-perday-remaining") == "0":
self.log.info("Daily API rate limit exceeded")
reset = response.headers.get("x-ratelimit-perday-reset")
t = (datetime.now() + timedelta(seconds=float(reset))).time()

self.log.error("Daily API rate limit exceeded")

api_key = self.api_key or self.session.auth.consumer_key
if api_key == self.API_KEY:
Expand All @@ -477,6 +475,11 @@ def _call(self, blog, endpoint, params, **kwargs):
"ter/docs/configuration.rst#extractortumblra"
"pi-key--api-secret")

if self.extractor.config("ratelimit") == "wait":
self.extractor.wait(seconds=reset)
return self._call(blog, endpoint, params)

t = (datetime.now() + timedelta(seconds=float(reset))).time()
raise exception.StopExtraction(
"Aborting - Rate limit will reset at %s",
"{:02}:{:02}:{:02}".format(t.hour, t.minute, t.second))
Expand Down

0 comments on commit 46fe469

Please sign in to comment.