-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_tweets.py
801 lines (775 loc) · 36.1 KB
/
get_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
795
796
797
import os
import sqlite3
from random import shuffle
from datetime import datetime
import time
import pymysql
from twitter_credentials import *
from twython import Twython
#############################################################################################
####################### INPUTS ##############################################################
db_name='DB_NAME.db'
max_queries_per_user=16
twitter = Twython(twitter_auth['consumer_key'],twitter_auth['consumer_secret'],twitter_auth['access_token'],twitter_auth['access_token_secret'])
# Working directory is where the sqlite database file is found!
working_directory="./"
#############################################################################################
######################## BELOW SHOULD NOT NEED TO BE ALTERED ################################
#############################################################################################
######################## Tweet Data Class ###################################################
class TweetData:
def __init__(
self,
conn
):
self.cnx=conn
self.c=self.cnx.cursor()
return(None)
def totranslate(self,string):
tempstr=string.replace("'","^").encode('unicode-escape').decode('utf-8')
return(tempstr)
def create_tweet_tables(self):
self.c.execute('CREATE TABLE IF NOT EXISTS user_profile (user_id BIGINT,' #user table needs some updates, to include current status id (should also be inserted into tweets)
+'screen_name VARCHAR(40),'
+'name VARCHAR(40),'
+'created_at DATETIME,'
+'description VARCHAR(200),'
+'status_id BIGINT,'
+'geo_enabled VARCHAR(5),'
+'protected VARCHAR(5),'
+'friends_count INT,'
+'followers_count INT,'
+'favourites_count INT,'
+'statuses_count INT,'
+'lang VARCHAR(3),'
+'location VARCHAR(75),'
+'verified VARCHAR(5),'
+'profile_url VARCHAR(200),'
+'default_image VARCHAR(5),'
+'time_zone VARCHAR(45),'
+'UTC_offset INT,'
+'profile_pic_hash CHAR(16),'
+'profile_pic_filename VARCHAR(40),'
+'profile_banner_hash CHAR(16),'
+'profile_banner_filename VARCHAR(40),'
+'obtained DATE,'
+'last_accessed DATE,'
+'active VARCHAR(5),'
+'PRIMARY KEY (user_id,screen_name)'
+');')
self.c.execute('CREATE INDEX IF NOT EXISTS sn_index ON user_profile(screen_name);')
# self.c.execute('CREATE INDEX name ON user_profile(name);')
self.c.execute('CREATE TABLE IF NOT EXISTS tweet (tweet_id BIGINT PRIMARY KEY,'
+'user_id BIGINT,'
+'screen_name VARCHAR(40),'
+'created_at DATETIME,'
+'text VARCHAR(180),'
+'geo_lat FLOAT,'
+'geo_long FLOAT,'
+'place_type VARCHAR(20),'
+'place_name VARCHAR(40),'
+'lang VARCHAR(3),'
+'source VARCHAR(110),'
+'retweet_count INT,'
+'favorite_count INT,'
+'retweet_status_id BIGINT,'
+'reply_to_status_id BIGINT,'
+'reply_to_user_id BIGINT,'
+'reply_to_screen_name VARCHAR(40),'
+'FOREIGN KEY (user_id,screen_name) REFERENCES user_profile (user_id,screen_name)'
# +'FOREIGN KEY (reply_to_user_id,reply_to_screen_name) REFERENCES user_profile (user_id,screen_name),'
# +'FOREIGN KEY (reply_to_status_id) REFERENCES tweet (tweet_id)
+');')
self.c.execute('CREATE INDEX IF NOT EXISTS sn_tweet_index ON tweet(screen_name);')
self.c.execute('CREATE INDEX IF NOT EXISTS user_id_index ON tweet(user_id);')
# self.c.execute('CREATE TABLE IF NOT EXISTS followers (ego_id BIGINT,'
# +'follower_id BIGINT,'
# +'FOREIGN KEY (ego_id) REFERENCES user (user_id),'
# +'FOREIGN KEY (follower_id) REFERENCES user (user_id),'
# +'PRIMARY KEY (ego_id,follower_id)'
# +');')
self.c.execute('CREATE TABLE IF NOT EXISTS tweet_media (tweet_id BIGINT,'
+'pic_hash CHAR(16),'
+'pic_id BIGINT,'
+'pic_source_user_id BIGINT,'
+'pic_source_status_id BIGINT,'
+'pic_filename VARCHAR(70),'
+'url VARCHAR(80),'
+'media_url VARCHAR(80),'
+'display_url VARCHAR(80),'
+'FOREIGN KEY (tweet_id) REFERENCES tweet (tweet_id),'
+'PRIMARY KEY (tweet_id,pic_id)'
+');')
self.c.execute('CREATE TABLE IF NOT EXISTS tweet_hashtags (tweet_id BIGINT,'
+'hashtag VARCHAR(50),'
+'FOREIGN KEY (tweet_id) REFERENCES tweet (tweet_id),'
+'PRIMARY KEY (tweet_id,hashtag)'
+');')
self.c.execute('CREATE TABLE IF NOT EXISTS tweet_usermentions (tweet_id BIGINT,'
+'user_mention_id BIGINT,'
+'user_mention_screen_name VARCHAR(40),'
+'user_mention_name VARCHAR(40),'
+'FOREIGN KEY (tweet_id) REFERENCES tweet (tweet_id),'
+'PRIMARY KEY (tweet_id,user_mention_id)'
+');')
self.c.execute('CREATE TABLE IF NOT EXISTS tweet_url (tweet_id BIGINT,'
+'url VARCHAR(100),'
+'expanded_url VARCHAR(200),'
+'FOREIGN KEY (tweet_id) REFERENCES tweet (tweet_id),'
+'PRIMARY KEY (tweet_id,url)'
+');')
self.c.execute("CREATE TABLE IF NOT EXISTS collected (user_id BIGINT,"
+"date_collected DATE,"
+"first_tweet BIGINT,"
+"last_tweet BIGINT,"
+"FOREIGN KEY (user_id) REFERENCES user (user_id),"
+"FOREIGN KEY (first_tweet) REFERENCES tweet (tweet_id),"
+"FOREIGN KEY (last_tweet) REFERENCES tweet (tweet_id)"
+");")
# def update_links(self,update_size=2000):
# count=0
# self.c.execute('SELECT count(1) from link;')
# result=c.fetchall()
# link_count=result[0][0]
# while count*update_size < link_count:
# self.c.execute("SELECT * FROM link LIMIT {0:d} OFFSET {1:d};".format(update_size,count*update_size))
# all_links=self.c.fetchall()
# count+=1
# qstr="INSERT OR IGNORE INTO followers (ego_id, follower_id) VALUES "
# if all_links[0][2] and not all_links[0][3]:
# qstr+=" ("+str(all_links[0][1])+","+str(all_links[0][0])+")"
# elif all_links[0][2]:
# qstr+="("+str(all_links[0][1])+","+str(all_links[0][0])+")"
# qstr+=",("+str(all_links[0][0])+","+str(all_links[0][1])+")"
# else:
# qstr+="("+str(all_links[0][0])+","+str(all_links[0][1])+")"
# for i in all_links[1:update_size]:
# if i[2]:
# qstr+=",("+str(i[1])+","+str(i[0])+")"
# if i[3]:
# qstr+=",("+str(i[0])+","+str(i[1])+")"
# qstr+=";"
# self.c.execute(qstr)
def get_location_userids(self):
self.c.execute("SELECT user_id FROM user WHERE Label = 1;")
location_users=self.c.fetchall()
location_users=[i[0] for i in location_users]
return(location_users)
def get_collected_userids(self):
self.c.execute("SELECT user_id FROM collected;")
collected_users=self.c.fetchall()
collected_users=[i[0] for i in collected_users]
return(collected_users)
def insertUserProfiles(self,users,obtained_date,access_date): #Insert a collection of users accessed on a certain date.
#First make string of values to insert.
ca=[datetime.strptime(i['created_at'].replace('+0000','UTC'),'%a %b %d %H:%M:%S %Z %Y') for i in users]
u=users
while(len(u)>0):
users=u[0:1000]
u=u[1000:max(1000,len(u))]
created_at=ca[0:1000]
ca=ca[1000:max(1000,len(ca))]
profilestring=""
for i in range(len(users)):
user=users[i]
profilestring+='('
profilestring+= str(user['id'])+','
profilestring+= "'"+str(user['screen_name'])+"',"
profilestring+= "'"+self.totranslate(user_profile['name'])[0:40]+" ',"
profilestring+= "'"+created_at[i].strftime("%Y-%m-%d %H:%M:%S")+"',"
profilestring+= "'"+self.totranslate(user['description'])[0:200]+" ',"
if 'status' in user.keys():
profilestring+= str(user['status']['id'])+","
else:
profilestring+='NULL,'
profilestring+= "'"+str(user['geo_enabled'])+"',"
profilestring+= "'"+str(user['protected'])+"',"
profilestring+= str(user['friends_count'])+','
profilestring+= str(user['followers_count'])+','
profilestring+= str(user['favourites_count'])+','
profilestring+= str(user['statuses_count'])+','
if user['lang'] is None:
profilestring+= 'NULL,'
else:
profilestring+= "'"+str(user['lang'])[0:3]+"',"
if user['location'] is None:
profilestring+= 'NULL,'
else:
profilestring+= "'"+self.totranslate(user['location'])[0:75]+" ',"
profilestring+= "'"+str(user['verified'])+"',"
if user['url'] is None:
profilestring += 'NULL,'
else:
if user['entities']['url']['urls'][0]['expanded_url'] is None:
profilestring+="'"+str(user['entities']['url']['urls'][0]['url'].replace("'","''"))+"',"
else:
profilestring+= "'"+str(user['entities']['url']['urls'][0]['expanded_url'].replace("'","''"))+"',"
profilestring+= "'"+str(user['default_profile_image'])+"',"
if user['time_zone'] is None:
profilestring+='NULL,NULL,'
else:
profilestring+= "'"+self.totranslate(user['time_zone'])+"',"
profilestring+= str(user['utc_offset'])+','
profilestring+="NULL,NULL,NULL,NULL,"
profilestring+= "'"+obtained_date +"',"
profilestring+= "'"+access_date +"',"
profilestring+= "'True'"#No comma here
if i==len(users)-1:
profilestring+=')'
else:
profilestring+= '),\n'
if profilestring !="":
self.c.execute('INSERT OR REPLACE INTO user_profile (user_id,'
+'screen_name,'
+'name,'
+'created_at,'
+'description,'
+'status_id,'
+'geo_enabled,'
+'protected,'
+'friends_count,'
+'followers_count,'
+'favourites_count,'
+'statuses_count,'
+'lang,'
+'location,'
+'verified,'
+'profile_url,'
+'default_image,'
+'time_zone,'
+'UTC_offset,'
+'profile_pic_hash,'
+'profile_pic_filename,'
+'profile_banner_hash,'
+'profile_banner_filename,'
+'obtained,'
+'last_accessed,'
+'active'
+')\n'
+'VALUES\n'
+profilestring
+'\n'
+';')
def insertUsersfromTweets(self,tweets,access_date): #Insert a collection of users accessed on a certain date.
#First make string of values to insert.
od=[datetime.strptime(i['created_at'].replace('+0000','UTC'),'%a %b %d %H:%M:%S %Z %Y') for i in tweets]
u=[i['user'] for i in tweets]
ca=[datetime.strptime(i['created_at'].replace('+0000','UTC'),'%a %b %d %H:%M:%S %Z %Y') for i in u]
while(len(u)>0):
users=u[0:1000]
u=u[1000:max(1000,len(u))]
created_at=ca[0:1000]
ca=ca[1000:max(1000,len(ca))]
obtained_date=od[0:1000]
od=od[1000:max(1000,len(od))]
profilestring=""
for i in range(len(users)):
user=users[i]
profilestring+='('
profilestring+= str(user['id'])+','
profilestring+= "'"+str(user['screen_name'])+"',"
profilestring+= "'"+self.totranslate(user['name'])[0:40]+" ',"
profilestring+= "'"+created_at[i].strftime("%Y-%m-%d %H:%M:%S")+"',"
profilestring+= "'"+self.totranslate(user['description'])[0:200]+" ',"
if 'status' in user.keys():
profilestring+= str(user['status']['id'])+","
else:
profilestring+='NULL,'
profilestring+= "'"+str(user['geo_enabled'])+"',"
profilestring+= "'"+str(user['protected'])+"',"
profilestring+= str(user['friends_count'])+','
profilestring+= str(user['followers_count'])+','
profilestring+= str(user['favourites_count'])+','
profilestring+= str(user['statuses_count'])+','
if user['lang'] is None:
profilestring+= 'NULL,'
else:
profilestring+= "'"+str(user['lang'])[0:3]+"',"
if user['location'] is None:
profilestring+= 'NULL,'
else:
profilestring+= "'"+self.totranslate(user['location'])[0:75]+" ',"
profilestring+= "'"+str(user['verified'])+"',"
if user['url'] is None:
profilestring += 'NULL,'
else:
if user['entities']['url']['urls'][0]['expanded_url'] is None:
profilestring+="'"+str(user['entities']['url']['urls'][0]['url'].replace("'","''"))+"',"
else:
profilestring+= "'"+str(user['entities']['url']['urls'][0]['expanded_url'].replace("'","''"))+"',"
profilestring+= "'"+str(user['default_profile_image'])+"',"
if user['time_zone'] is None:
profilestring+='NULL,NULL,'
else:
profilestring+= "'"+self.totranslate(user['time_zone'])+"',"
profilestring+= str(user['utc_offset'])+','
profilestring+="NULL,NULL,NULL,NULL,"
profilestring+= "'"+obtained_date[i].strftime('%Y-%m-%d') +"',"
profilestring+= "'"+access_date +"',"
profilestring+= "'True'"#No comma here
if i==len(users)-1:
profilestring+=')'
else:
profilestring+= '),\n'
if profilestring !="":
self.c.execute('INSERT OR REPLACE INTO user_profile (user_id,'
+'screen_name,'
+'name,'
+'created_at,'
+'description,'
+'status_id,'
+'geo_enabled,'
+'protected,'
+'friends_count,'
+'followers_count,'
+'favourites_count,'
+'statuses_count,'
+'lang,'
+'location,'
+'verified,'
+'profile_url,'
+'default_image,'
+'time_zone,'
+'UTC_offset,'
+'profile_pic_hash,'
+'profile_pic_filename,'
+'profile_banner_hash,'
+'profile_banner_filename,'
+'obtained,'
+'last_accessed,'
+'active'
+')\n'
+'VALUES\n'
+profilestring
+'\n'
+';')
def insertTweets(self,tweets): #put the tweet in the tweet table, update the tweet media, tweet hashtag, tweet url
#First make string of values to insert.
ca=[datetime.strptime(i['created_at'].replace('+0000','UTC'),'%a %b %d %H:%M:%S %Z %Y') for i in tweets]
t=tweets
while(len(t)>0):
tweets=t[0:1000]
t=t[1000:max(1000,len(t))]
created_at=ca[0:1000]
ca=ca[1000:max(1000,len(ca))]
tweetstring=""
htstring=""
umstring=""
urlstring=""
vals=""
for i in range(len(tweets)):
tweet=tweets[i]
tweetstring+='('
tweetstring+= str(tweet['id'])+','
if 'user' in tweet.keys():
tweetstring+= str(tweet['user']['id'])+","
tweetstring+= "'"+tweet['user']['screen_name']+"',"
else:
tweetstring+='NULL,NULL,'
tweetstring+= "'"+created_at[i].strftime("%Y-%m-%d %H:%M:%S")+"',"
tweetstring+= "'"+self.totranslate(tweet['text'])[0:180]+" ',"
if tweet['geo'] is None:
tweetstring+='NULL,NULL,'
else:
tweetstring+=str(tweet['geo']['coordinates'][0])+','+str(tweet['geo']['coordinates'][1])+','
if tweet['place'] is None:
tweetstring+='NULL,NULL,'
else:
tweetstring+= "'"+tweet['place']['place_type']+"',"
tweetstring+= "'"+self.totranslate(tweet['place']['name'])[0:40]+" ',"
tweetstring+= "'"+str(tweet['lang'])[0:3]+"',"
tweetstring+="'"+tweet['source'].replace("'","''")[0:109]+" ',"
if 'retweet_count' in tweet.keys():
tweetstring+= str(tweet['retweet_count'])+","
else:
tweetstring+= "NULL,"
if 'favorite_count' in tweet.keys():
tweetstring+= str(tweet['favorite_count'])+","
else:
tweetstring+= "NULL,"
if 'retweeted_status' in tweet.keys():
tweetstring+=str(tweet['retweeted_status']['id'])+','
else:
tweetstring+='NULL,'
if tweet['in_reply_to_status_id'] is None:
tweetstring+='NULL,NULL,NULL'
else:
tweetstring+=str(tweet['in_reply_to_status_id'])+','+str(tweet['in_reply_to_user_id'])+",'"+tweet['in_reply_to_screen_name']+"'"
if i==len(tweets)-1:
tweetstring+=')'
else:
tweetstring+= '),\n'
for j in tweet['entities']['hashtags']:
if htstring=="":
htstring+='('
else:
htstring+=',\n('
htstring+=str(tweet['id'])+','
htstring+="'"+j['text'][0:180]+"')"
for j in tweet['entities']['urls']:
if urlstring=="":
urlstring+='('
else:
urlstring+=',\n('
urlstring+=str(tweet['id'])+','
urlstring+="'"+j['url'].replace("'","''")+"',"
if 'expanded_url' in j.keys():
urlstring+="'"+j['expanded_url'].replace("'","''")+"')"
else:
urlstring+="NULL)"
for j in tweet['entities']['user_mentions']:
if umstring=="":
umstring+='('
else:
umstring+=',\n('
umstring+=str(tweet['id'])+','
umstring+=str(j['id'])+","
umstring+="'"+j['screen_name']+"',"
umstring+="'"+self.totranslate(j['name'])[0:40]+" ')"
if 'media' in tweet['entities'].keys():
if 'extended_entities' in tweet.keys():
txt='extended_entities'
else:
txt='entities'
for k in range(len(tweet[txt]['media'])):
if vals=="":
vals+='('
else:
vals+=',\n('
j=tweet[txt]['media'][k]
vals+=str(tweet['id'])+","
vals+="NULL,"
vals+=str(j['id'])+','
if 'source_status_id' in j.keys():
vals+=str(j['source_user_id'])+","
vals+=str(j['source_status_id'])+","
else:
vals+="NULL,NULL,"
vals+="NULL,"
vals+="'"+j['url']+"',"
vals+="'"+j['media_url'][0:79]+" ',"
vals+="'"+j['display_url']+"')"
if tweetstring !="":
self.c.execute('INSERT OR REPLACE INTO tweet (tweet_id,'
+'user_id,'
+'screen_name,'
+'created_at,'
+'text,'
+'geo_lat,'
+'geo_long,'
+'place_type,'
+'place_name,'
+'lang,'
+'source,'
+'retweet_count,'
+'favorite_count,'
+'retweet_status_id,'
+'reply_to_status_id,'
+'reply_to_user_id,'
+'reply_to_screen_name'#No comma here!
+')\n'
+'VALUES\n'
+tweetstring
+';')
if htstring !="":
self.c.execute('INSERT OR IGNORE INTO tweet_hashtags (tweet_id,'
+'hashtag'
+')\n'
+'VALUES\n'
+htstring
+';')
if umstring !="":
self.c.execute('INSERT OR IGNORE INTO tweet_usermentions (tweet_id,'
+'user_mention_id,'
+'user_mention_screen_name,'
+'user_mention_name'
+')\n'
+'VALUES\n'
+umstring
+';')
if urlstring !="":
self.c.execute('INSERT OR IGNORE INTO tweet_url (tweet_id,'
+'url,'
+'expanded_url'
+')\n'
+'VALUES\n'
+urlstring
+';')
if vals !="":
self.c.execute('INSERT OR REPLACE INTO tweet_media (tweet_id,'
+'pic_hash,'
+'pic_id,'
+'pic_source_user_id,'
+'pic_source_status_id,'
+'pic_filename,'
+'url,'
+'media_url,'
+'display_url'
+')\n'
+'VALUES\n'
+vals
+';')
def insertStatuses(self,users): #put the tweet in the tweet table, update the tweet media, tweet hashtag, tweet url
#First make string of values to insert. #Retweets in Statuses don't have original users attached.
t=[i['status'] for i in users if 'status' in i.keys()]
ul=[{'id':i['id'],'screen_name':i['screen_name']} for i in users if 'status' in i.keys()]
ca=[datetime.strptime(i['created_at'].replace('+0000','UTC'),'%a %b %d %H:%M:%S %Z %Y') for i in t]
while(len(t)>0):
tweets=t[0:1000]
t=t[1000:max(1000,len(t))]
created_at=ca[0:1000]
ca=ca[1000:max(1000,len(ca))]
userlist=ul[0:1000]
ul=ul[1000:max(1000,len(ul))]
tweetstring=""
htstring=""
umstring=""
urlstring=""
vals=""
for i in range(len(tweets)):
tweet=tweets[i]
tweetstring+='('
tweetstring+= str(tweet['id'])+','
tweetstring+= str(userlist[i]['id'])+","
tweetstring+= "'"+userlist[i]['screen_name']+"',"
tweetstring+= "'"+created_at[i].strftime("%Y-%m-%d %H:%M:%S")+"',"
tweetstring+= "'"+self.totranslate(tweet['text'])[0:180]+" ',"
if tweet['geo'] is None:
tweetstring+='NULL,NULL,'
else:
tweetstring+=str(tweet['geo']['coordinates'][0])+','+str(tweet['geo']['coordinates'][1])+','
if tweet['place'] is None:
tweetstring+='NULL,NULL,'
else:
tweetstring+= "'"+tweet['place']['place_type']+"',"
tweetstring+= "'"+self.totranslate(tweet['place']['name'])+" ',"
tweetstring+= "'"+str(tweet['lang'])[0:3]+"',"
tweetstring+= "'"+tweet['source'].replace("'","''")[0:109]+" ',"
if 'retweet_count' in tweet.keys():
tweetstring+=str(tweet['retweet_count'])+","
else:
tweetstring+="NULL,"
if 'favorite_count' in tweet.keys():
tweetstring+=str(tweet['favorite_count'])+","
else:
tweetstring+="NULL,"
if 'retweeted_status' in tweet.keys():
tweetstring+=str(tweet['retweeted_status']['id'])+','
else:
tweetstring+='NULL,'
if tweet['in_reply_to_status_id'] is None:
tweetstring+='NULL,NULL,NULL'
else:
tweetstring+=str(tweet['in_reply_to_status_id'])+','+str(tweet['in_reply_to_user_id'])+",'"+tweet['in_reply_to_screen_name']+"'"
if i==len(tweets)-1:
tweetstring+=')'
else:
tweetstring+= '),\n'
for j in tweet['entities']['hashtags']:
if htstring=="":
htstring+='('
else:
htstring+=',\n('
htstring+=str(tweet['id'])+','
htstring+="'"+j['text'][0:160]+"')"
for j in tweet['entities']['urls']:
if urlstring=="":
urlstring+='('
else:
urlstring+=',\n('
urlstring+=str(tweet['id'])+','
urlstring+="'"+j['url'].replace("'","''")+"',"
if 'expanded_url' in j.keys():
urlstring+="'"+j['expanded_url'].replace("'","''")+"')"
else:
urlstring+="NULL)"
for j in tweet['entities']['user_mentions']:
if umstring=="":
umstring+='('
else:
umstring+=',\n('
umstring+=str(tweet['id'])+','
umstring+=str(j['id'])+","
umstring+="'"+j['screen_name']+"',"
umstring+="'"+self.totranslate(j['name'])[0:40]+" ')"
if 'media' in tweet['entities'].keys():
if 'extended_entities' in tweet.keys():
txt='extended_entities'
else:
txt='entities'
for k in range(len(tweet[txt]['media'])):
if vals=="":
vals+='('
else:
vals+=',\n('
j=tweet[txt]['media'][k]
vals+=str(tweet['id'])+","
vals+="NULL,"
vals+=str(j['id'])+','
if 'source_status_id' in j.keys():
vals+=str(j['source_user_id'])+","
vals+=str(j['source_status_id'])+","
else:
vals+="NULL,NULL,"
vals+="NULL,"
vals+="'"+j['url']+"',"
vals+="'"+j['media_url'][0:79]+" ',"
vals+="'"+j['display_url']+"')"
if tweetstring !="":
self.c.execute('INSERT OR REPLACE INTO tweet (tweet_id,'
+'user_id,'
+'screen_name,'
+'created_at,'
+'text,'
+'geo_lat,'
+'geo_long,'
+'place_type,'
+'place_name,'
+'lang,'
+'source,'
+'retweet_count,'
+'favorite_count,'
+'retweet_status_id,'
+'reply_to_status_id,'
+'reply_to_user_id,'
+'reply_to_screen_name'#No comma here!
+')\n'
+'VALUES\n'
+tweetstring
+';')
if htstring !="":
self.c.execute('INSERT OR IGNORE INTO tweet_hashtags (tweet_id,'
+'hashtag'
+')\n'
+'VALUES\n'
+htstring
+';')
if umstring !="":
self.c.execute('INSERT OR IGNORE INTO tweet_usermentions (tweet_id,'
+'user_mention_id,'
+'user_mention_screen_name,'
+'user_mention_name'
+')\n'
+'VALUES\n'
+umstring
+';')
if urlstring !="":
self.c.execute('INSERT OR IGNORE INTO tweet_url (tweet_id,'
+'url,'
+'expanded_url'
+')\n'
+'VALUES\n'
+urlstring
+';')
if vals !="":
self.c.execute('INSERT OR REPLACE INTO tweet_media (tweet_id,'
+'pic_hash,'
+'pic_id,'
+'pic_source_user_id,'
+'pic_source_status_id,'
+'pic_filename,'
+'url,'
+'media_url,'
+'display_url'
+')\n'
+'VALUES\n'
+vals
+';')
def allFromTweets(self,tweets,access_date):
rts=[i['retweeted_status'] for i in tweets if 'retweeted_status' in i.keys()]
y=[i for i in tweets if i['id'] not in [j['id'] for j in rts]]
z=rts+y
users=[i['user'] for i in z]
self.insertUsersfromTweets(z,access_date)
self.cnx.commit()
self.insertStatuses(users)
self.cnx.commit()
self.insertTweets(rts)
self.cnx.commit()
self.insertTweets(y)
self.cnx.commit()
return(None)
def insert_user_collected(self,user_id,date_collected,first_tweet=None,last_tweet=None):
if (first_tweet is None) or (last_tweet is None):
self.c.execute("INSERT INTO collected (user_id,date_collected) VALUES "
+"("+str(user_id)+",'"+date_collected+"');")
else:
self.c.execute("INSERT INTO collected (user_id,date_collected,first_tweet,last_tweet) VALUES "
+"("+str(user_id)+",'"+date_collected+"',"+str(first_tweet)+","+str(last_tweet)+");")
##########################################################################################################
############################### EXECUTION CODE ###########################################################
os.chdir(working_directory)
conn=sqlite3.connect(db_name)
tweet_data=TweetData(conn)
tweet_data.create_tweet_tables()
location_users=tweet_data.get_location_userids()
collected_users=tweet_data.get_collected_userids()
remaining_users=list(set(location_users)-set(collected_users))
shuffle(remaining_users)
print(len(remaining_users))
start_time=datetime.now()
query_start=datetime.now()
while (len(remaining_users)>0):
tweets=[]
time.sleep(max(0,(1-(datetime.now()-query_start).total_seconds())))
query_start=datetime.now()
date_now=datetime.now().strftime("%Y-%m-%d")
uid=remaining_users.pop(0)
collect=True
try:
user_profile=twitter.show_user(user_id=uid)
collect=True
except:
time.sleep(1)
try:
user_profile=twitter.show_user(user_id=uid)
collect=True
except:
collect=False
print("Collect failed for user "+str(uid))
if collect:
tweet_data.insertUserProfiles([user_profile],date_now,date_now)
conn.commit()
if (user_profile['statuses_count']>0) and not (user_profile['protected']):
try:
temp_tweets=twitter.get_user_timeline(user_id=uid,count=200)
query_count=1
except:
time.sleep(1)
try:
temp_tweets=twitter.get_user_timeline(user_id=uid,count=200)
query_count=1
except:
temp_tweets=[]
query_count=0
tweets+=temp_tweets
tweet_data.allFromTweets(temp_tweets,date_now)
while (len(temp_tweets)>0) and (query_count < max_queries_per_user):
conn.commit()
# print(" "+str(max(0,(1-(datetime.now()-query_start).total_seconds()))))
time.sleep(max(0,(1-(datetime.now()-query_start).total_seconds())))
query_start=datetime.now()
# print(" "+str(len(tweets)))
m_id=min([i['id'] for i in temp_tweets])-1
try:
temp_tweets=twitter.get_user_timeline(user_id=uid,count=200,max_id=m_id)
except:
time.sleep(1)
try:
temp_tweets=twitter.get_user_timeline(user_id=uid,count=200,max_id=m_id)
except:
temp_tweets=[]
print("Error getting follow-on tweets user {0:d} (@{1:s})".format(uid,user_profile['screen_name']))
tweets+=temp_tweets
tweet_data.allFromTweets(temp_tweets,date_now)
query_count+=1
if len(tweets)>0:
first_tweet=min([i['id'] for i in tweets])
last_tweet=max([i['id'] for i in tweets])
tweet_data.insert_user_collected(uid,date_now,first_tweet,last_tweet)
else:
tweet_data.insert_user_collected(uid,date_now)
print("Inserted "+str(len(tweets))+" tweets from user "+str(uid)+".")
if len(remaining_users) % 10 == 0:
print("\n{0:d} Users remaining.\n".format(len(remaining_users)))
conn.close()