[twitter] implement '"ratelimit": "abort:N"' (#5251 #8864)

This commit is contained in:
Mike Fährmann
2026-01-23 19:54:28 +01:00
parent be9b4cf24f
commit e93cfa3348
2 changed files with 23 additions and 6 deletions

View File

@@ -6472,6 +6472,9 @@ Description
``"abort"``
Raise an error and stop extraction
``"abort:N"``
Raise an error and stop extraction
after waiting ``N`` times until rate limit reset
``"wait"``
Wait until rate limit reset
``"wait:N"``

View File

@@ -2246,12 +2246,26 @@ class TwitterAPI():
rl = self.extractor.config("ratelimit")
if rl == "abort":
raise exception.AbortExtraction("Rate limit exceeded")
elif rl and isinstance(rl, str) and rl.startswith("wait:"):
until = None
seconds = text.parse_float(rl.partition(":")[2]) or 60.0
else:
until = response.headers.get("x-rate-limit-reset")
seconds = None if until else 60.0
until = response.headers.get("x-rate-limit-reset")
seconds = None if until else 60.0
if rl and isinstance(rl, str) and ":" in rl:
rl, _, num = rl.partition(":")
if rl == "abort":
amt = getattr(self, "_ratelimit_amt", 1)
num = text.parse_int(num)
msg = f"Rate limit exceeded ({amt}/{num})"
if amt >= num:
raise exception.AbortExtraction(msg)
self.log.warning(msg)
self._ratelimit_amt = amt + 1
elif rl == "wait":
until = None
seconds = text.parse_float(num) or 60.0
else:
self.log.warning("Unsupported 'ratelimit' setting '%s'", rl)
self.extractor.wait(until=until, seconds=seconds)
def _process_tombstone(self, entry, tombstone):