-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtwttrapi.py
271 lines (234 loc) · 8.3 KB
/
twttrapi.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import requests
class TwttrAPIClient:
def __init__(self, api_key, session="", proxy=""):
self.api_key = api_key
self.base_url = "https://twttrapi.p.rapidapi.com/"
self.twttr_session = session
self.twttr_proxy = proxy
def _get_headers(self):
return {
"twttr-session": self.twttr_session,
"twttr-proxy": self.twttr_proxy,
"x-rapidapi-host": "twttrapi.p.rapidapi.com",
"x-rapidapi-key": self.api_key,
"content-type": "application/json",
}
def _send_request(self, method, endpoint, params=None, data=None):
url = self.base_url + endpoint
headers = self._get_headers()
if method == "POST":
headers["content-type"] = "application/x-www-form-urlencoded"
response = requests.request(method, url, headers=headers, params=params, data=data)
response.raise_for_status()
return response.json()
def get_tweet(self, tweet_id):
endpoint = "get-tweet"
params = {
"tweet_id": tweet_id
}
return self._send_request("GET", endpoint, params)
def get_tweet_conversation(self, tweet_id):
endpoint = "get-tweet-conversation"
params = {
"tweet_id": tweet_id
}
return self._send_request("GET", endpoint, params)
def user_tweets(self, username=None, user_id=None, cursor=None):
endpoint = "user-tweets"
params = {
"username": username,
"user_id": user_id,
"cursor": cursor
}
return self._send_request("GET", endpoint, params)
def user_media(self, username=None, user_id=None, cursor=None):
endpoint = "user-media"
params = {
"username": username,
"user_id": user_id,
"cursor": cursor
}
return self._send_request("GET", endpoint, params)
def user_likes(self, username=None, user_id=None, cursor=None):
endpoint = "user-likes"
params = {
"username": username,
"user_id": user_id,
"cursor": cursor
}
return self._send_request("GET", endpoint, params)
def for_you_timeline(self, cursor=None):
endpoint = "for-you-timeline"
params = {
"cursor": cursor
}
return self._send_request("GET", endpoint, params)
def following_timeline(self, cursor=None):
endpoint = "following-timeline"
params = {
"cursor": cursor
}
return self._send_request("GET", endpoint, params)
def create_tweet(self, tweet_text, attachment_url=None, in_reply_to_tweet_id=None, media_id=None):
endpoint = "create-tweet"
data = {
"tweet_text": tweet_text,
"attachment_url": attachment_url,
"in_reply_to_tweet_id": in_reply_to_tweet_id,
"media_id": media_id
}
return self._send_request("POST", endpoint, data=data)
def delete_tweet(self, tweet_id):
endpoint = "delete-tweet"
params = {
"tweet_id": tweet_id
}
return self._send_request("GET", endpoint, params)
def get_user(self, username=None):
endpoint = "get-user"
params = {
"username": username
}
return self._send_request("GET", endpoint, params)
def user_followers(self, username=None, user_id=None, cursor=None):
endpoint = "user-followers"
params = {
"username": username,
"user_id": user_id,
"cursor": cursor
}
return self._send_request("GET", endpoint, params)
def user_following(self, username=None, user_id=None, cursor=None):
endpoint = "user-following"
params = {
"username": username,
"user_id": user_id,
"cursor": cursor
}
return self._send_request("GET", endpoint, params)
def search_suggestions(self, query, cursor=None):
endpoint = "search-suggestions"
params = {
"query": query,
"cursor": cursor
}
return self._send_request("GET", endpoint, params)
def search_top(self, query, cursor=None):
endpoint = "search-top"
params = {
"query": query,
"cursor": cursor
}
return self._send_request("GET", endpoint, params)
def search_latest(self, query, cursor=None):
endpoint = "search-latest"
params = {
"query": query,
"cursor": cursor
}
return self._send_request("GET", endpoint, params)
def search_users(self, query, cursor=None):
endpoint = "search-users"
params = {
"query": query,
"cursor": cursor
}
return self._send_request("GET", endpoint, params)
def search_images(self, query, cursor=None):
endpoint = "search-images"
params = {
"query": query,
"cursor": cursor
}
return self._send_request("GET", endpoint, params)
def search_videos(self, query, cursor=None):
endpoint = "search-videos"
params = {
"query": query,
"cursor": cursor
}
return self._send_request("GET", endpoint, params)
def login_email_username(self, username_or_email, password):
endpoint = "login-email-username"
data = {
"username_or_email": username_or_email,
"password": password
}
return self._send_request("POST", endpoint, data=data)
def login_2fa(self, login_data, response):
endpoint = "login-2fa"
data = {
"login_data": login_data,
"response": response
}
return self._send_request("POST", endpoint, data=data)
def logout(self):
endpoint = "logout"
return self._send_request("POST", endpoint)
def follow_user(self, username=None, user_id=None):
endpoint = "follow-user"
data = {
"username": username,
"user_id": user_id
}
return self._send_request("POST", endpoint, data=data)
def unfollow_user(self, username=None, user_id=None):
endpoint = "unfollow-user"
data = {
"username": username,
"user_id": user_id
}
return self._send_request("POST", endpoint, data=data)
def favorite_tweet(self, tweet_id):
endpoint = "favorite-tweet"
data = {
"tweet_id": tweet_id
}
return self._send_request("POST", endpoint, data=data)
def unfavorite_tweet(self, tweet_id):
endpoint = "unfavorite-tweet"
data = {
"tweet_id": tweet_id
}
return self._send_request("POST", endpoint, data=data)
def retweet_tweet(self, tweet_id):
endpoint = "retweet-tweet"
data = {
"tweet_id": tweet_id
}
return self._send_request("POST", endpoint, data=data)
def unretweet_tweet(self, tweet_id):
endpoint = "unretweet-tweet"
data = {
"tweet_id": tweet_id
}
return self._send_request("POST", endpoint, data=data)
def get_dm_conversations(self, cursor=None):
endpoint = "get-dm-conversations"
params = {
"cursor": cursor
}
return self._send_request("GET", endpoint, params)
def get_dm_conversation(self, username=None, user_id=None, cursor=None):
endpoint = "get-dm-conversation"
params = {
"username": username,
"user_id": user_id,
"cursor": cursor
}
return self._send_request("GET", endpoint, params)
def send_dm(self, message, to_user_name=None, to_user_id=None, media_id=None):
endpoint = "send-dm"
data = {
"message": message,
"to_user_name": to_user_name,
"to_user_id": to_user_id,
"media_id": media_id
}
return self._send_request("POST", endpoint, data=data)
def upload_image(self, image_url):
endpoint = "upload-image"
data = {
"image_url": image_url
}
return self._send_request("POST", endpoint, data=data)