Skip to content

Commit 589abd6

Browse files
committed
Parallelize other extraction functions
1 parent efc0339 commit 589abd6

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

twExtract/__init__.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -319,15 +319,13 @@ def extractStatusV2Android(url,workaroundTokens):
319319
twid = m.group(2)
320320
if workaroundTokens == None:
321321
raise TwExtractError(400, "Extract error (no tokens defined)")
322-
# get tweet
323322
tokens = workaroundTokens
324323
random.shuffle(tokens)
325-
for authToken in tokens:
324+
def request_with_token(twid, authToken):
326325
try:
327-
328326
vars = json.loads('{"referrer":"home","includeTweetImpression":true,"includeHasBirdwatchNotes":false,"isReaderMode":false,"includeEditPerspective":false,"includeEditControl":true,"focalTweetId":0,"includeCommunityTweetRelationship":true,"includeTweetVisibilityNudge":true}')
329327
vars['focalTweetId'] = int(twid)
330-
tweet = twitterApiGet(f"https://x.com/i/api/graphql/{androidGraphql_api}/ConversationTimelineV2?variables={urllib.parse.quote(json.dumps(vars))}&features={urllib.parse.quote(androidGraphqlFeatures)}", authToken=authToken)
328+
tweet = twitterApiGet(f"https://x.com/i/api/graphql/{androidGraphql_api}/ConversationTimelineV2?variables={urllib.parse.quote(json.dumps(vars))}&features={urllib.parse.quote(androidGraphqlFeatures)}", authToken=authToken,btoken=androidBearer)
331329
try:
332330
rateLimitRemaining = tweet.headers.get("x-rate-limit-remaining")
333331
print(f"Twitter Android Token Rate limit remaining: {rateLimitRemaining}")
@@ -336,13 +334,13 @@ def extractStatusV2Android(url,workaroundTokens):
336334
if tweet.status_code == 429:
337335
print("Rate limit reached for android token")
338336
# try another token
339-
continue
337+
raise TwExtractError(400, "Extract error: rate limit reached")
340338
output = tweet.json()
341339

342340
if "errors" in output:
343341
print(f"Error in output: {json.dumps(output['errors'])}")
344342
# try another token
345-
continue
343+
raise TwExtractError(400, "Extract error: errors in output - "+json.dumps(output['errors']))
346344
entries = None
347345
for instruction in output['data']['timeline_response']['instructions']:
348346
if instruction["__typename"] == "TimelineAddEntries":
@@ -366,11 +364,11 @@ def extractStatusV2Android(url,workaroundTokens):
366364
print("Tweet 404")
367365
return {'error':'Tweet not found (404); May be due to invalid tweet, changes in Twitter\'s API, or a protected account.'}
368366
except Exception as e:
369-
print(f"Exception in extractStatusV2: {str(e)}")
370-
continue
367+
print(f"Exception in extractStatusV2Android: {str(e)}")
368+
raise TwExtractError(400, "Extract error")
371369

372370
return tweet
373-
raise TwExtractError(400, "Extract error")
371+
return parallel_token_request(twid, tokens, request_with_token)
374372

375373
def extractStatusV2TweetDetail(url,workaroundTokens):
376374
# get tweet ID
@@ -383,7 +381,7 @@ def extractStatusV2TweetDetail(url,workaroundTokens):
383381
# get tweet
384382
tokens = workaroundTokens
385383
random.shuffle(tokens)
386-
for authToken in tokens:
384+
def request_with_token(twid, authToken):
387385
try:
388386
vars = json.loads('{"focalTweetId":"0","with_rux_injections":false,"includePromotedContent":true,"withCommunity":true,"withQuickPromoteEligibilityTweetFields":true,"withBirdwatchNotes":true,"withVoice":true,"withV2Timeline":true}')
389387
vars['focalTweetId'] = str(twid)
@@ -396,13 +394,13 @@ def extractStatusV2TweetDetail(url,workaroundTokens):
396394
if tweet.status_code == 429:
397395
print("Rate limit reached for token")
398396
# try another token
399-
continue
397+
raise TwExtractError(400, "Extract error: rate limit reached")
400398
output = tweet.json()
401399

402400
if "errors" in output:
403401
print(f"Error in output: {json.dumps(output['errors'])}")
404402
# try another token
405-
continue
403+
raise TwExtractError(400, "Extract error: errors in output - "+json.dumps(output['errors']))
406404
entries = None
407405
for instruction in output['data']['threaded_conversation_with_injections_v2']['instructions']:
408406
if instruction["type"] == "TimelineAddEntries":
@@ -427,10 +425,10 @@ def extractStatusV2TweetDetail(url,workaroundTokens):
427425
return {'error':'Tweet not found (404); May be due to invalid tweet, changes in Twitter\'s API, or a protected account.'}
428426
except Exception as e:
429427
print(f"Exception in extractStatusV2: {str(e)}")
430-
continue
428+
raise TwExtractError(400, "Extract error")
431429

432430
return tweet
433-
raise TwExtractError(400, "Extract error")
431+
return parallel_token_request(twid, tokens, request_with_token)
434432

435433
def extractStatusV2Anon(url,x):
436434
# get tweet ID
@@ -492,7 +490,7 @@ def fixTweetData(tweet):
492490
return tweet
493491

494492
def extractStatus(url,workaroundTokens=None):
495-
methods=[extractStatusV2Anon,extractStatusV2,extractStatusV2TweetDetail,extractStatusV2Android]
493+
methods=[extractStatusV2Anon,extractStatusV2,extractStatusV2Android,extractStatusV2TweetDetail]
496494
for method in methods:
497495
try:
498496
result = method(url,workaroundTokens)

0 commit comments

Comments
 (0)