forked from DrSkippy/Gnacs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
2014-06_mysql.py
executable file
·432 lines (371 loc) · 17.1 KB
/
2014-06_mysql.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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
__author__="Scott Hendrickson, Josh Montague"
__license__="Simplified BSD"
import sys
from datetime import datetime
import re
import os
import codecs
import json
import acscsv
# for custom twitter output, import both the fields module and the code module
from twitter_acs_fields import *
import twitter_acs
#
# define any custom field extractor classes here. inherit from acscsv.* as needed
#
class Field_twitter_hashtags_text_DB(acscsv._LimitedField):
"""
Combine the first 'limit' hashtags found in the payload into a list and assign to
self.value. If there are less than 'limit', the list is padded with
self.default_value
"""
path = ["twitter_entities", "hashtags"]
fields = ["text"]
def __init__(self, json_record, limit=5):
super(
Field_twitter_hashtags_text_DB
, self).__init__(json_record, limit=limit)
class Field_twitter_symbols_text_DB(acscsv._LimitedField):
"""
Assign to self.value a list of twitter_entities.symbols 'text's.
This one is a little experimental... haven't seen it in the wild (Activity Streams) yet,
so using https://blog.twitter.com/2013/symbols-entities-tweets as the template.
"""
path = ["twitter_entities", "symbols"]
fields = ["text"]
def __init__(self, json_record, limit=5):
super(
Field_twitter_symbols_text_DB
, self).__init__(json_record, limit=limit)
class Field_twitter_mentions_id_name_DB(acscsv._LimitedField):
"""
Assign to self.value a list of 'limit' twitter_entities.user_mentions.screen_name and .id pairs
(in order, but in a flat list).
"""
path = ["twitter_entities", "user_mentions"]
fields = ["id", "screen_name"]
def __init__(self, json_record, limit=5):
super(
Field_twitter_mentions_id_name_DB
, self).__init__(json_record, limit=limit)
class Field_twitter_urls_tco_expanded_DB(acscsv._LimitedField):
"""
combination of two classes above
"""
path = ["twitter_entities", "urls"]
fields = ["url", "expanded_url"]
def __init__(self, json_record, limit=5):
super(
Field_twitter_urls_tco_expanded_DB
, self).__init__(json_record, limit=limit)
class Field_twitter_media_id_url_DB(acscsv._LimitedField):
"""
Assign to self.value a list of twitter_entities.media.id and .expanded_url pairs
"""
path = ["twitter_entities", "media"]
fields = ["id", "expanded_url"]
def __init__(self, json_record, limit=5):
super(
Field_twitter_media_id_url_DB
, self).__init__(json_record, limit=limit)
class Field_actor_links(acscsv._Field):
"""
Assign to self.value a string repr of a list of the links contained in actor.links (or
default_value if the corresponding dict is empty). The number of links included in the
list is optionally specified in the constructor.
"""
path = ["actor", "links"]
def __init__(self, json_record, limit=2):
super(
Field_actor_links
, self).__init__(json_record)
if self.value != self.default_value:
# ignore the links that are "null" in the payload ( ==> None in the dict )
self.value_list = [ x["href"] for x in self.value if x["href"] is not None ]
self.value_list = self.fix_length( self.value_list, limit )
#
# define your main subclass here (eg inherit from twitter_acs.TwacsCSV)
#
class CustomCSV( twitter_acs.TwacsCSV ):
"""
Customized output experiment: 2014-05-05 (JM)
Goal is for the parsed JSON to end up in separate, delimited files suitable for loading
into a relational database (MySQL to start).
Test class for experimenting with new output combinations. This class should inherit
from the appropriate module.class in the core library. Compliance and invalid records
are handled by the parent class' procRecord() method. This class should only define a
new get_output_list() method which overrides the parent method and determines the
custom output.
"""
split_flag = "GNIPSPLIT"
def combine_lists(self, *args):
"""
Takes an arbitrary number of lists to be combined into a single string for passing back
to main gnacs.py and subsequent splitting. The special delimiter for parsing the string
back into separate lists is defined in here.
"""
#flag = "GNIPSPLIT"
# append flag to the end of all but last list
[ x.append(self.split_flag) for i,x in enumerate(args) if i < (len(args) - 1) ]
# combine the lists into one
combined_list = []
[ combined_list.extend(x) for x in args ]
return combined_list
def get_output_list(self, d):
"""
Take a JSON Activity Streams payload as a Python dictionary.
Custom output format designed for loading multiple database tables. Creates N separate
lists of activity fields, joins them on a GNIPSPLIT to be split at the gnacs.py level
and written to separate files.
"""
# timestamp format
t_fmt = "%Y-%m-%d %H:%M:%S"
now = datetime.utcnow().strftime( t_fmt )
default_value = acscsv._Field({}).default_value
# hash_list will be [ id, tag1, id, tag2, ... ] and will be split in gnacs.py
hash_list = []
for i in Field_twitter_hashtags_text_DB(d).value_list:
if i != default_value: # temporary hack to exclude all the "NULL" values
hash_list.extend( [ Field_id(d).value, i ] )
acs_list = [
Field_id(d).value
, Field_gnip_rules(d).value
, now
, Field_postedtime(d).value
, Field_verb(d).value
, Field_actor_id(d).value
, Field_body(d).value
, Field_twitter_lang(d).value
, Field_gnip_language_value(d).value
, Field_link(d).value
, Field_generator_displayname(d).value
] \
+ Field_geo_coordinates(d).value_list \
+ Field_twitter_symbols_text_DB(d).value_list \
+ Field_twitter_mentions_id_name_DB(d).value_list \
+ Field_twitter_urls_tco_expanded_DB(d).value_list \
+ Field_twitter_media_id_url_DB(d).value_list
ustatic_list = [
now
, Field_actor_id(d).value
, Field_id(d).value # needs to be updated programatically in an actual app
, Field_actor_postedtime(d).value
, Field_actor_preferredusername(d).value
, Field_actor_displayname(d).value
, Field_actor_link(d).value
, Field_actor_summary(d).value
] \
+ Field_actor_links(d, limit=2).value_list \
+ [
Field_actor_twittertimezone(d).value
, Field_actor_utcoffset(d).value
, Field_actor_verified(d).value
, Field_actor_language(d).value
] \
+ Field_gnip_profilelocations_geo_coordinates(d).value_list \
+ [
Field_gnip_profilelocations_address_countrycode(d).value
, Field_gnip_profilelocations_address_locality(d).value
, Field_gnip_profilelocations_address_region(d).value
, Field_gnip_profilelocations_address_subregion(d).value
, Field_gnip_profilelocations_displayname(d).value
, Field_gnip_klout_profile_klout_user_id(d).value
]
udyn_list = [
Field_actor_id(d).value
, Field_id(d).value # needs to be updated programatically in an actual app
, Field_gnip_klout_score(d).value
] \
+ Field_gnip_klout_profile_topics(d).value_list \
+ [
Field_actor_statusesCount(d).value
, Field_actor_followerscount(d).value
, Field_actor_friendscount(d).value
, Field_actor_listedcount(d).value
]
output_list = self.combine_lists(
acs_list
, ustatic_list
, udyn_list
, hash_list
)
return output_list
#TODO: possible future extension of ^
# # this is the acs_list if all twitter entities are stripped out into separate tables
# acs_list = [
# Field_id(d).value
# , Field_gnip_rules(d).value
# , now
# , Field_postedtime(d).value
# , Field_verb(d).value
# , Field_actor_id(d).value
# , Field_body(d).value
# , Field_twitter_lang(d).value
# , Field_gnip_lang(d).value
# , Field_link(d).value
# , Field_generator_displayname(d).value
# ] \
# + Field_geo_coords(d).value_list \
#
#
# #
# sym_list = []
# for i in Field_twitter_symbols_text_DB(d).value_list:
# sym_list.extend( [ Field_id(d).value, i ] )
# #
# # nb: this isn't going to work yet. need to split out the actId, uid, uname triplets
# mentions_list = []
# for i in Field_twitter_mentions_id_name_DB(d).value_list:
# mentions_list.extend( [ Field_id(d).value, i ] )
# #
# urls_list = []
# media_list = []
def _multi_file_DB_1(self, d):
"""
!!
This is an early version of the output_list method... superceded by the one above.
This will definitely crash if you try to run it without updating various names
and classes
!!
Custom output format designed for loading multiple database tables.
Creates 3 separate lists of activity fields, joins them on a GNIPSPLIT to be split
at the gnacs.py level and written to separate files.
Superceded 2014-05-12 for more star-schema approach of e.g. hashtag/mention/link/... tables
(JM)
"""
# timestamp format
t_fmt = "%Y-%m-%d %H:%M:%S"
now = datetime.utcnow().strftime( t_fmt )
# the explicit .value attr reference is needed
acs_list = [
Field_id(d).value
, Field_gnip_rules(d).value
, now
, Field_postedtime(d).value
, Field_verb(d).value
, Field_actor_id(d).value
, Field_body(d).value
, Field_twitter_lang(d).value
, Field_gnip_lang(d).value
, Field_link(d).value
, Field_generator_displayname(d).value
] \
+ Field_geo_coords(d).value_list \
+ Field_twitter_hashtags_text_DB(d).value_list \
+ Field_twitter_symbols_text_DB(d).value_list \
+ Field_twitter_mentions_id_name_DB(d).value_list \
+ Field_twitter_urls_tco_expanded_DB(d).value_list \
+ Field_twitter_media_id_url_DB(d).value_list
ustatic_list = [
now
, Field_actor_id(d).value
, Field_id(d).value # needs to be updated programatically in an actual app
, Field_actor_postedtime(d).value
, Field_actor_preferredusername(d).value
, Field_actor_displayname(d).value
, Field_actor_acct_link(d).value
, Field_actor_summary(d).value
] \
+ Field_actor_links(d, limit=2).value_list \
+ [
Field_actor_twittertimezone(d).value
, Field_actor_utcoffset_DB(d).value
, Field_actor_verified(d).value
, Field_actor_lang(d).value
] \
+ Field_gnip_pl_geo_coords(d).value_list \
+ [
Field_gnip_pl_countrycode(d).value
, Field_gnip_pl_locality(d).value
, Field_gnip_pl_region(d).value
, Field_gnip_pl_subregion(d).value
, Field_gnip_pl_displayname(d).value
, Field_gnip_klout_user_id(d).value
]
udyn_list = [
Field_actor_id(d).value
, Field_id(d).value # needs to be updated programatically in an actual app
, Field_gnip_klout_score(d).value
] \
+ Field_gnip_klout_topics(d).value_list \
+ [
Field_actor_statuses_DB(d).value
, Field_actor_followers_DB(d).value
, Field_actor_friends_DB(d).value
, Field_actor_listed_DB(d).value
]
#
#TODO: replace with combine_lists() method above
#
# consider instead sending the combined list and an arbitrary list of positions to split it
#flag = "GNIPSPLIT" # this is hardcoded into gnacs.py, as well. change both if needed!
# only need mortar on first two bricks
[ x.append(self.split_flag) for x in acs_list, ustatic_list ]
combined_list = []
combined_list.extend(acs_list)
combined_list.extend(ustatic_list)
combined_list.extend(udyn_list)
#
return combined_list
if __name__ == "__main__":
"""
Receive data from stdin (decompressed JSON-lines only) and process using the existing
code, but let this module's get_output_list() method define the output fields.
"""
# Get the appropriate object by mocking the constructor in the main gnacs.py code. most
# common command-line options (flags) don't matter since we're explicitly defining the
# fields to be printed in the method above
# ex: TwacsCSV("|", None, *[True]*7)
processing_obj = CustomCSV("|", None, *[True]*7)
# set up files and dirs for output
#data_dir = os.environ['HOME'] + "/gnacs_db"
data_dir = os.environ["PWD"] + "/gnacs_db"
if not os.path.exists(data_dir):
os.mkdir(data_dir)
# open file objects for writing
acs_f = codecs.open( data_dir + '/table_activities.csv', 'wb', 'utf8')
ustatic_f = codecs.open( data_dir + '/table_users_static.csv', 'wb', 'utf8')
udyn_f = codecs.open( data_dir + '/table_users_dynamic.csv', 'wb', 'utf8')
hash_f = codecs.open( data_dir + '/table_hashtags.csv', 'wb', 'utf8')
line_number = 0
for r in sys.stdin:
line_number += 1
try:
recs = [json.loads(r.strip())]
except ValueError:
try:
# maybe a missing line feed?
recs = [json.loads(x) for x in r.strip().replace("}{", "}GNIP_SPLIT{").split("GNIP_SPLIT")]
except ValueError:
sys.stderr.write("Invalid JSON record (%d) %s, skipping\n"%(line_number, r.strip()))
continue
for record in recs:
if len(record) == 0:
continue
#
# TODO: swap procRecord call for get_source_list(), split accordingly, clean
# up next 30 loc
# record is parsed and returned as a single list, split and trimmed of delimiters
compRE = re.compile(r"GNIPREMOVE")
# choose the emptyField so it will convert to MySQL NULL
tmp_combined_rec = processing_obj.procRecord(record, emptyField="\\N")
if compRE.search(tmp_combined_rec):
sys.stderr.write("Skipping compliance activity: ({}) {}\n"
.format(line_number, tmp_combined_rec) )
continue
# otherwise, write to appropriate file objects (from above)
#flag = "GNIPSPLIT"
acs_str, ustatic_str, udyn_str, hash_str = tmp_combined_rec.split( processing_obj.split_flag )
# clean up any leading/trailing pipes
acs_str = acs_str.strip("|")
ustatic_str = ustatic_str.strip("|")
udyn_str = udyn_str.strip("|")
hash_str = hash_str.strip("|") # id|tag1|id|tag2|...
hash_list = re.findall("[^|]+\|[^|]+", hash_str) # [ 'id|tag1', 'id|tag2', ... ]
#
acs_f.write(acs_str + "\n")
ustatic_f.write(ustatic_str + "\n")
udyn_f.write(udyn_str + "\n")
[ hash_f.write(x + "\n") for x in hash_list ]