Skip to content

Commit

Permalink
Refactor: [Server][TwitterRouter] ツイート送信コードを調整
Browse files Browse the repository at this point in the history
  • Loading branch information
tsukumijima committed Sep 15, 2023
1 parent 780965d commit a031584
Showing 1 changed file with 50 additions and 57 deletions.
107 changes: 50 additions & 57 deletions server/app/routers/TwitterRouter.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,74 +373,67 @@ async def TwitterTweetAPI(

# ツイートを送信 (GraphQL API)

cookie_session_user_handler = cast(CookieSessionUserHandler, twitter_account_api.auth)
cookies_dict = cookie_session_user_handler.get_cookies_as_dict()
headers_dict = cookie_session_user_handler.get_graphql_api_headers()

media_entities = []
for media_id in media_ids:
media_entities.append({
"media_id": media_id,
"tagged_users": []
'media_id': media_id,
'tagged_users': []
})

payload = {
"variables": {
"tweet_text": tweet,
"media": {
"media_entities": media_entities,
"possibly_sensitive": False
response = await asyncio.to_thread(requests.post,
url = 'https://twitter.com/i/api/graphql/tTsjMKyhajZvK4q76mpIBg/CreateTweet',
headers = headers_dict,
cookies = cookies_dict,
json = {
'variables': {
'tweet_text': tweet,
'media': {
'media_entities': media_entities,
'possibly_sensitive': False,
},
'withDownvotePerspective': False,
'withReactionsMetadata': False,
'withReactionsPerspective': False,
'withSuperFollowsTweetFields': True,
'withSuperFollowsUserFields': True,
'semantic_annotation_ids': [],
'dark_request': False,
},
"withDownvotePerspective": False,
"withReactionsMetadata": False,
"withReactionsPerspective": False,
"withSuperFollowsTweetFields": True,
"withSuperFollowsUserFields": True,
"semantic_annotation_ids": [],
"dark_request": False
},
"features": {
"tweetypie_unmention_optimization_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": False,
"tweet_awards_web_tipping_enabled": False,
"longform_notetweets_rich_text_read_enabled": True,
"longform_notetweets_inline_media_enabled": True,
"responsive_web_graphql_exclude_directive_enabled": True,
"verified_phone_label_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,
"responsive_web_media_download_video_enabled": False,
"responsive_web_graphql_skip_user_profile_image_extensions_enabled": False,
"responsive_web_graphql_timeline_navigation_enabled": True,
"responsive_web_enhance_cards_enabled": False
},
"fieldToggles": {
"withArticleRichContentState": False,
"withAuxiliaryUserLabels": False
'features': {
'tweetypie_unmention_optimization_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': False,
'tweet_awards_web_tipping_enabled': False,
'longform_notetweets_rich_text_read_enabled': True,
'longform_notetweets_inline_media_enabled': True,
'responsive_web_graphql_exclude_directive_enabled': True,
'verified_phone_label_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,
'responsive_web_media_download_video_enabled': False,
'responsive_web_graphql_skip_user_profile_image_extensions_enabled': False,
'responsive_web_graphql_timeline_navigation_enabled': True,
'responsive_web_enhance_cards_enabled': False,
},
'fieldToggles': {
'withArticleRichContentState': False,
'withAuxiliaryUserLabels': False,
},
'queryId': 'tTsjMKyhajZvK4q76mpIBg',
},
"queryId": "tTsjMKyhajZvK4q76mpIBg"
}

cookie_session_user_handler = cast(CookieSessionUserHandler, twitter_account_api.auth)
cookies = cookie_session_user_handler.get_cookies_as_dict()

headers = cookie_session_user_handler._tweetdeck_api_headers # type: ignore
headers['content-type'] = 'application/json'
headers['cookie'] = '; '.join([f'{key}={value}' for key, value in cookies.items()])
headers['origin'] = 'https://twitter.com'
headers['referer'] = 'https://twitter.com/'
headers.pop('x-twitter-client-version')

response = requests.post(
url = 'https://twitter.com/i/api/graphql/tTsjMKyhajZvK4q76mpIBg/CreateTweet',
headers = cast(dict[str, str], headers),
json = payload,
)
if response.status_code != 200:
return {
'is_success': False,
'detail': f'Twitter GraphQl API Error (HTTP Error {response.status_code})',
'detail': f'Twitter GraphQL API Error (HTTP Error {response.status_code})',
}

return {
Expand Down

0 comments on commit a031584

Please sign in to comment.