-
Notifications
You must be signed in to change notification settings - Fork 1
/
combiner_config.py
227 lines (188 loc) · 7.03 KB
/
combiner_config.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
from collections import namedtuple
import datetime as dt
import pytz
CombinerConfig = namedtuple("CombinerConfig", ["input_path",
"output_directory", "output_file_name",
"file_list", "output_columns", "file_to_rows"])
def map_messages(identifier, participants, message, timezone="America/Los_Angeles"):
message['identifier'] = identifier
message['participants'] = participants
la_timezone = pytz.timezone(timezone)
message['timestamp_iso'] = la_timezone.localize(dt.datetime.utcfromtimestamp(message['timestamp_ms']/1000)).isoformat()
message['content'] = message['content'].replace('\n', ' ') if 'content' in message else ''
return message
def message_file_to_rows(file, filename):
participants = '-'.join(list(map(lambda x: x['name'], file["participants"])))
identifier = file["thread_path"]
messages = file["messages"]
mapped_messages = list(
map(lambda message: map_messages(identifier, participants, message), messages)
)
return mapped_messages
MessagesConfig = CombinerConfig(
input_path="data/facebook/messages",
output_directory='output',
output_file_name="messages.csv",
file_list=["message_1.json"],
output_columns=[
"identifier",
"participants",
"content",
"timestamp_ms",
"timestamp_iso",
"sender_name",
"type"
],
file_to_rows=message_file_to_rows
)
def map_posts(message, timezone="America/Los_Angeles"):
message['identifier'] = message['timestamp']
message['title'] = message['title'] if 'title' in message else 'Missing Title'
la_timezone = pytz.timezone(timezone)
message['timestamp_iso'] = la_timezone.localize(dt.datetime.utcfromtimestamp(message['timestamp'])).isoformat()
for data in message['data'] if 'data' in message else []:
if 'post' in data:
message['content'] = data['post'].replace('\n', ' ')
if 'content' not in message:
message['content'] = 'Empty'
return message
def post_file_to_rows(file,filename):
posts = file
mapped_messages = list(
map(lambda message: map_posts(message), posts)
)
return mapped_messages
PostsConfig = CombinerConfig(
input_path="data/facebook/posts",
output_directory='output',
output_file_name="posts.csv",
file_list=[
"your_posts_" + str(x) + ".json" for x in range(1, 10)
],
output_columns=[
"identifier",
"content",
"timestamp",
"timestamp_iso",
"title"
],
file_to_rows=post_file_to_rows
)
def map_locations(message, timezone="America/Los_Angeles"):
message['identifier'] = message['creation_timestamp']
message['longitude'] = message['coordinate']['longitude']
message['latitude'] = message['coordinate']['latitude']
message['title'] = message['title'] if 'title' in message else 'Missing Title'
la_timezone = pytz.timezone(timezone)
message['timestamp_iso'] = la_timezone.localize(dt.datetime.utcfromtimestamp(message['creation_timestamp'])).isoformat()
return message
def location_file_to_rows(file, filename):
locations = file['location_history']
mapped_messages = list(
map(lambda message: map_locations(message), locations)
)
return mapped_messages
LocationsConfig = CombinerConfig(
input_path="data/facebook/location",
output_directory='output',
output_file_name="locations.csv",
file_list=[
"location_history.json"
],
output_columns=[
"identifier",
"name",
"creation_timestamp",
"timestamp_iso",
"longitude",
"latitude"
],
file_to_rows=location_file_to_rows
)
def map_likes(message, like_type, timezone="America/Los_Angeles"):
message['identifier'] = message['timestamp']
content_types = ['post', 'comment', 'photo', 'video', 'album', 'note', 'link', 'activity', 'GIF', 'life event']
if like_type == 'reactions':
valid_content_types = list(filter(lambda x: x in message['title'], content_types))
message['title'] = message['title'] if 'title' in message else 'Missing Title'
message['content_type'] = valid_content_types[0] if len(valid_content_types) > 0 else 'No valid content type'
reaction_type = 'LIKE'
for data in message['data']:
if 'reaction' in data:
reaction_type = data['reaction']['reaction']
message['reaction_type'] = reaction_type
if like_type == 'page':
message['title'] = message['name'] if 'name' in message else 'Missing Title'
message['content_type'] = 'page'
message['reaction_type'] = 'LIKE'
message['like_type'] = like_type
la_timezone = pytz.timezone(timezone)
message['timestamp_iso'] = la_timezone.localize(dt.datetime.utcfromtimestamp(message['timestamp'])).isoformat()
return message
def likes_file_to_rows(file, filename):
JSON_PARSE_KEY = {
'pages.json': 'page_likes',
'posts_and_comments.json': 'reactions'
}
LIKE_TYPE_DICT = {
'pages.json': 'page',
'posts_and_comments.json': 'reactions'
}
likes = file[JSON_PARSE_KEY[filename]]
mapped_messages = list(
map(lambda message: map_likes(message, LIKE_TYPE_DICT[filename]), likes)
)
return mapped_messages
LikesConfig = CombinerConfig(
input_path="data/facebook/likes_and_reactions",
output_directory='output',
output_file_name="likes_and_reactions.csv",
file_list=[
"posts_and_comments.json"
],
output_columns=[
"identifier",
"like_type",
"reaction_type",
"content_type",
"timestamp",
"timestamp_iso",
"title"
],
file_to_rows=likes_file_to_rows
)
def map_comments(comment, timezone='America/Los_Angeles'):
comment['identifier'] = comment['timestamp']
content = 'Missing content'
author = 'Missing Author'
if 'data' in comment and len(comment['data']) > 0:
comment_object = comment['data'][0]['comment']
content = comment_object['comment']
author = comment_object['author'] if 'author' in comment_object else 'Missing Author'
comment['author'] = author
comment['content'] = content
la_timezone = pytz.timezone(timezone)
comment['timestamp_iso'] = la_timezone.localize(dt.datetime.utcfromtimestamp(comment['timestamp'])).isoformat()
return comment
def comments_file_to_rows(file, filename):
comments = file['comments']
mapped_messages = list(
map(lambda message: map_comments(message), comments)
)
return mapped_messages
CommentsConfig = CombinerConfig(
input_path="data/facebook/comments",
output_directory='output',
output_file_name="comments.csv",
file_list=[
"comments.json"
],
output_columns=[
"timestamp",
"title",
"content",
"author",
"timestamp_iso"
],
file_to_rows=comments_file_to_rows
)