User API working again, with_tweets still broken. #284

This commit is contained in:
Dylan
2025-09-10 16:46:05 +01:00
parent 98196b0e30
commit cbf55e7429
4 changed files with 33 additions and 20 deletions

View File

@@ -57,7 +57,6 @@ def test_api_user():
def test_api_user_suspended():
resp = client.get(testUserSuspended.replace("https://twitter.com","https://api.vxtwitter.com"),headers={"User-Agent":"test"})
jData = resp.get_json()
assert resp.status_code==500
assert 'suspended' in jData["error"]
def test_api_user_private():

View File

@@ -3,6 +3,7 @@ import os
import twExtract
import utils
from vx_testdata import *
import twitfix
def test_twextract_syndicationAPI():
tweet = twExtract.extractStatus_syndication(testMediaTweet,workaroundTokens=tokens)
@@ -42,20 +43,23 @@ def test_twextract_extractV2():
tweet = twExtract.extractStatusV2(testTextTweet,workaroundTokens=tokens)
def test_twextract_UserExtract():
user = twExtract.extractUser(testUser,workaroundTokens=tokens)
rawUserData = twExtract.extractUser(testUser,workaroundTokens=tokens)
user = twitfix.getApiUserResponse(rawUserData)
assert user["screen_name"]=="jack"
assert user["id"]==12
assert user["created_at"] == "Tue Mar 21 20:50:14 +0000 2006"
def test_twextract_UserExtractID():
user = twExtract.extractUser(testUserIDUrl,workaroundTokens=tokens)
rawUserData = twExtract.extractUser(testUserIDUrl,workaroundTokens=tokens)
user = twitfix.getApiUserResponse(rawUserData)
assert user["screen_name"]=="jack"
assert user["id"]==12
assert user["created_at"] == "Tue Mar 21 20:50:14 +0000 2006"
def test_twextract_UserExtractWeirdURLs():
for url in testUserWeirdURLs:
user = twExtract.extractUser(url,workaroundTokens=tokens)
rawUserData = twExtract.extractUser(url,workaroundTokens=tokens)
user = twitfix.getApiUserResponse(rawUserData)
assert user["screen_name"]=="jack"
assert user["id"]==12
assert user["created_at"] == "Tue Mar 21 20:50:14 +0000 2006"

View File

