This repository was archived by the owner on Apr 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwitter.py
More file actions
executable file
·381 lines (312 loc) · 14.4 KB
/
twitter.py
File metadata and controls
executable file
·381 lines (312 loc) · 14.4 KB
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!/usr/bin/env python3
import base64
import time
import requests
from requests_oauthlib import OAuth1
from bs4 import BeautifulSoup
import configparser
import logging
from collections import defaultdict
# Logging parameters
logging.getLogger("urllib3").propagate = True
logging.getLogger("requests").propagate = True
logging.basicConfig(level=logging.INFO)
class TwitterApiError(Exception):
pass
class TwitterApi:
def __init__(self):
'''Class Initialization'''
# Read Credentials
Config = configparser.ConfigParser()
Config.read("credentials.ini")
consumer_key = Config.get("TwitterCredentials", "consumer_key")
consumer_secret = Config.get("TwitterCredentials", "consumer_secret")
access_token = Config.get("TwitterCredentials", "access_token")
access_token_secret = Config.get("TwitterCredentials", "access_token_secret")
self.session = requests.Session()
self.url_counter = defaultdict(int)
self.access_objs = []
self.url = "https://twitter.com"
self.api_base_url = 'https://api.twitter.com/'
self.loginPost = "https://twitter.com/sessions"
self.ads_guest_token = None
self.ads_bearer_auth = 'AAAAAAAAAAAAAAAAAAAAAOLv4AAAAAAAQubRLkVexZO02uKUva6eI9ZHmMY%3D3jfkYEj27hoTzTlXvxRiMg0wSb285GH9h2WfCvEeOh53QyxA5j'
self.user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36'
# Add App Authentication to Access Objects
self.access_objs.append({'type': 'app',
'name': 'app',
'consumer_key': consumer_key,
'consumer_secret': consumer_secret,
'token': self.get_token(consumer_key, consumer_secret),
'rate_limit_remaining': defaultdict(int),
'rate_limit_reset': defaultdict(int)})
def get_token(self, consumer_key: str, consumer_secret: str) -> str:
'''Get API Token'''
key_secret = '{}:{}'.format(consumer_key, consumer_secret).encode('ascii')
b64_encoded_key = base64.b64encode(key_secret).decode('ascii')
base_url = 'https://api.twitter.com/'
auth_url = '{}oauth2/token'.format(base_url)
auth_headers = {'Authorization': 'Basic {}'.format(b64_encoded_key),
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'}
auth_data = {'grant_type': 'client_credentials'}
auth_resp = requests.post(auth_url, headers=auth_headers, data=auth_data)
access_token = auth_resp.json()['access_token']
return access_token
def fetch_login_auth_token(self):
'''Fetch authentication token'''
data = {"session[username_or_email]": self.username.encode('ascii'),
"session[password]": self.password.encode('ascii'),
"scribe_log": "",
"redirect_after_login": "/",
"remember_me": "1"}
with self.session:
resp = self.session.get(self.url)
soup = BeautifulSoup(resp.content, "lxml")
self.auth_token = soup.select_one("input[name=authenticity_token]")["value"]
self.ui_metric = soup.select_one("input[name=ui_metrics]")
# update data, post and you are logged in.
data["authenticity_token"] = self.auth_token
data['ui_metrics'] = self.ui_metric
def fetch_ads_guest_token(self):
'''Fetch a new guest token for the ads library'''
url = "https://api.twitter.com/1.1/guest/activate.json"
headers = {'authorization': 'Bearer {}'.format(self.ads_bearer_auth)}
req = self.make_request(url, headers=headers, type='post', use_auth=False)
self.ads_guest_token = req.json()['guest_token']
def ads_timeline(self, user_id: int, cursor: int = 0) -> dict:
'''Fetch Twitter ad data for a user'''
url = "https://ads.twitter.com/transparency/ads_timeline.json"
params = {}
params['user_id'] = user_id
if self.ads_guest_token is None:
self.fetch_ads_guest_token()
headers = {'x-guest-token': str(self.ads_guest_token), 'authorization': 'Bearer {}'.format(self.ads_bearer_auth)}
r = self.make_request(url, params, headers)
if r.status_code == 403:
self.fetch_ads_guest_token()
r = self.make_request(url, params, headers)
if r.ok:
status_ids = []
data = []
for obj in r.json()['ads']:
status_ids.append(obj['creativeId'])
if status_ids:
tweets = self.statuses_lookup(id=status_ids, map=True)
creativeIds = tweets['id']
for obj in r.json()['ads']:
if obj['creativeId'] in creativeIds:
obj['data'] = creativeIds[obj['creativeId']]
else:
obj['data'] = None
data.append(obj)
return data
def statuses_lookup(self, **kwargs):
'''Fetch individual statuses (up to 100 per call)'''
allowed_params = ['id', 'include_entities', 'trim_user', 'map', 'include_ext_alt_text', 'include_card_uri', 'tweet_mode']
# Set Default parameters
if 'tweet_mode' not in kwargs:
kwargs['tweet_mode'] = 'extended'
# Remove unknown parameters
for k in kwargs.keys():
if k not in allowed_params:
kwargs.pop(k, None)
if len(kwargs['id']) > 100:
raise TwitterApiError("More than 100 ids used for statuses_lookup")
kwargs['id'] = ','.join([str(id) for id in kwargs['id']])
url = '{}1.1/statuses/lookup.json'.format(self.api_base_url)
r = self.make_request(url, params=kwargs)
if r.ok:
return r.json()
def users_lookup(self, **kwargs) -> dict:
'''Fetch user info (up to 100 per call)'''
allowed_params = ['screen_name', 'user_id', 'include_entities', 'tweet_mode']
# Set Default parameters
if 'tweet_mode' not in kwargs:
kwargs['tweet_mode'] = 'extended'
# Remove unknown parameters
for k in kwargs.keys():
if k not in allowed_params:
kwargs.pop(k, None)
for k in ['screen_name', 'user_id']:
if k in kwargs:
kwargs[k] = ','.join(kwargs[k])
url = '{}1.1/users/lookup.json'.format(self.api_base_url)
r = self.make_request(url, params=kwargs)
if r.ok:
return r.json()
def lists_list(self, **kwargs) -> dict:
'''Get list of lists for a user'''
allowed_params = ['user_id', 'screen_name', 'reverse']
# Remove unknown parameters
for k in kwargs.keys():
if k not in allowed_params:
kwargs.pop(k, None)
url = '{}1.1/lists/list.json'.format(self.api_base_url)
r = self.make_request(url, params=kwargs)
if r.ok:
return r.json()
def lists_members(self, **kwargs) -> dict:
'''Get members of a specific list'''
allowed_params = ['list_id', 'slug', 'owner_screen_name', 'owner_id', 'count', 'cursor']
# Set Default parameters
if 'count' not in kwargs:
kwargs['count'] = 5000
# Remove unknown parameters
for k in kwargs.keys():
if k not in allowed_params:
kwargs.pop(k, None)
url = '{}1.1/lists/members.json'.format(self.api_base_url)
r = self.make_request(url, params=kwargs)
if r.ok:
return r.json()
def lists_memberships(self, **kwargs) -> dict:
'''Gets lists where a particular user is a member'''
allowed_params = ['user_id', 'screen_name', 'count', 'cursor', 'filter_to_owned_lists']
# Set Default parameters
if 'count' not in kwargs:
kwargs['count'] = 500
# Remove unknown parameters
for k in kwargs.keys():
if k not in allowed_params:
kwargs.pop(k, None)
url = '{}1.1/lists/memberships.json'.format(self.api_base_url)
r = self.make_request(url, params=kwargs)
if r.ok:
return r.json()
def lists_subscribers(self, **kwargs) -> dict:
'''Gets subscribers to a list'''
allowed_params = ['list_id', 'slug', 'owner_screen_name', 'owner_id', 'count', 'cursor', 'include_entities', 'skip_status']
# Set Default parameters
if 'count' not in kwargs:
kwargs['count'] = 5000
# Remove unknown parameters
for k in kwargs.keys():
if k not in allowed_params:
kwargs.pop(k, None)
url = '{}1.1/lists/subscribers.json'.format(self.api_base_url)
r = self.make_request(url, params=kwargs)
if r.ok:
return r.json()
def lists_statuses(self, **kwargs) -> dict:
'''Gets lists where a particular user is a member'''
allowed_params = ['list_id', 'slug', 'owner_screen_name', 'owner_id', 'since_id', 'max_id', 'count', 'include_entities', 'include_rts']
# Set Default parameters
if 'count' not in kwargs:
kwargs['count'] = 100
# Remove unknown parameters
for k in kwargs.keys():
if k not in allowed_params:
kwargs.pop(k, None)
url = '{}1.1/lists/statuses.json'.format(self.api_base_url)
r = self.make_request(url, params=kwargs)
if r.ok:
return r.json()
def friends_list(self, **kwargs) -> dict:
'''Get friends list for a user'''
allowed_params = ['user_id', 'screen_name', 'cursor', 'count', 'skip_status', 'include_user_entries']
# Set Default parameters
if 'count' not in kwargs:
kwargs['count'] = 200
# Remove unknown parameters
for k in kwargs.keys():
if k not in allowed_params:
kwargs.pop(k, None)
url = '{}1.1/friends/list.json'.format(self.api_base_url)
r = self.make_request(url, params=kwargs)
if r.ok:
return r.json()
def friends_ids(self, **kwargs) -> dict:
'''Get friends ids for a user'''
allowed_params = ['user_id', 'screen_name', 'cursor', 'stringify_ids', 'count']
# Set Default parameters
if 'count' not in kwargs:
kwargs['count'] = 5000
# Remove unknown parameters
for k in kwargs.keys():
if k not in allowed_params:
kwargs.pop(k, None)
url = '{}1.1/friends/ids.json'.format(self.api_base_url)
r = self.make_request(url, params=kwargs)
if r.ok:
return r.json()
def followers_list(self, **kwargs) -> dict:
'''Get followers list for a user'''
allowed_params = ['user_id', 'screen_name', 'cursor', 'count', 'skip_status', 'include_user_entries']
# Set Default parameters
if 'count' not in kwargs:
kwargs['count'] = 200
# Remove unknown parameters
for k in kwargs.keys():
if k not in allowed_params:
kwargs.pop(k, None)
url = '{}1.1/followers/list.json'.format(self.api_base_url)
r = self.make_request(url, params=kwargs)
if r.ok:
return r.json()
def follower_ids(self, **kwargs) -> dict:
'''Get follower ids for a user'''
allowed_params = ['user_id', 'screen_name', 'cursor', 'stringify_ids', 'count']
# Set Default parameters
if 'count' not in kwargs:
kwargs['count'] = 5000
# Remove unknown parameters
for k in kwargs.keys():
if k not in allowed_params:
kwargs.pop(k, None)
url = '{}1.1/followers/ids.json'.format(self.api_base_url)
r = self.make_request(url, params=kwargs)
if r.ok:
return r.json()
def get_access_obj(self, access_objs, url):
while True:
for obj in access_objs:
if obj['rate_limit_remaining'][url] > 0 or obj['rate_limit_reset'][url] < int(time.time()):
return obj
time.sleep(1)
def make_request(self, url: str, params: dict = {}, headers: dict = {}, type: str = 'get', use_auth=True, user_auth=None) -> dict:
'''Helper function for Twitter API calls'''
retries = 0
max_retries = 3
self.url_counter['url'] += 1
if user_auth:
access_obj = self.access_objs[0]
else:
access_obj = self.get_access_obj(self.access_objs, url)
while True:
auth = None
if use_auth:
if 'token' in access_obj:
if 'authorization' not in headers:
headers.update({'authorization': 'Bearer {}'.format(access_obj['token'])})
else:
auth = OAuth1(access_obj['consumer_key'], access_obj['consumer_secret'], access_obj['access_token'], access_obj['access_token_secret'])
if type == 'get':
r = requests.get(url, params=params, headers=headers, auth=auth)
elif type == 'post':
r = requests.post(url, params=params, headers=headers, auth=auth)
status_code = r.status_code
response_headers = r.headers
if 'x-rate-limit-remaining' in response_headers:
rate_limit_remaining = int(response_headers['x-rate-limit-remaining'])
rate_limit_reset = int(response_headers['x-rate-limit-reset'])
access_obj['rate_limit_remaining'][url] = rate_limit_remaining
access_obj['rate_limit_reset'][url] = rate_limit_reset
if status_code == 200:
return r
elif status_code == 429:
access_obj = self.get_access_obj(self.access_objs, url)
retries += 1
logging.warning("Rate limit reached. Sleeping for {} seconds...".format((rate_limit_reset - int(time.time())+1)))
elif status_code == 401:
return None
elif status_code == 403:
logging.warning("Received status error code {} to endpoint {}".format(status_code, url))
return r
elif status_code == 404:
return None
else:
logging.warning("Received status error code {} making call to endpoint {}".format(status_code, url))
retries += 1
time.sleep(retries**2)
if retries > max_retries:
return False