Skip to content

Commit acf281a

Browse files
committed
[twitter] implement 'retries-api' option (#8317)
retry API requests when encountering server-related errors
1 parent 1e7f4ee commit acf281a

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

docs/configuration.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6294,6 +6294,18 @@ Note
62946294
<extractor.*.image-filter_>`__.
62956295

62966296

6297+
extractor.twitter.retries-api
6298+
-----------------------------
6299+
Type
6300+
``integer``
6301+
Default
6302+
``4``
6303+
Description
6304+
Maximum number of retries
6305+
for API requests when encountering server ``errors``,
6306+
or ``-1`` for infinite retries.
6307+
6308+
62976309
extractor.twitter.retweets
62986310
--------------------------
62996311
Type

docs/gallery-dl.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,7 @@
877877
"quoted" : false,
878878
"ratelimit" : "wait",
879879
"replies" : true,
880+
"retries-api" : 4,
880881
"retweets" : false,
881882
"search-limit": 20,
882883
"search-pagination": "cursor",

gallery_dl/extractor/twitter.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1902,14 +1902,14 @@ def _pagination_tweets(self, endpoint, variables,
19021902

19031903
while True:
19041904
params["variables"] = self._json_dumps(variables)
1905-
data = self._call(endpoint, params)["data"]
1905+
data = self._call(endpoint, params)
19061906

19071907
try:
19081908
if path is None:
1909-
instructions = (data["user"]["result"]["timeline"]
1909+
instructions = (data["data"]["user"]["result"]["timeline"]
19101910
["timeline"]["instructions"])
19111911
else:
1912-
instructions = data
1912+
instructions = data["data"]
19131913
for key in path:
19141914
instructions = instructions[key]
19151915
instructions = instructions["instructions"]
@@ -1940,6 +1940,26 @@ def _pagination_tweets(self, endpoint, variables,
19401940
except LookupError:
19411941
extr.log.debug(data)
19421942

1943+
if errors := data.get("errors"):
1944+
if "api_retries" not in locals():
1945+
api_tries = 1
1946+
api_retries = extr.config("retries-api", 4)
1947+
if api_retries < 0:
1948+
api_retries = float("inf")
1949+
1950+
err = []
1951+
srv = False
1952+
for e in errors:
1953+
err.append(f"- '{e.get('message') or e.get('name')}'")
1954+
if e.get("source") == "Server":
1955+
srv = True
1956+
1957+
self.log.warning("API errors (%s/%s):\n%s",
1958+
api_tries, api_retries+1, "\n".join(err))
1959+
if srv and api_tries <= api_retries:
1960+
api_tries += 1
1961+
continue
1962+
19431963
if user := extr._user_obj:
19441964
user = user["legacy"]
19451965
if user.get("blocked_by"):

0 commit comments

Comments
 (0)