api_graph.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import json
  2. base_url = "https://twitter.com/i/api/graphql/"
  3. def gen_features_base():
  4. return {
  5. "responsive_web_graphql_exclude_directive_enabled": True,
  6. "blue_business_profile_image_shape_enabled": False,
  7. "verified_phone_label_enabled": False,
  8. "responsive_web_graphql_skip_user_profile_image_extensions_enabled": False, # no effect found
  9. "responsive_web_graphql_timeline_navigation_enabled": False # no effect found
  10. }
  11. def gen_features_base_ext():
  12. return gen_features_base() | {
  13. "longform_notetweets_consumption_enabled": True,
  14. "view_counts_everywhere_api_enabled": True,
  15. "graphql_is_translatable_rweb_tweet_is_translatable_enabled" : False,
  16. "tweet_awards_web_tipping_enabled" : False,
  17. "longform_notetweets_rich_text_read_enabled": False, #TODO consider implementing. Example tweet id: 1649150616063602693
  18. # this "duplicate" is required for the user_by_screen_name endpoint
  19. "longform_notetweets_richtext_consumption_enabled": False, # TODO consider implementing. Example tweet id: 1649150616063602693
  20. "tweetypie_unmention_optimization_enabled": True, # False = unmention_info, True = unmention_data
  21. "responsive_web_edit_tweet_api_enabled" : False, # edits on twitter?
  22. "freedom_of_speech_not_reach_fetch_enabled" : False, # no effect found
  23. "vibe_api_enabled" : False, # no effect found
  24. "standardized_nudges_misinfo": False, # no effect found
  25. "tweet_with_visibility_results_prefer_gql_limited_actions_policy_enabled" : False, # no effect found
  26. "interactive_text_enabled": False, # no effect found
  27. "responsive_web_enhance_cards_enabled" : False, # no effect found
  28. "responsive_web_text_conversations_enabled" : False, # no effect found
  29. "rweb_lists_timeline_redesign_enabled": False, # no effect found
  30. "creator_subscriptions_tweet_preview_api_enabled": False, # no effect found
  31. "longform_notetweets_inline_media_enabled": True # no effect found
  32. }
  33. def user_by_screen_name(username):
  34. variables = {
  35. "screen_name": username,
  36. }
  37. return {"url": base_url + "k26ASEiniqy4eXMdknTSoQ/UserByScreenName", "params": {"variables": json.dumps(variables), "features": json.dumps(gen_features_base())}}
  38. def gen_paged_params(endpoint, variables, cursor, count):
  39. variables |= {
  40. "count": count,
  41. "includePromotedContent": False, # no effect found
  42. "withVoice" : False # no effect found
  43. }
  44. if cursor is not None:
  45. variables['cursor'] = cursor
  46. features = gen_features_base_ext()
  47. return {"url": base_url + endpoint, "params": {"variables": json.dumps(variables), "features": json.dumps(features)}}
  48. def likes(user_id, cursor=None, count=20):
  49. variables = {
  50. "userId": user_id,
  51. "withDownvotePerspective": False,
  52. "withReactionsMetadata": False, # no effect found
  53. "withReactionsPerspective": False, # no effect found
  54. }
  55. return gen_paged_params("fN4-E0MjFJ9Cn7IYConL7g/Likes", variables, cursor, count)
  56. def user_tweets(user_id, cursor=None, count=40):
  57. variables = {
  58. "userId" : user_id
  59. }
  60. return gen_paged_params("CdG2Vuc1v6F5JyEngGpxVw/UserTweets", variables, cursor, count)
  61. def tweet_detail(tweet_id, cursor=None, count=20):
  62. variables = {
  63. "focalTweetId": tweet_id,
  64. "withBirdwatchNotes": False
  65. }
  66. return gen_paged_params("BbCrSoXIR7z93lLCVFlQ2Q/TweetDetail", variables, cursor, count)
  67. def search(query, cursor=None, count=20):
  68. variables = {
  69. "rawQuery": query,
  70. "product": "Latest"
  71. }
  72. return gen_paged_params("gkjsKepM6gl_HmFWoWKfgg/SearchTimeline", variables, cursor, count)
  73. def favoriters(tweet_id, cursor=None, count=20):
  74. variables = {
  75. "tweetId" : tweet_id
  76. }
  77. return gen_paged_params("mDc_nU8xGv0cLRWtTaIEug/Favoriters", variables, cursor, count)
  78. def retweeters(tweet_id, cursor=None, count=20):
  79. variables = {
  80. "tweetId" : tweet_id
  81. }
  82. return gen_paged_params("RCR9gqwYD1NEgi9FWzA50A/Retweeters", variables, cursor, count)
  83. def following(user_id, cursor=None, count=20):
  84. variables = {
  85. "userId" : user_id
  86. }
  87. return gen_paged_params("JPZiqKjET7_M1r5Tlr8pyA/Following", variables, cursor, count)
  88. def followers(user_id, cursor=None, count=20):
  89. variables = {
  90. "userId" : user_id
  91. }
  92. return gen_paged_params("EAqBhgcGr_qPOzhS4Q3scQ/Followers", variables, cursor, count)