-
Notifications
You must be signed in to change notification settings - Fork 0
/
capture_tweets.py
794 lines (629 loc) · 29.8 KB
/
capture_tweets.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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
# -*- coding: utf-8
# Reinaldo Chaves ([email protected])
# Script that every day captures tweets from Mexico and Brazil in the Twitter API
# Profiles and keywords widely used in countries are sought to carry out attacks against journalists and environmental activists
#
import os
import tweepy as tw
import pandas as pd
import json
import requests
from datetime import datetime, timedelta
consumer_key = os.environ["TWEEPY_API_KEY"]
consumer_secret = os.environ["TWEEPY_CONSUMER_SECRET"]
access_token = os.environ["TWEEPY_ACCESS_TOKEN"]
access_token_secret = os.environ["TWEEPY_TOKEN_SECRET"]
# use Twitter keys for authentication, via Tweepy
auth = tw.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tw.API(auth, wait_on_rate_limit=True)
#########
# BRAZIL
#########
# search for keywords most likely in Brazil to be used in hate attacks against journalists and heavily attacked environmental profiles
# Each search is limited to 500 characters
new_search = "blogueira OR jornalista burra OR jornalista chata OR jornalista esquerda OR jornalista esquerdista OR jornalista esquerdopata OR jornalista fake OR jornalista feia OR jornalista horrorosa OR jornalista imbecil OR jornalista louca OR jornalista militante OR jornalista puta OR jornalista safada OR jornalista vagabunda -filter:retweets"
# search the last seven days to the current date
now = datetime.now()
dia_hoje = now.strftime("%d")
mes_hoje = now.strftime("%m")
ano_hoje = now.strftime("%Y")
today = ano_hoje + "-" + mes_hoje + "-" + dia_hoje
# 2,000 tweets as a limit
tweets = tw.Cursor(api.search_tweets,
q=new_search,
lang="pt",
until=today).items(2000)
users_locs = [[tweet.id, tweet.created_at, tweet.source, tweet.geo, tweet.coordinates, tweet.entities, tweet.user, tweet.text] for tweet in tweets]
# transform into initial dataframe and do first cleanups
tweet_text = pd.DataFrame(data=users_locs,
columns=['id', 'created_at', 'source', 'geo', 'coordinates', 'entities', 'user', 'text'])
tweet_text['id'] = tweet_text['id'].apply(str)
tweet_text.rename(columns={'id': 'tweet_id'}, inplace=True)
tweet_text['created_at'] = tweet_text['created_at'].apply(lambda a: pd.to_datetime(a).date())
tweet_text['created_at'].map(lambda x: x.isoformat())
# do the final cleanups and find the information of the authors of the tweets
clean_tweets = []
for num, row in tweet_text.iterrows():
tweet_id = row['tweet_id']
created_at = row['created_at']
source = row['source']
geo = row['geo']
coordinates = row['coordinates']
entities = row['entities']
user = row['user']
text = row['text']
try:
expanded_url = entities['urls'][0]['expanded_url']
except:
expanded_url = ""
author_name = user.name[0:]
author_screen_name = user.screen_name[0:]
location = user.location[0:]
dicionario = {"tweet_id": tweet_id,
"created_at": created_at,
"source": source,
"geo": geo,
"coordinates": coordinates,
"expanded_url": expanded_url,
"author_name": author_name,
"author_screen_name": author_screen_name,
"location": location,
"text": text,
"likely_target_of_attack": "journalists"
}
clean_tweets.append(dicionario)
df_clean_tweets1 = pd.DataFrame(clean_tweets)
#df_clean_tweets.info()
#df_clean_tweets.to_excel('test.xlsx',sheet_name='Sheet1',index=False)
# More cases related to environmental activists
new_search = "indígena militante OR ambientalista antipatriota OR ambientalista ladrão OR ambientalista corrupto OR indios mal informados OR índio de butique OR índia de butique OR índio antipatriota OR índia antipatriota OR ambientalista antipatriota -filter:retweets"
# 2,000 tweets as a limit
tweets = tw.Cursor(api.search_tweets,
q=new_search,
lang="pt",
until=today).items(2000)
users_locs = [[tweet.id, tweet.created_at, tweet.source, tweet.geo, tweet.coordinates, tweet.entities, tweet.user, tweet.text] for tweet in tweets]
# transform into initial dataframe and do first cleanups
tweet_text_e = pd.DataFrame(data=users_locs,
columns=['id', 'created_at', 'source', 'geo', 'coordinates', 'entities', 'user', 'text'])
tweet_text_e['id'] = tweet_text_e['id'].apply(str)
tweet_text_e.rename(columns={'id': 'tweet_id'}, inplace=True)
tweet_text_e['created_at'] = tweet_text_e['created_at'].apply(lambda a: pd.to_datetime(a).date())
tweet_text_e['created_at'].map(lambda x: x.isoformat())
# do the final cleanups and find the information of the authors of the tweets
clean_tweets = []
for num, row in tweet_text_e.iterrows():
tweet_id = row['tweet_id']
created_at = row['created_at']
source = row['source']
geo = row['geo']
coordinates = row['coordinates']
entities = row['entities']
user = row['user']
text = row['text']
try:
expanded_url = entities['urls'][0]['expanded_url']
except:
expanded_url = ""
author_name = user.name[0:]
author_screen_name = user.screen_name[0:]
location = user.location[0:]
dicionario = {"tweet_id": tweet_id,
"created_at": created_at,
"source": source,
"geo": geo,
"coordinates": coordinates,
"expanded_url": expanded_url,
"author_name": author_name,
"author_screen_name": author_screen_name,
"location": location,
"text": text,
"likely_target_of_attack": 'environmental activists'
}
clean_tweets.append(dicionario)
df_clean_tweets2 = pd.DataFrame(clean_tweets)
#df_clean_tweets_e.info()
frames = [df_clean_tweets1, df_clean_tweets2]
df_clean_tweets_portuguese = pd.concat(frames)
df_clean_tweets_portuguese = df_clean_tweets_portuguese.drop_duplicates(
subset = ['author_name', 'text'],
keep = 'last').reset_index(drop = True)
df_clean_tweets_portuguese.info()
# Airtable keys
# https://airtable.com/api
AIRTABLE_BASE_ID_P = os.environ["AIRTABLE_BASE_ID_P"]
AIRTABLE_TABLE_NAME = os.environ["AIRTABLE_TABLE_NAME"]
AIRTABLE_API_KEY = os.environ["AIRTABLE_API_KEY"]
ENDPOINT = f'https://api.airtable.com/v0/{AIRTABLE_BASE_ID_P}/{AIRTABLE_TABLE_NAME}'
headers = {
"Authorization": f"Bearer {AIRTABLE_API_KEY}",
"Content-Type": "application/json"
}
# Collects the tweets that are already stored in the Airtable table
params = ()
airtable_records = []
run = True
while run is True:
response = requests.get(ENDPOINT, params=params, headers=headers)
airtable_response = response.json()
airtable_records += (airtable_response['records'])
if 'offset' in airtable_response:
run = True
params = (('offset', airtable_response['offset']),)
else:
run = False
airtable_rows = []
airtable_index = []
for record in airtable_records:
airtable_rows.append(record['fields'])
airtable_index.append(record['id'])
df_current_data = pd.DataFrame(airtable_rows, index=airtable_index)
# Unites current Tweepy scrape and stored data
# Eliminate duplicate Tweet IDs and ['author_name', 'text']
all_data = pd.merge(df_clean_tweets_portuguese, \
df_current_data, \
how = 'left',
left_on=['author_name','text'], \
right_on=['author_name','text'])
all_data.info()
all_data = all_data[["coordinates_x", "tweet_id_x", "expanded_url_x", "source_x", "created_at_x", "author_screen_name_x", "author_name", "location_x", "likely_target_of_attack_x", "text", "geo_x"]]
all_data.rename(columns = {'coordinates_x':'coordinates'},inplace = True)
all_data.rename(columns = {'tweet_id_x':'tweet_id'},inplace = True)
all_data.rename(columns = {'expanded_url_x':'expanded_url'},inplace = True)
all_data.rename(columns = {'source_x':'source'},inplace = True)
all_data.rename(columns = {'created_at_x':'created_at'},inplace = True)
all_data.rename(columns = {'author_screen_name_x':'author_screen_name'},inplace = True)
all_data.rename(columns = {'location_x':'location'},inplace = True)
all_data.rename(columns = {'likely_target_of_attack_x':'likely_target_of_attack'},inplace = True)
all_data.rename(columns = {'geo_x':'geo'},inplace = True)
all_data['tweet_id'] = all_data['tweet_id'].str.strip()
#all_data.shape
df_tweets_final = all_data.drop_duplicates(
subset = ['author_name', 'text'],
keep = 'last').reset_index(drop = True)
df_tweets_final.info()
# function to add new data to airtable
def add_to_airtable(tweet_id, created_at, source, geo, coordinates, expanded_url, author_name, author_screen_name, location,text, likely_target_of_attack):
data = {
"records": [
{
"fields": {
"tweet_id": str(tweet_id),
"created_at": str(created_at),
"source": str(source),
"geo": str(geo),
"coordinates": str(coordinates),
"expanded_url": str(expanded_url),
"author_name": str(author_name),
"author_screen_name": str(author_screen_name),
"location": str(location),
"text": text,
"likely_target_of_attack": str(likely_target_of_attack)
}
}
]
}
data = json.dumps(data, indent=4, sort_keys=False, default=str)
#print(data)
try:
r = requests.request("POST", ENDPOINT, headers=headers, data=data)
except requests.exceptions.HTTPError as err:
raise SystemExit(err)
#print(f"Response status code: {r.status_code}")
#if r.status_code != 200:
# print(f"Response status code: {r.status_code}")
return r.status_code
# test for new data
size = len(df_tweets_final.index)
if size != 0:
for num, row in df_tweets_final.iterrows():
tweet_id = row['tweet_id']
if tweet_id is None or tweet_id == "":
tweet_id = "nan"
created_at = row['created_at']
if created_at is None or created_at == "":
created_at = "nan"
source = row['source']
if source is None or source == "":
source = "nan"
geo = row['geo']
if geo is None or geo == "":
geo = "nan"
coordinates = row['coordinates']
if coordinates is None or coordinates == "":
coordinates = "nan"
expanded_url = row['expanded_url']
if expanded_url is None or expanded_url == "":
expanded_url = "nan"
author_name = row['author_name']
if author_name is None or author_name == "":
author_name = "nan"
author_screen_name = row['author_screen_name']
if author_screen_name is None or author_screen_name == "":
author_screen_name = "nan"
location = row['location']
if location is None or location == "":
location = "nan"
text = row['text']
if text is None or text == "":
text = "nan"
likely_target_of_attack = row['likely_target_of_attack']
if likely_target_of_attack is None or likely_target_of_attack == "":
likely_target_of_attack = "nan"
add_to_airtable(tweet_id, created_at, source, geo, coordinates, expanded_url, author_name, author_screen_name, location,text,likely_target_of_attack)
# MEXICO
# 1 - search for keywords most likely in Mexico
# Each search is limited to 500 characters
new_search = "to:AristeguiOnline OR to:aristeguicnn OR to:DeniseDresserG OR to:lumendoz OR to:Fridaguerrera OR to:rynram OR to:CarlosLoret OR to:epigmenioibarra OR periodista tonta OR periodista tonto OR periodista pendeja OR periodista pendejo OR periodista tarada OR periodista tarado OR periodista estúpida OR periodista estupida OR periodista estúpido OR periodista estupido -filter:retweets"
tweets = tw.Cursor(api.search_tweets,
q=new_search,
lang="es",
until=today).items(2000)
users_locs = [[tweet.id, tweet.created_at, tweet.source, tweet.geo, tweet.coordinates, tweet.entities, tweet.user, tweet.text] for tweet in tweets]
# transform into initial dataframe and do first cleanups
tweet_text_es_1 = pd.DataFrame(data=users_locs,
columns=['id', 'created_at', 'source', 'geo', 'coordinates', 'entities', 'user', 'text'])
size = len(tweet_text_es_1.index)
if size != 0:
tweet_text_es_1['id'] = tweet_text_es_1['id'].apply(str)
tweet_text_es_1.rename(columns={'id': 'tweet_id'}, inplace=True)
tweet_text_es_1['created_at'] = tweet_text_es_1['created_at'].apply(lambda a: pd.to_datetime(a).date())
tweet_text_es_1['created_at'].map(lambda x: x.isoformat())
# do the final cleanups and find the information of the authors of the tweets
clean_tweets = []
for num, row in tweet_text_es_1.iterrows():
tweet_id = row['tweet_id']
created_at = row['created_at']
source = row['source']
geo = row['geo']
coordinates = row['coordinates']
entities = row['entities']
user = row['user']
text = row['text']
try:
expanded_url = entities['urls'][0]['expanded_url']
except:
expanded_url = ""
author_name = user.name[0:]
author_screen_name = user.screen_name[0:]
location = user.location[0:]
dicionario = {"tweet_id": tweet_id,
"created_at": created_at,
"source": source,
"geo": geo,
"coordinates": coordinates,
"expanded_url": expanded_url,
"author_name": author_name,
"author_screen_name": author_screen_name,
"location": location,
"text": text,
"likely_target_of_attack": "journalists"
}
clean_tweets.append(dicionario)
df_clean_tweets_es_1 = pd.DataFrame(clean_tweets)
else:
df_clean_tweets_es_1 = tweet_text_es_2.copy()
#df_clean_tweets_es_1.info()
#df_clean_tweets.to_excel('test.xlsx',sheet_name='Sheet1',index=False)
# 2
new_search = "periodista bruja OR periodista arpia OR periodista arpía OR periodista burra OR periodista burro OR periodista amargada OR OR periodista amargado OR periodista vieja OR periodista loca OR periodista esta loca OR periodista chiflada OR periodista ridícula OR periodista ridículo OR periodista una perra OR periodista inmunda OR periodista inmundo OR periodista vieja inmunda OR periodista vieja de mierda OR periodista asquerosa OR periodista asqueroso -filter:retweets"
tweets = tw.Cursor(api.search_tweets,
q=new_search,
lang="es",
until=today).items(2000)
users_locs = [[tweet.id, tweet.created_at, tweet.source, tweet.geo, tweet.coordinates, tweet.entities, tweet.user, tweet.text] for tweet in tweets]
# transform into initial dataframe and do first cleanups
tweet_text_es_2 = pd.DataFrame(data=users_locs,
columns=['id', 'created_at', 'source', 'geo', 'coordinates', 'entities', 'user', 'text'])
size = len(tweet_text_es_2.index)
if size != 0:
tweet_text_es_2['id'] = tweet_text_es_2['id'].apply(str)
tweet_text_es_2.rename(columns={'id': 'tweet_id'}, inplace=True)
tweet_text_es_2['created_at'] = tweet_text_es_2['created_at'].apply(lambda a: pd.to_datetime(a).date())
tweet_text_es_2['created_at'].map(lambda x: x.isoformat())
# do the final cleanups and find the information of the authors of the tweets
clean_tweets = []
for num, row in tweet_text_es_2.iterrows():
tweet_id = row['tweet_id']
created_at = row['created_at']
source = row['source']
geo = row['geo']
coordinates = row['coordinates']
entities = row['entities']
user = row['user']
text = row['text']
try:
expanded_url = entities['urls'][0]['expanded_url']
except:
expanded_url = ""
author_name = user.name[0:]
author_screen_name = user.screen_name[0:]
location = user.location[0:]
dicionario = {"tweet_id": tweet_id,
"created_at": created_at,
"source": source,
"geo": geo,
"coordinates": coordinates,
"expanded_url": expanded_url,
"author_name": author_name,
"author_screen_name": author_screen_name,
"location": location,
"text": text,
"likely_target_of_attack": "journalists"
}
clean_tweets.append(dicionario)
df_clean_tweets_es_2 = pd.DataFrame(clean_tweets)
else:
df_clean_tweets_es_2 = tweet_text_es_2.copy()
# 3
new_search = "periodista ve a lavar platos OR periodista mamadora OR periodista zorra OR periodista rubia tarada OR periodista cabrona OR periodista embaucadora OR periodista ladrona OR periodista ladron OR periodista ladrón OR periodista hija de perra OR periodista hija de puta OR periodista hijo de perra OR periodista hijo de puta OR periodista lambiscona OR periodista naca OR periodista lamebotas OR periodista arrastrada OR periodista luchona -filter:retweets"
tweets = tw.Cursor(api.search_tweets,
q=new_search,
lang="es",
until=today).items(2000)
users_locs = [[tweet.id, tweet.created_at, tweet.source, tweet.geo, tweet.coordinates, tweet.entities, tweet.user, tweet.text] for tweet in tweets]
# transform into initial dataframe and do first cleanups
tweet_text_es_3 = pd.DataFrame(data=users_locs,
columns=['id', 'created_at', 'source', 'geo', 'coordinates', 'entities', 'user', 'text'])
size = len(tweet_text_es_3.index)
if size != 0:
tweet_text_es_3['id'] = tweet_text_es_3['id'].apply(str)
tweet_text_es_3.rename(columns={'id': 'tweet_id'}, inplace=True)
tweet_text_es_3['created_at'] = tweet_text_es_3['created_at'].apply(lambda a: pd.to_datetime(a).date())
tweet_text_es_3['created_at'].map(lambda x: x.isoformat())
# do the final cleanups and find the information of the authors of the tweets
clean_tweets = []
for num, row in tweet_text_es_3.iterrows():
tweet_id = row['tweet_id']
created_at = row['created_at']
source = row['source']
geo = row['geo']
coordinates = row['coordinates']
entities = row['entities']
user = row['user']
text = row['text']
try:
expanded_url = entities['urls'][0]['expanded_url']
except:
expanded_url = ""
author_name = user.name[0:]
author_screen_name = user.screen_name[0:]
location = user.location[0:]
dicionario = {"tweet_id": tweet_id,
"created_at": created_at,
"source": source,
"geo": geo,
"coordinates": coordinates,
"expanded_url": expanded_url,
"author_name": author_name,
"author_screen_name": author_screen_name,
"location": location,
"text": text,
"likely_target_of_attack": "journalists"
}
clean_tweets.append(dicionario)
df_clean_tweets_es_3 = pd.DataFrame(clean_tweets)
else:
df_clean_tweets_es_3 = tweet_text_es_3.copy()
# 4
new_search = "periodista pinche gata OR periodista cerda OR periodista marrana OR periodista simia OR periodista vieja fea OR periodista vieja gorda OR periodista gorda OR periodista machorra OR periodista marimacha OR periodista tortillera OR periodista travesti OR periodista desviada OR periodista negra asquerosa OR periodista negra de mierda OR periodista pinche negra OR periodista india OR periodista prieta OR periodista negro de mierda -filter:retweets"
tweets = tw.Cursor(api.search_tweets,
q=new_search,
lang="es",
until=today).items(2000)
users_locs = [[tweet.id, tweet.created_at, tweet.source, tweet.geo, tweet.coordinates, tweet.entities, tweet.user, tweet.text] for tweet in tweets]
# transform into initial dataframe and do first cleanups
tweet_text_es_4 = pd.DataFrame(data=users_locs,
columns=['id', 'created_at', 'source', 'geo', 'coordinates', 'entities', 'user', 'text'])
size = len(tweet_text_es_4.index)
if size != 0:
tweet_text_es_4['id'] = tweet_text_es_4['id'].apply(str)
tweet_text_es_4.rename(columns={'id': 'tweet_id'}, inplace=True)
tweet_text_es_4['created_at'] = tweet_text_es_4['created_at'].apply(lambda a: pd.to_datetime(a).date())
tweet_text_es_4['created_at'].map(lambda x: x.isoformat())
# do the final cleanups and find the information of the authors of the tweets
clean_tweets = []
for num, row in tweet_text_es_4.iterrows():
tweet_id = row['tweet_id']
created_at = row['created_at']
source = row['source']
geo = row['geo']
coordinates = row['coordinates']
entities = row['entities']
user = row['user']
text = row['text']
try:
expanded_url = entities['urls'][0]['expanded_url']
except:
expanded_url = ""
author_name = user.name[0:]
author_screen_name = user.screen_name[0:]
location = user.location[0:]
dicionario = {"tweet_id": tweet_id,
"created_at": created_at,
"source": source,
"geo": geo,
"coordinates": coordinates,
"expanded_url": expanded_url,
"author_name": author_name,
"author_screen_name": author_screen_name,
"location": location,
"text": text,
"likely_target_of_attack": "journalists"
}
clean_tweets.append(dicionario)
df_clean_tweets_es_4 = pd.DataFrame(clean_tweets)
else:
df_clean_tweets_es_4 = tweet_text_es_4.copy()
# 5
new_search = "periodista comunista asquerosa OR periodista feminazi OR periodista abortera OR periodista comunista asqueroso OR periodista drogadicta OR periodista vagabunda OR periodista cocainomana OR periodista mariguanera OR periodista marihuanera OR periodista drogadicto OR periodista vagabundo OR periodista cocainomano OR periodista mariguanero OR periodista marihuanero -filter:retweets"
tweets = tw.Cursor(api.search_tweets,
q=new_search,
lang="es",
until=today).items(2000)
users_locs = [[tweet.id, tweet.created_at, tweet.source, tweet.geo, tweet.coordinates, tweet.entities, tweet.user, tweet.text] for tweet in tweets]
# transform into initial dataframe and do first cleanups
tweet_text_es_5 = pd.DataFrame(data=users_locs,
columns=['id', 'created_at', 'source', 'geo', 'coordinates', 'entities', 'user', 'text'])
size = len(tweet_text_es_5.index)
if size != 0:
tweet_text_es_5['id'] = tweet_text_es_5['id'].apply(str)
tweet_text_es_5.rename(columns={'id': 'tweet_id'}, inplace=True)
tweet_text_es_5['created_at'] = tweet_text_es_5['created_at'].apply(lambda a: pd.to_datetime(a).date())
tweet_text_es_5['created_at'].map(lambda x: x.isoformat())
# do the final cleanups and find the information of the authors of the tweets
clean_tweets = []
for num, row in tweet_text_es_5.iterrows():
tweet_id = row['tweet_id']
created_at = row['created_at']
source = row['source']
geo = row['geo']
coordinates = row['coordinates']
entities = row['entities']
user = row['user']
text = row['text']
try:
expanded_url = entities['urls'][0]['expanded_url']
except:
expanded_url = ""
author_name = user.name[0:]
author_screen_name = user.screen_name[0:]
location = user.location[0:]
dicionario = {"tweet_id": tweet_id,
"created_at": created_at,
"source": source,
"geo": geo,
"coordinates": coordinates,
"expanded_url": expanded_url,
"author_name": author_name,
"author_screen_name": author_screen_name,
"location": location,
"text": text,
"likely_target_of_attack": "journalists"
}
clean_tweets.append(dicionario)
df_clean_tweets_es_5 = pd.DataFrame(clean_tweets)
else:
df_clean_tweets_es_5 = tweet_text_es_5.copy()
# 6
new_search = "to:BuenMadrazo OR to:CEMDA OR to:tiburon_pepe OR to:PabloMontanoB OR to:sikuaa OR ambientalista tonta OR ambientalista pendeja OR ambientalista tarada OR ambientalista estúpida OR ambientalista estupida OR activista tonta OR activista pendeja OR activista tarada OR activista estúpida OR activista estupida OR indio tonto OR indio pendejo OR indio tarado OR indio estúpido OR indio estupido OR india estúpida OR india estupida -filter:retweets"
tweets = tw.Cursor(api.search_tweets,
q=new_search,
lang="es",
until=today).items(2000)
users_locs = [[tweet.id, tweet.created_at, tweet.source, tweet.geo, tweet.coordinates, tweet.entities, tweet.user, tweet.text] for tweet in tweets]
# transform into initial dataframe and do first cleanups
tweet_text_es_6 = pd.DataFrame(data=users_locs,
columns=['id', 'created_at', 'source', 'geo', 'coordinates', 'entities', 'user', 'text'])
size = len(tweet_text_es_6.index)
if size != 0:
tweet_text_es_6['id'] = tweet_text_es_6['id'].apply(str)
tweet_text_es_6.rename(columns={'id': 'tweet_id'}, inplace=True)
tweet_text_es_6['created_at'] = tweet_text_es_6['created_at'].apply(lambda a: pd.to_datetime(a).date())
tweet_text_es_6['created_at'].map(lambda x: x.isoformat())
# do the final cleanups and find the information of the authors of the tweets
clean_tweets = []
for num, row in tweet_text_es_6.iterrows():
tweet_id = row['tweet_id']
created_at = row['created_at']
source = row['source']
geo = row['geo']
coordinates = row['coordinates']
entities = row['entities']
user = row['user']
text = row['text']
try:
expanded_url = entities['urls'][0]['expanded_url']
except:
expanded_url = ""
author_name = user.name[0:]
author_screen_name = user.screen_name[0:]
location = user.location[0:]
dicionario = {"tweet_id": tweet_id,
"created_at": created_at,
"source": source,
"geo": geo,
"coordinates": coordinates,
"expanded_url": expanded_url,
"author_name": author_name,
"author_screen_name": author_screen_name,
"location": location,
"text": text,
"likely_target_of_attack": "environmental activists"
}
clean_tweets.append(dicionario)
df_clean_tweets_es_6 = pd.DataFrame(clean_tweets)
else:
df_clean_tweets_es_6 = tweet_text_es_6.copy()
#df_clean_tweets_es_6.info()
frames = [df_clean_tweets_es_1, df_clean_tweets_es_2, df_clean_tweets_es_3, df_clean_tweets_es_4, df_clean_tweets_es_5, df_clean_tweets_es_6]
df_clean_tweets_spanish = pd.concat(frames)
df_clean_tweets_spanish = df_clean_tweets_spanish.drop_duplicates(
subset = ['author_name', 'text'],
keep = 'last').reset_index(drop = True)
#df_clean_tweets_spanish.info()
# Airtable keys
# https://airtable.com/api
AIRTABLE_BASE_ID_E = os.environ["AIRTABLE_BASE_ID_E"]
AIRTABLE_TABLE_NAME = os.environ["AIRTABLE_TABLE_NAME"]
AIRTABLE_API_KEY = os.environ["AIRTABLE_API_KEY"]
ENDPOINT = f'https://api.airtable.com/v0/{AIRTABLE_BASE_ID_E}/{AIRTABLE_TABLE_NAME}'
# Collects the tweets that are already stored in the Airtable table
params = ()
airtable_records = []
run = True
while run is True:
response = requests.get(ENDPOINT, params=params, headers=headers)
airtable_response = response.json()
airtable_records += (airtable_response['records'])
if 'offset' in airtable_response:
run = True
params = (('offset', airtable_response['offset']),)
else:
run = False
airtable_rows = []
airtable_index = []
for record in airtable_records:
airtable_rows.append(record['fields'])
airtable_index.append(record['id'])
df_current_data = pd.DataFrame(airtable_rows, index=airtable_index)
# Unites current Tweepy scrape and stored data
# Eliminate duplicate Tweet IDs and ['author_name', 'text']
all_data = pd.merge(df_clean_tweets_spanish, \
df_current_data, \
how = 'left',
left_on=['author_name','text'], \
right_on=['author_name','text'])
all_data = all_data[["coordinates_x", "tweet_id_x", "expanded_url_x", "source_x", "created_at_x", "author_screen_name_x", "author_name", "location_x", "likely_target_of_attack_x", "text", "geo_x"]]
all_data.rename(columns = {'coordinates_x':'coordinates'},inplace = True)
all_data.rename(columns = {'tweet_id_x':'tweet_id'},inplace = True)
all_data.rename(columns = {'expanded_url_x':'expanded_url'},inplace = True)
all_data.rename(columns = {'source_x':'source'},inplace = True)
all_data.rename(columns = {'created_at_x':'created_at'},inplace = True)
all_data.rename(columns = {'author_screen_name_x':'author_screen_name'},inplace = True)
all_data.rename(columns = {'location_x':'location'},inplace = True)
all_data.rename(columns = {'likely_target_of_attack_x':'likely_target_of_attack'},inplace = True)
all_data.rename(columns = {'geo_x':'geo'},inplace = True)
all_data['tweet_id'] = all_data['tweet_id'].str.strip()
#all_data.shape
df_tweets_final_es = all_data.drop_duplicates(
subset = ['author_name', 'text'],
keep = 'last').reset_index(drop = True)
df_tweets_final_es.info()
# test for new data
size = len(df_tweets_final_es.index)
if size != 0:
for num, row in df_tweets_final_es.iterrows():
tweet_id = row['tweet_id']
created_at = row['created_at']
source = row['source']
geo = row['geo']
coordinates = row['coordinates']
expanded_url = row['expanded_url']
author_name = row['author_name']
author_screen_name = row['author_screen_name']
location = row['location']
text = row['text']
likely_target_of_attack = row['likely_target_of_attack']
add_to_airtable(tweet_id, created_at, source, geo, coordinates, expanded_url, author_name, author_screen_name, location,text, likely_target_of_attack)