@@ -42,6 +42,9 @@ tweetDetailGraphql_api="e7RKseIxLu7HgkWNKZ6qnw"
tweetFeedGraphqlFeatures='{"rweb_video_screen_enabled":false,"profile_label_improvements_pcf_label_in_post_enabled":true,"rweb_tipjar_consumption_enabled":true,"verified_phone_label_enabled":false,"creator_subscriptions_tweet_preview_api_enabled":true,"responsive_web_graphql_timeline_navigation_enabled":true,"responsive_web_graphql_skip_user_profile_image_extensions_enabled":false,"premium_content_api_read_enabled":false,"communities_web_enable_tweet_community_results_fetch":true,"c9s_tweet_anatomy_moderator_badge_enabled":true,"responsive_web_grok_analyze_button_fetch_trends_enabled":false,"responsive_web_grok_analyze_post_followups_enabled":true,"responsive_web_jetfuel_frame":false,"responsive_web_grok_share_attachment_enabled":true,"articles_preview_enabled":true,"responsive_web_edit_tweet_api_enabled":true,"graphql_is_translatable_rweb_tweet_is_translatable_enabled":true,"view_counts_everywhere_api_enabled":true,"longform_notetweets_consumption_enabled":true,"responsive_web_twitter_article_tweet_consumption_enabled":true,"tweet_awards_web_tipping_enabled":false,"responsive_web_grok_show_grok_translated_post":false,"responsive_web_grok_analysis_button_from_backend":true,"creator_subscriptions_quote_tweet_preview_enabled":false,"freedom_of_speech_not_reach_fetch_enabled":true,"standardized_nudges_misinfo":true,"tweet_with_visibility_results_prefer_gql_limited_actions_policy_enabled":true,"longform_notetweets_rich_text_read_enabled":true,"longform_notetweets_inline_media_enabled":true,"responsive_web_grok_image_annotation_enabled":true,"responsive_web_enhance_cards_enabled":false}'
tweetFeedGraphql_api="OAx9yEcW3JA9bPo63pcYlA"
userByScreenNameGraphqlFeatures='{"hidden_profile_subscriptions_enabled":true,"payments_enabled":false,"profile_label_improvements_pcf_label_in_post_enabled":true,"rweb_tipjar_consumption_enabled":true,"verified_phone_label_enabled":false,"subscriptions_verification_info_is_identity_verified_enabled":true,"subscriptions_verification_info_verified_since_enabled":true,"highlights_tweets_tab_ui_enabled":true,"responsive_web_twitter_article_notes_tab_enabled":true,"subscriptions_feature_can_gift_premium":true,"creator_subscriptions_tweet_preview_api_enabled":true,"responsive_web_graphql_skip_user_profile_image_extensions_enabled":false,"responsive_web_graphql_timeline_navigation_enabled":true}'
userByScreenNameGraphql_api="96tVxbPqMZDoYB5pmzezKA"
twitterUrl = "x.com" # doubt this will change but just in case
class TwExtractError(Exception):
def __init__(self, code, message):
@@ -522,20 +525,26 @@ def extractUser(url,workaroundTokens):
if authToken.startswith("oa|"): # oauth token not supported atm
continue
try:
reqHeaders = getAuthHeaders(bearer,authToken=authToken)
vars=json.loads('{"screen_name":"","withGrokTranslatedBio":false}')
reqHeaders = getAuthHeaders(v2bearer,authToken=authToken)
if not useId:
user = requests.get(f"https://api.{twitterUrl}/1.1/users/show.json?screen_name={screen_name}",headers=reqHeaders)
vars['screen_name'] = screen_name
user = requests.get(f"https://x.com/i/api/graphql/{userByScreenNameGraphql_api}/UserByScreenName",{'variables':json.dumps(vars),'features':userByScreenNameGraphqlFeatures,'fieldToggles':'{"withAuxiliaryUserLabels":true}'},headers=reqHeaders)
else:
user = requests.get(f"https://api.{twitterUrl}/1.1/users/show.json?user_id={screen_name}",headers=reqHeaders)
raise NotImplementedError("User ID method not implemented")
#user = requests.get(f"https://api.{twitterUrl}/1.1/users/show.json?user_id={screen_name}",headers=reqHeaders)
output = user.json()
if "errors" in output:
# pick the first error and create a twExtractError
error = output["errors"][0]
raise TwExtractError(error["code"], error["message"])
elif 'user' not in output['data']:
raise TwExtractError(404, "User not found.")
elif output['data']['user']['result']['__typename'] == 'UserUnavailable':
raise TwExtractError(404, output['data']['user']['result']['message'])
return output
except Exception as e:
if hasattr(e,"msg") and (e.msg == 'User has been suspended.' or e.msg == 'User not found.'):
if hasattr(e,"msg") and ('suspended' in e.msg or e.msg == 'User not found.'):
raise e
continue
raise TwExtractError(400, "Extract error")

View File

@@ -4,18 +4,19 @@ from configHandler import config
from utils import stripEndTCO
def getApiUserResponse(user):
userResult = user["data"]["user"]["result"]
return {
"id": user["id"],
"screen_name": user["screen_name"],
"name": user["name"],
"profile_image_url": user["profile_image_url_https"],
"description": user["description"],
"location": user["location"],
"followers_count": user["followers_count"],
"following_count": user["friends_count"],
"tweet_count": user["statuses_count"],
"created_at": user["created_at"],
"protected": user["protected"],
"id": int(userResult["rest_id"]),
"screen_name": userResult["core"]["screen_name"],
"name": userResult["core"]["name"],
"profile_image_url": userResult['avatar']["image_url"],
"description": userResult["legacy"]["description"],
"location": userResult["location"]["location"],
"followers_count": userResult["legacy"]["followers_count"],
"following_count": userResult["legacy"]["friends_count"],
"tweet_count": userResult["legacy"]["statuses_count"],
"created_at": userResult["core"]["created_at"],
"protected": userResult["privacy"]["protected"],
"fetched_on": int(datetime.now().timestamp()),
}