From faaf1906573a362d1a150793e0bfaabf13a81d0f Mon Sep 17 00:00:00 2001 From: Dylan Date: Thu, 15 Jun 2023 09:37:04 +0100 Subject: [PATCH] Quick token cycle fix --- twExtract/__init__.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/twExtract/__init__.py b/twExtract/__init__.py index 8229581..441ff5e 100644 --- a/twExtract/__init__.py +++ b/twExtract/__init__.py @@ -28,16 +28,19 @@ def extractStatus_fallback(url): raise twExtractError.TwExtractError(400, "Extract error") twid = m.group(2) # get tweet - - authToken=random.choice(config["config"]["workaroundTokens"].split(",")) - csrfToken=str(uuid.uuid4()).replace('-', '') - tweet = requests.get("https://api.twitter.com/1.1/statuses/show/" + twid + ".json?tweet_mode=extended&cards_platform=Web-12&include_cards=1&include_reply_count=1&include_user_entities=0", headers={"Authorization":bearer,"Cookie":f"auth_token={authToken}; ct0={csrfToken}; ","x-twitter-active-user":"yes","x-twitter-auth-type":"OAuth2Session","x-twitter-client-language":"en","x-csrf-token":csrfToken,"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/116.0"}) - output = tweet.json() - if "errors" in output: - # pick the first error and create a twExtractError - error = output["errors"][0] - raise twExtractError.TwExtractError(error["code"], error["message"]) - return output + tokens = config["config"]["workaroundTokens"].split(",") + for authToken in tokens: + try: + csrfToken=str(uuid.uuid4()).replace('-', '') + tweet = requests.get("https://api.twitter.com/1.1/statuses/show/" + twid + ".json?tweet_mode=extended&cards_platform=Web-12&include_cards=1&include_reply_count=1&include_user_entities=0", headers={"Authorization":bearer,"Cookie":f"auth_token={authToken}; ct0={csrfToken}; ","x-twitter-active-user":"yes","x-twitter-auth-type":"OAuth2Session","x-twitter-client-language":"en","x-csrf-token":csrfToken,"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/116.0"}) + output = tweet.json() + if "errors" in output: + # try another token + continue + except Exception as e: + continue + return output + raise twExtractError.TwExtractError(400, "Extract error") except Exception as e: raise twExtractError.TwExtractError(400, "Extract error")