forked from mikeizbicki/twitter_postgres_parallel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload_tweets.py
More file actions
393 lines (351 loc) · 14.9 KB
/
load_tweets.py
File metadata and controls
393 lines (351 loc) · 14.9 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
382
383
384
385
386
387
388
389
390
391
392
393
#!/usr/bin/python3
# process command line args
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--db',required=True)
parser.add_argument('--inputs',nargs='+',required=True)
parser.add_argument('--print_every',type=int,default=1000)
args = parser.parse_args()
# imports
import sqlalchemy
import os
import datetime
import zipfile
import io
import json
# create database connection
engine = sqlalchemy.create_engine(args.db, connect_args={
'application_name': 'load_tweets.py',
})
connection = engine.connect()
################################################################################
# helper functions
################################################################################
def remove_nulls(s):
r'''
Postgres doesn't support strings with the null character \x00 in them, but twitter does.
This helper function replaces the null characters with an escaped version so that they can be loaded into postgres.
Technically, this means the data in postgres won't be an exact match of the data in twitter,
and there is no way to get the original twitter data back from the data in postgres.
The null character is extremely rarely used in real world text (approx. 1 in 1 billion tweets),
and so this isn't too big of a deal.
A more correct implementation, however, would be to *escape* the null characters rather than remove them.
This isn't hard to do in python, but it is a bit of a pain to do with the JSON/COPY commands for the denormalized data.
Since our goal is for the normalized/denormalized versions of the data to match exactly,
we're not going to escape the strings for the normalized data.
>>> remove_nulls('\x00')
''
>>> remove_nulls('hello\x00 world')
'hello world'
'''
if s is None:
return None
else:
return s.replace('\x00','\\x00')
def get_id_urls(url):
'''
Given a url, returns the corresponding id in the urls table.
If no row exists for the url, then one is inserted automatically.
'''
sql = sqlalchemy.sql.text('''
insert into urls
(url)
values
(:url)
on conflict do nothing
returning id_urls
;
''')
res = connection.execute(sql,{'url':url}).first()
if res is None:
sql = sqlalchemy.sql.text('''
select id_urls
from urls
where
url=:url
''')
res = connection.execute(sql,{'url':url}).first()
id_urls = res[0]
return id_urls
################################################################################
# main functions
################################################################################
def insert_tweet(connection,tweet):
'''
Inserts the tweet into the database.
Args:
connection: a sqlalchemy connection to the postgresql db
tweet: a dictionary representing the json tweet object
'''
# skip tweet if it's already inserted
with connection.begin() as trans:
sql=sqlalchemy.sql.text('''
SELECT id_tweets
FROM tweets
WHERE id_tweets = :id_tweets
''')
res = connection.execute(sql,{
'id_tweets':tweet['id'],
})
if res.first() is not None:
return
# insert tweet within a transaction;
# this ensures that a tweet does not get "partially" loaded
#connection.commit()
#with connection.begin() as trans:
########################################
# insert into the users table
########################################
if tweet['user']['url'] is None:
user_id_urls = None
else:
user_id_urls = get_id_urls(tweet['user']['url'])
# create/update the user
sql = sqlalchemy.sql.text('''
INSERT INTO users
(id_users,created_at,updated_at,screen_name,name,location,id_urls,description,protected,verified,friends_count,listed_count,favourites_count,statuses_count,withheld_in_countries)
VALUES
(:id_users,:created_at,:updated_at,:screen_name,:name,:location,:id_urls,:description,:protected,:verified,:friends_count,:listed_count,:favourites_count,:statuses_count,:withheld_in_countries)
ON CONFLICT (id_users) DO
NOTHING
''')
# NOTE:
# doing the following on conflict is slightly more correct as it would allow us
# to overwrite older versions of a user's information with newer versions;
# but it is significantly slower because it introduces a lock on the table
# that causes other insertion processes to wait.
#UPDATE SET
#created_at=:created_at,
#updated_at=:updated_at,
#screen_name=:screen_name,
#name=:name,
#location=:location,
#id_urls=:id_urls,
#description=:description,
#protected=:protected,
#verified=:verified,
#friends_count=:friends_count,
#listed_count=:listed_count,
#favourites_count=:favourites_count,
#statuses_count=:statuses_count,
#withheld_in_countries=:withheld_in_countries
#WHERE :updated_at > users.updated_at OR users.updated_at is null
res = connection.execute(sql,{
'id_users':tweet['user']['id'],
'created_at':tweet['user']['created_at'],
'updated_at':tweet['created_at'],
'screen_name':remove_nulls(tweet['user']['screen_name']),
'name':remove_nulls(tweet['user']['name']),
'location':remove_nulls(tweet['user']['location']),
'id_urls':user_id_urls,
'description':remove_nulls(tweet['user']['description']),
'protected':tweet['user']['protected'],
'verified':tweet['user']['verified'],
'friends_count':tweet['user']['friends_count'],
'listed_count':tweet['user']['listed_count'],
'favourites_count':tweet['user']['favourites_count'],
'statuses_count':tweet['user']['statuses_count'],
'withheld_in_countries':tweet['user'].get('withheld_in_countries',None),
})
########################################
# insert into the tweets table
########################################
try:
geo_coords = tweet['geo']['coordinates']
geo_coords = str(tweet['geo']['coordinates'][0]) + ' ' + str(tweet['geo']['coordinates'][1])
geo_str = 'POINT'
except TypeError:
try:
geo_coords = '('
for i,poly in enumerate(tweet['place']['bounding_box']['coordinates']):
if i>0:
geo_coords+=','
geo_coords+='('
for j,point in enumerate(poly):
geo_coords+= str(point[0]) + ' ' + str(point[1]) + ','
geo_coords+= str(poly[0][0]) + ' ' + str(poly[0][1])
geo_coords+=')'
geo_coords+=')'
geo_str = 'MULTIPOLYGON'
except KeyError:
if tweet['user']['geo_enabled']:
geo_str = None
geo_coords = None
try:
text = tweet['extended_tweet']['full_text']
except:
text = tweet['text']
try:
country_code = tweet['place']['country_code'].lower()
except TypeError:
country_code = None
if country_code == 'us':
state_code = tweet['place']['full_name'].split(',')[-1].strip().lower()
if len(state_code)>2:
state_code = None
else:
state_code = None
try:
place_name = tweet['place']['full_name']
except TypeError:
place_name = None
# NOTE:
# The tweets table has the following foreign key:
# > FOREIGN KEY (in_reply_to_user_id) REFERENCES users(id_users)
#
# This means that every "in_reply_to_user_id" field must reference a valid entry in the users table.
# If the id is not in the users table, then you'll need to add it in an "unhydrated" form.
if tweet.get('in_reply_to_user_id',None) is not None:
sql=sqlalchemy.sql.text('''
INSERT INTO users
(id_users,screen_name)
VALUES
(:id_users,:screen_name)
ON CONFLICT DO NOTHING
''')
res = connection.execute(sql,{
'id_users':tweet['in_reply_to_user_id'],
'screen_name':tweet['in_reply_to_screen_name'],
})
# insert the tweet
sql=sqlalchemy.sql.text(f'''
INSERT INTO tweets
(id_tweets,id_users,created_at,in_reply_to_status_id,in_reply_to_user_id,quoted_status_id,geo,retweet_count,quote_count,favorite_count,withheld_copyright,withheld_in_countries,place_name,country_code,state_code,lang,text,source)
VALUES
(:id_tweets,:id_users,:created_at,:in_reply_to_status_id,:in_reply_to_user_id,:quoted_status_id,ST_GeomFromText(:geo_str || '(' || :geo_coords || ')'),:retweet_count,:quote_count,:favorite_count,:withheld_copyright,:withheld_in_countries,:place_name,:country_code,:state_code,:lang,:text,:source)
ON CONFLICT DO NOTHING;
''')
res = connection.execute(sql,{
'id_tweets':tweet['id'],
'id_users':tweet['user']['id'],
'created_at':tweet['created_at'],
'in_reply_to_status_id':tweet.get('in_reply_to_status_id',None),
'in_reply_to_user_id':tweet.get('in_reply_to_user_id',None),
'quoted_status_id':tweet.get('quoted_status_id',None),
'geo_coords':geo_coords,
'geo_str':geo_str,
'retweet_count':tweet.get('retweet_count',None),
'quote_count':tweet.get('quote_count',None),
'favorite_count':tweet.get('favorite_count',None),
'withheld_copyright':tweet.get('withheld_copyright',None),
'withheld_in_countries':tweet.get('withheld_in_countries',None),
'place_name':place_name,
'country_code':country_code,
'state_code':state_code,
'lang':tweet.get('lang'),
'text':remove_nulls(text),
'source':remove_nulls(tweet.get('source',None)),
})
########################################
# insert into the tweet_urls table
########################################
try:
urls = tweet['extended_tweet']['entities']['urls']
except KeyError:
urls = tweet['entities']['urls']
for url in urls:
id_urls = get_id_urls(url['expanded_url'])
sql=sqlalchemy.sql.text('''
INSERT INTO tweet_urls
(id_tweets,id_urls)
VALUES
(:id_tweets,:id_urls)
ON CONFLICT DO NOTHING
''')
res = connection.execute(sql,{
'id_tweets':tweet['id'],
'id_urls':id_urls,
})
########################################
# insert into the tweet_mentions table
########################################
try:
mentions = tweet['extended_tweet']['entities']['user_mentions']
except KeyError:
mentions = tweet['entities']['user_mentions']
for mention in mentions:
sql=sqlalchemy.sql.text('''
INSERT INTO users
(id_users,name,screen_name)
VALUES
(:id_users,:name,:screen_name)
ON CONFLICT DO NOTHING
''')
res = connection.execute(sql,{
'id_users':mention['id'],
'name':remove_nulls(mention['name']),
'screen_name':remove_nulls(mention['screen_name']),
})
sql=sqlalchemy.sql.text('''
INSERT INTO tweet_mentions
(id_tweets,id_users)
VALUES
(:id_tweets,:id_users)
ON CONFLICT DO NOTHING
''')
res = connection.execute(sql,{
'id_tweets':tweet['id'],
'id_users':mention['id']
})
########################################
# insert into the tweet_tags table
########################################
try:
hashtags = tweet['extended_tweet']['entities']['hashtags']
cashtags = tweet['extended_tweet']['entities']['symbols']
except KeyError:
hashtags = tweet['entities']['hashtags']
cashtags = tweet['entities']['symbols']
tags = [ '#'+hashtag['text'] for hashtag in hashtags ] + [ '$'+cashtag['text'] for cashtag in cashtags ]
for tag in tags:
sql=sqlalchemy.sql.text('''
INSERT INTO tweet_tags
(id_tweets,tag)
VALUES
(:id_tweets,:tag)
ON CONFLICT DO NOTHING
''')
res = connection.execute(sql,{
'id_tweets':tweet['id'],
'tag':remove_nulls(tag)
})
########################################
# insert into the tweet_media table
########################################
try:
media = tweet['extended_tweet']['extended_entities']['media']
except KeyError:
try:
media = tweet['extended_entities']['media']
except KeyError:
media = []
for medium in media:
id_urls = get_id_urls(medium['media_url'])
sql=sqlalchemy.sql.text('''
INSERT INTO tweet_media
(id_tweets,id_urls,type)
VALUES
(:id_tweets,:id_urls,:type)
ON CONFLICT DO NOTHING
''')
res = connection.execute(sql,{
'id_tweets':tweet['id'],
'id_urls':id_urls,
'type':medium['type']
})
# loop through file
# NOTE:
# we reverse sort the filenames because this results in fewer updates to the users table,
# which prevents excessive dead tuples and autovacuums
for filename in sorted(args.inputs, reverse=True):
with zipfile.ZipFile(filename, 'r') as archive:
#with connection.begin() as trans:
print(datetime.datetime.now(),filename)
for subfilename in sorted(archive.namelist(), reverse=True):
with io.TextIOWrapper(archive.open(subfilename)) as f:
for i,line in enumerate(f):
tweet = json.loads(line)
insert_tweet(connection,tweet)
# print message
if i%args.print_every==0:
print(datetime.datetime.now(),filename,subfilename,'i=',i,'id=',tweet['id'])