Skip to content

Commit

Permalink
[twitter] fix guest token expiring during pagination (mikf#3445)
Browse files Browse the repository at this point in the history
  • Loading branch information
ClosedPort22 committed Dec 24, 2022
1 parent ec43a39 commit 8135c51
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions gallery_dl/extractor/twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1008,7 +1008,6 @@ def __init__(self, extractor):
self.headers["x-csrf-token"] = csrf_token

self.logged_in = bool(cookies.get("auth_token", domain=cookiedomain))
self.authenticate()

def authenticate(self):
if self.logged_in:
Expand Down Expand Up @@ -1180,17 +1179,22 @@ def _user_id_by_screen_name(self, screen_name):
else:
raise exception.NotFoundError("user")

@cache(maxage=3600)
@cache(maxage=3*3600)
def _guest_token(self):
root = "https://api.twitter.com"
endpoint = "/1.1/guest/activate.json"
return str(self._call(endpoint, None, root, "POST")["guest_token"])
return str(
self._call(endpoint, None, root, False, "POST")["guest_token"])

def _call(self, endpoint, params, root=None, method="GET"):
def _call(self, endpoint, params, root=None, authenticate=True,
method="GET"):
if root is None:
root = self.root

while True:
if authenticate:
self.authenticate()

response = self.extractor.request(
root + endpoint, method=method, params=params,
headers=self.headers, fatal=None)
Expand Down Expand Up @@ -1348,7 +1352,6 @@ def _pagination_tweets(self, endpoint, variables,
blocked = bool(user.get("blocked_by"))
if blocked and self.logged_in and extr.config("logout"):
self.logged_in = False
self.authenticate()
extr.log.info(
"{} blocked your account. Retrying API request as "
"guest".format(user["screen_name"]))
Expand Down

0 comments on commit 8135c51

Please sign in to comment.