-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
110 lines (91 loc) · 4.38 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
from calendar import c
from urllib import response
from xmlrpc.client import Boolean
import requests, json
from bearer_auth import BearerAuth
from datetime import datetime
GUEST_TOKEN_URL = 'https://api.twitter.com/1.1/guest/activate.json'
TWEETS_COUNT = 300 # I think this is a safe count but you may increase.
class TwitterAPI(object):
def __init__(self, graphql_ubs: str, graphql_ut: str, graphql_td: str, bearer_token: str):
self.bearer_token = bearer_token
self.screen_name_url = 'https://twitter.com/i/api/graphql/%s/UserByScreenName' % graphql_ubs
self.user_tweets_url = 'https://twitter.com/i/api/graphql/%s/UserTweets' % graphql_ut
self.tweet_detail_url = 'https://twitter.com/i/api/graphql/%s/TweetDetail' % graphql_td
self.guest_token = ''
def get_guest_token(self) -> bool:
resp = requests.post(GUEST_TOKEN_URL, auth=BearerAuth(self.bearer_token))
if resp.status_code != 200:
print('[!] failed to retrive guest token: %d' % resp.status_code)
return False
if len(resp.text) <= 0:
print('[!] invalid response retriving guest token: %s' % resp.text)
return False
data = json.loads(resp.text)
if 'guest_token' not in data:
print('[!] invalid response retriving guest token: %s' % resp.text)
return False
self.guest_token = data['guest_token']
return True
def get_user_by_screen_name(self, username: str) -> requests.Response:
variables = '{"screen_name":"%s","withSafetyModeUserFields":false,"withSuperFollowsUserFields":false}' % username
params = {'variables': variables}
resp = requests.get(self.screen_name_url, auth=BearerAuth(self.bearer_token), headers={'x-guest-token': self.guest_token}, params=params)
if resp.status_code == 429:
self.get_guest_token()
return self.get_user_by_screen_name(username)
return resp
def get_tweets(self, rest_id: str, count: int, cursor: str) -> tuple:
remaining_count = 0
if count > TWEETS_COUNT:
remaining_count = count - TWEETS_COUNT
count = TWEETS_COUNT
variables = {
"userId": rest_id,
"count": count,
"includePromotedContent": False,
"withQuickPromoteEligibilityTweetFields": False,
"withSuperFollowsUserFields": False,
"withDownvotePerspective": False,
"withReactionsMetadata": False,
"withReactionsPerspective": False,
"withSuperFollowsTweetFields": True,
"withVoice": True,
"withV2Timeline": False,
"__fs_responsive_web_uc_gql_enabled": False,
"__fs_dont_mention_me_view_api_enabled": False,
"__fs_interactive_text": False
}
if cursor != "":
variables["cursor"] = cursor
params = {'variables': json.dumps(variables)}
resp = requests.get(self.user_tweets_url, auth=BearerAuth(self.bearer_token), headers={'x-guest-token': self.guest_token}, params=params)
if resp.status_code == 429:
self.get_guest_token()
return self.get_tweets(rest_id, count, cursor)
return (resp, remaining_count)
def get_tweet(self, rest_id: str) -> requests.Response:
variables = {
"focalTweetId": rest_id,
"with_rux_injections": False,
"includePromotedContent": False,
"withCommunity": True,
"withQuickPromoteEligibilityTweetFields": False,
"withBirdwatchNotes": False,
"withSuperFollowsUserFields": False,
"withDownvotePerspective": False,
"withReactionsMetadata": False,
"withReactionsPerspective": False,
"withSuperFollowsTweetFields": True,
"withVoice": True,
"withV2Timeline": False,
"__fs_responsive_web_uc_gql_enabled": False,
"__fs_dont_mention_me_view_api_enabled": False,
"__fs_interactive_text": False
}
params = {'variables': json.dumps(variables)}
resp = requests.get(self.tweet_detail_url, auth=BearerAuth(self.bearer_token), headers={'x-guest-token': self.guest_token}, params=params)
if resp.status_code == 429:
self.get_guest_token()
return self.get_tweet(rest_id)
return resp