-
Notifications
You must be signed in to change notification settings - Fork 0
/
twittee_handle.py
462 lines (357 loc) · 16.3 KB
/
twittee_handle.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
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
import datetime
from multiprocessing.connection import answer_challenge
import requests
import json
import urllib3
import certifi
http = urllib3.PoolManager(ca_certs=certifi.where())
payload = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:98.0) Gecko/20100101 Firefox/98.0'}
import os
from requests.sessions import InvalidSchema
import time
from geopy.geocoders import Nominatim
from pprint import pprint
# import pprint
# pp = pprint.PrettyPrinter(indent=4)
import csv
import re
from dotenv import load_dotenv
load_dotenv()
bearer_token = os.getenv("TOKEN")
list_blacklist = []
error_list = []
# try:
# with open('/Users/nicolas/Python/indeXee/data/blacklist_domain.csv', 'r', newline='', encoding='UTF-8') as h:
# reader = csv.reader(h, delimiter=",")
# data = list(reader)
# for row in data:
# list_blacklist.append(row[0])
# except:
# pass
def search_specific_handle(username,daylimit=7,keywords=[],blacklist=[],whitelist=[],limit = 10):
limit = limit - 10
DEBUG_MODE = False
final_data = []
# Warn user if keyword argument is not a list #
if type(keywords) != list:
return "Function's keyword argument should be a list."
# ------------------------------------------- #
#Setting variables for search_specific_handle()
search_url = "https://api.twitter.com/2/users/by/username/"
headers = {"Authorization": "Bearer {}".format(bearer_token)}
search_url += username
# -------------------------------------------- #
# Sending the request and recieving an endpoint json response
response = requests.request("GET", search_url, headers = headers)
endpoint_response = response.json()
# print(f"\nendpoint_response for {username}:")
# pp.pprint(endpoint_response)
# ---------------------- --------------------------------------
if DEBUG_MODE:
print(endpoint_response)
# Set the user_id and set a search_url for that id's tweets
try:
user_id = response.json()["data"]["id"]
if DEBUG_MODE:
print(user_id)
except:
print("Handle does not exist")
return None
search_url = "https://api.twitter.com/2/users/{}/tweets".format(user_id)
#Setting the timedelta
# Crate a date/time object that is acceptable by twitter API to use start_time
now = datetime.datetime.now()
d = datetime.timedelta(days = daylimit)
a = now - d
a = str(a).replace(" ","T")
match = re.findall(pattern="\.\d+",string=a)[0]
a = a.replace(match,"") + "Z"
# --------------------------------------------------------------------------- #
if DEBUG_MODE:
print(a)
returned = 0
# Query for the twitter API request
query_params = {
'max_results': '10',
'tweet.fields': 'text,created_at,id,lang,entities',
'expansions': 'author_id',
'user.fields': 'id,name,username,entities,url',
'start_time':f'{a}',
}
# --------------------------------- #
# Send the request using search_url, parameters and headers set above and recieve a json response from endpoint.
response = requests.request("GET", search_url,params=query_params, headers = headers)
endpoint_response = response.json()
# --------------------------------------------------------------------
if DEBUG_MODE:
print(endpoint_response)
with open('endpoint_response.json','w') as fh:
fh.write(json.dumps(endpoint_response,indent=2))
print("Waiting for input before processing endpoint_response")
input()
# --------------------------------------------------------------------
if 'meta' in endpoint_response:
if 'result_count' in endpoint_response['meta']:
if endpoint_response['meta']['result_count'] == 0:
print('No tweet returned from this handle.')
return []
for tweet in endpoint_response['data']:
# TO REVIEW TWEET RESPONSE
# print(f"\ntweet:")
# pp.pprint(endpoint_response)
processed_tweet = {
'username': '{}'.format(username),
'created_at': '{}'.format(tweet['created_at']),
'text': '{}'.format(tweet['text']),
'lang': '{}'.format(tweet['lang']),
'author_url': '{}'.format("https://twitter.com/"+username),
'urls': [],
'author_id': user_id,
'tweet_id': tweet['id'],
'tweet_url': f"https://twitter.com/{username}/status/{tweet['id']}",
}
#Author_webiste_url
if 'entities' in endpoint_response['includes']['users'][0]:
if 'url' in endpoint_response['includes']['users'][0]['entities']:
processed_tweet['author_website_url'] = endpoint_response['includes']['users'][0]['entities']['url']['urls'][0]['expanded_url']
else:
processed_tweet['author_website_url'] = None
else:
processed_tweet['author_website_url'] = None
if DEBUG_MODE:
print('author webiste url ==> ',processed_tweet['author_website_url'])
#Entities (urls)
final_urls = []
if 'entities' in tweet:
for key in tweet['entities']:
if key != 'urls':
continue
else:
for url in tweet['entities']['urls']:
if 'twitter.com' not in url['expanded_url']:
# Try to get actual_url from shoretened_url
try:
shortened_url = url['expanded_url']
print("Gathering actual url from shortened url...")
time.sleep(0.1)
r = requests.get(shortened_url, headers={"User-Agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36"})
actual_url = r.url
print(shortened_url[0:25]+"..."," >>> ", actual_url[0:25]+"...","\n")
final_urls.append(actual_url)
# -----------------------------------------
# If cant, use another method, if that fails too return the shoretened url
except:
try:
time.sleep(0.1)
actual_url = requests.get(shortened_url).url
print(shortened_url[0:25]+"..."," >>> ", actual_url[0:25]+"...","\n")
final_urls.append(actual_url)
except:
print("Couldn't convert:", str(url), " skipping..\n")
final_urls.append(url)
# ----------------------------------
# Blacklist and whitelist !!
for link in final_urls[:]:
skip_url_w = False
skip_url_b = False
for item in blacklist:
if item in link:
skip_url_b = True
break
for item in whitelist:
if item not in link:
skip_url_w = True
break
if skip_url_b or skip_url_w:
final_urls.remove(link)
if final_urls != []:
processed_tweet['urls'] = final_urls
all_keywords_found = True
for keyword in keywords:
if keyword in processed_tweet['text']:
continue
else:
all_keywords_found = False
break
if all_keywords_found:
final_data.append(processed_tweet)
returned += 1
if 'meta' in endpoint_response:
if DEBUG_MODE:
print('\nFound META!\n')
if 'next_token' in endpoint_response['meta']:
if DEBUG_MODE:
print('\nFound next_token!\n')
next_token = endpoint_response['meta']['next_token']
token_found = True
else:
token_found = False
else:
token_found= False
print('Processed first page got {} tweet'.format(returned))
while token_found:
if limit != -10:
if returned >= limit:
token_found = False
break
found_this_step = 0
#Setting the timedelta
# Crate a date/time object that is acceptable by twitter API to use start_time
now = datetime.datetime.now()
d = datetime.timedelta(days = daylimit)
a = now - d
a = str(a).replace(" ","T")
match = re.findall(pattern="\.\d+",string=a)[0]
a = a.replace(match,"") + "Z"
# --------------------------------------------------------------------------- #
if DEBUG_MODE:
print(a)
# Query for the twitter API request
query_params = {
'max_results': '10',
'tweet.fields': 'text,created_at,id,lang,entities',
'expansions': 'author_id',
'user.fields': 'id,name,username,entities,url',
'start_time':f'{a}',
'pagination_token': next_token
}
# --------------------------------- #
# Send the request using search_url, parameters and headers set above and recieve a json response from endpoint.
response = requests.request("GET", search_url,params=query_params, headers = headers)
endpoint_response = response.json()
if 'data' not in endpoint_response:
print("No data in next page")
token_found = False
break
# --------------------------------------------------------------------
if DEBUG_MODE:
print(endpoint_response)
with open('endpoint_response.json','w') as fh:
fh.write(json.dumps(endpoint_response,indent=2))
print("Waiting for input before processing endpoint_response")
input()
# --------------------------------------------------------------------
for tweet in endpoint_response['data']:
print(f"\nendpoint_response['data']= {endpoint_response['data']}\n")
processed_tweet = {
'username': '{}'.format(username),
'created_at': '{}'.format(tweet['created_at']),
'text': '{}'.format(tweet['text']),
'lang': '{}'.format(tweet['lang']),
'author_url': '{}'.format("https://twitter.com/"+username),
'entities': {'urls':[]},
'author_id': user_id,
'tweet_id': tweet['id'],
'tweet_url': tweet['tweet_url'],
}
#Author_webiste_url
if 'entities' in endpoint_response['includes']['users'][0]:
if 'url' in endpoint_response['includes']['users'][0]['entities']:
processed_tweet['author_website_url'] = endpoint_response['includes']['users'][0]['entities']['url']['urls'][0]['expanded_url']
else:
processed_tweet['author_website_url'] = None
else:
processed_tweet['author_website_url'] = None
if DEBUG_MODE:
print('author webiste url ==> ',processed_tweet['author_website_url'])
#Entities (urls)
final_urls = []
if 'entities' in tweet:
for key in tweet['entities']:
if key != 'urls':
continue
else:
for url in tweet['entities']['urls']:
if 'twitter.com' not in url['expanded_url']:
# Try to get actual_url from shoretened_url
try:
shortened_url = url['expanded_url']
print("Gathering actual url from shortened url...")
time.sleep(0.1)
r = requests.get(shortened_url, headers={"User-Agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36"})
actual_url = r.url
print(shortened_url[0:25]+"..."," >>> ", actual_url[0:25]+"...","\n")
final_urls.append(actual_url)
# -----------------------------------------
# If cant, use another method, if that fails too return the shoretened url
except:
try:
time.sleep(0.1)
actual_url = requests.get(shortened_url).url
print(shortened_url[0:25]+"..."," >>> ", actual_url[0:25]+"...","\n")
final_urls.append(actual_url)
except:
print("Couldn't convert:", str(url), " skipping..\n")
final_urls.append(url)
# ----------------------------------
# Blacklist and whitelist !!
for link in final_urls[:]:
skip_url_w = False
skip_url_b = False
for item in blacklist:
if item in link:
skip_url_b = True
break
for item in whitelist:
if item not in link:
skip_url_w = True
break
if skip_url_b or skip_url_w:
final_urls.remove(link)
if final_urls != []:
processed_tweet['entities']['urls'] = final_urls
all_keywords_found = True
"""
for keyword in keywords:
if keyword in processed_tweet['text']:
continue
else:
all_keywords_found = False
break
"""
if all_keywords_found:
final_data.append(processed_tweet)
found_this_step += 1
returned += found_this_step
if 'meta' in endpoint_response:
if DEBUG_MODE:
print('\nFound META!\n')
if 'next_token' in endpoint_response['meta'] and 'data' in endpoint_response:
if DEBUG_MODE:
print('\nFound more data with next_token!\n')
next_token = endpoint_response['meta']['next_token']
token_found = True
else:
token_found = False
# --------------------------------------------------------------------
if DEBUG_MODE:
print(final_data)
with open('endpoint_response.json','w') as fh:
fh.write(json.dumps(final_data,indent=2))
# --------------------------------------------------------------------
return final_data
# Original contributor: E Y S
########################################################################################################
if __name__ == '__main__':
import time
start_time = time.time()
import pprint
pp = pprint.PrettyPrinter(indent=4)
print()
print()
print('-------------------------------')
print(f"{os.path.basename(__file__)}")
keywords = []
username = 'McNeese'
tweet_data = search_specific_handle(username,daylimit=7,keywords=keywords,blacklist=[],whitelist=[],limit = 10)
for tweet in tweet_data:
print()
print()
pp.pprint(tweet)
print('-------------------------------')
run_time = round((time.time() - start_time), 1)
if run_time > 60:
print(f'{os.path.basename(__file__)} finished in {run_time/60} minutes.')
else:
print(f'{os.path.basename(__file__)} finished in {run_time}s.')
print()