-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhangouts.py
452 lines (371 loc) · 12.9 KB
/
hangouts.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import os
import argparse
import json
from datetime import datetime
VERSION = "0.1"
class Participant(object):
"""
Participant class.
"""
def __init__(self, gaia_id, chat_id, name, phone):
"""
Constructor
"""
self.name = name
self.phone = phone
self.gaia_id = gaia_id
self.chat_id = chat_id
def get_id(self):
"""
Getter for the internal Google ID of the participant.
@return internal Google participant id
"""
return self.gaia_id
def get_phone(self):
"""
Getter for the phone of a participant.
@return phone of the participant (may be None)
"""
return self.phone
def get_name(self):
"""
Getter for the name of a participant.
@return name of the participant (may be None)
"""
return self.name
def __unicode__(self):
"""
@return name of the participant or its id if name is None
"""
if self.get_name() is None:
if self.get_phone() is None:
return self.get_id()
else:
return self.get_phone()
else:
#return "%s <%s>" % (self.get_name(), self.get_id())
return self.get_name()
class ParticipantList(object):
"""
List class for participants.
"""
def __init__(self):
"""
Constructor
"""
self.p_list = {}
self.current_iter = 0
self.max_iter = 0
def add(self, p):
"""
Adds a participant to the list.
@return the participant list
"""
self.p_list[p.get_id()] = p
return self.p_list
def get_by_id(self, id):
"""
Queries a participant by its id.
@return the participant or None if id is not listed
"""
try:
return self.p_list[id]
except:
return None
def __iter__(self):
self.current_iter = 0
self.max_iter = len(self.p_list)-1
return self
def next(self):
if self.current_iter > self.max_iter:
raise StopIteration
else:
self.current_iter += 1
return self.p_list.values()[self.current_iter-1]
def __unicode__(self):
"""
@return names of the participants seperated by a comma
"""
string = ""
for p in self.p_list.values():
string += unicode(p) + ", "
return string[:-2]
class Event(object):
"""
Event class.
"""
def __init__(self, event_id, sender_id, timestamp, message):
"""
Constructor
"""
self.event_id = event_id
self.sender_id = sender_id
self.timestamp = timestamp
self.message = message
def get_id(self):
"""
Getter method for the id.
@return event id
"""
return self.event_id
def get_sender_id(self):
"""
Getter method for the sender id.
@return sender id of the event
"""
return self.sender_id
def get_timestamp(self):
"""
Getter method for the timestamp.
@return timestamp of the event
"""
return self.timestamp
def get_message(self):
"""
Getter method for the message.
@return message (list)
"""
return self.message
def get_picture(self):
"""
Getter method for the picture.
@return picture (url)
"""
return self.message
def get_formatted_message(self):
"""
Getter method for a formatted message (the messages are joined by a space).
@return message (string)
"""
string = ""
for m in self.message:
string += m + " "
return string[:-1]
class EventList(object):
"""
Event list class
"""
def __init__(self):
"""
Constructor
"""
self.event_list = {}
self.current_iter = 0
self.max_iter = 0
def add(self, e):
"""
Adds an event to the event list
@return event list
"""
self.event_list[e.get_id()] = e
return self.event_list
def get_by_id(self, id):
"""
Getter method for an event by its id.
@returns event
"""
try:
return self.event_list[id]
except:
return None
def __iter__(self):
self.current_iter = 0
self.max_iter = len(self.event_list)-1
return self
def next(self):
if self.current_iter > self.max_iter:
raise StopIteration
else:
self.current_iter += 1
return self.event_list.values()[self.current_iter-1]
class Conversation(object):
"""
Conversation class
"""
def __init__(self, conversation_id, timestamp, participants, events):
"""
Constructor
"""
self.conversation_id = conversation_id
self.timestamp = timestamp
self.participants = participants
self.events = events
def __str__(self):
"""
Prints to string
"""
return self.conversation_id
def get_id(self):
"""
Getter method for the conversation id
@return conversation id
"""
return self.conversation_id
def get_timestamp(self):
"""
Getter method for the timestamp
@return timestamp
"""
return self.timestamp
def get_participants(self):
"""
Getter method for the participants.
@return participants of the conversation
"""
return self.participants
def get_events(self):
"""
Getter method for the sorted events.
@return events of the conversation (sorted)
"""
return sorted(self.events, key=lambda event: event.get_timestamp())
def get_events_unsorted(self):
"""
Getter method for the unsorted events.
@return events of the conversation (unsorted)
"""
return self.events
class HangoutsReader(object):
"""
Hangouts reader class
"""
def __init__(self, logfile, verbose_mode=False, print_output_mode=True, conversation_id=None):
"""
Constructor
"""
self.filename = self.validate_file(logfile)
self.verbose_mode = verbose_mode
self.conversation_id = conversation_id
self.print_output_mode = print_output_mode
self.conversations = []
self.conversations_data = {}
# parse the json file
self.parse_json_file(logfile, self.print_output_mode)
def parse_json_file(self, filename, print_output_mode):
"""
Parses the json file. Prints the conversation list or a complete conversation depending on the users choice.
@return None
"""
with open(filename) as json_data:
self.print_v("Analyzing json file ...")
data = json.load(json_data)
for conversation in data["conversation_state"]:
c = self.extract_conversation_data(conversation)
if self.conversation_id is None and print_output_mode:
self.print_("conversation id: %s, participants: %s" % (c.get_id(), unicode(c.get_participants())))
elif c.get_id() == self.conversation_id:
self.conversations_data[c.get_id()] = self.print_conversation(c)
self.conversations.append(c)
def print_conversation(self, conversation):
"""
Prints conversations in human readable format.
@return None
"""
participants = conversation.get_participants()
data = [] # my additions (clean up)
for event in conversation.get_events():
if self.print_output_mode:# my additions (clean up)
self.print_("%(timestamp)s: <%(author)s> %(message)s" % \
{
"timestamp": datetime.fromtimestamp(long(long(event.get_timestamp())/10**6.)),
"author": participants.get_by_id(event.get_sender_id()).get_name(),
"message": event.get_formatted_message()
})
# my additions (needs to be cleaner)
time = datetime.fromtimestamp(long(long(event.get_timestamp())/10**6.))
author = participants.get_by_id(event.get_sender_id()).get_name()
message = event.get_formatted_message()
data.append([author,message,time])
return data
def extract_conversation_data(self, conversation):
"""
Extracts the data that belongs to a single conversation.
@return Conversation object
"""
try:
# note the initial timestamp of this conversation
initial_timestamp = conversation["response_header"]["current_server_time"]
conversation_id = conversation["conversation_id"]["id"]
# find out the participants
participant_list = ParticipantList()
for participant in conversation["conversation_state"]["conversation"]["participant_data"]:
gaia_id = participant["id"]["gaia_id"]
chat_id = participant["id"]["chat_id"]
try:
name = participant["fallback_name"]
except KeyError:
name = None
try:
phone = participant["phone_number"]["e164"]
except KeyError:
phone = None
p = Participant(gaia_id,chat_id,name,phone)
participant_list.add(p)
event_list = EventList()
for event in conversation["conversation_state"]["event"]:
event_id = event["event_id"]
sender_id = event["sender_id"] # has dict values "gaia_id" and "chat_id"
timestamp = event["timestamp"]
text = list()
try:
message_content = event["chat_message"]["message_content"]
try:
for segment in message_content["segment"]:
if segment["type"].lower() in ("TEXT".lower(), "LINK".lower()):
text.append(segment["text"])
except KeyError:
pass # may happen when there is no (compatible) attachment
try:
for attachment in message_content["attachment"]:
# if there is a Google+ photo attachment we append the URL
if attachment["embed_item"]["type"][0].lower() == "PLUS_PHOTO".lower():
text.append(attachment["embed_item"]["embeds.PlusPhoto.plus_photo"]["url"])
except KeyError:
pass
except KeyError:
continue # that's okay
# finally add the event to the event list
event_list.add(Event(event_id, sender_id["gaia_id"], timestamp, text))
except KeyError:
raise RuntimeError("The conversation data could not be extracted.")
return Conversation(conversation_id, initial_timestamp, participant_list, event_list)
def validate_file(self, filename):
"""
Checks if a file is valid or not.
Raises a ValueError if the file could not be found.
@return filename if everything is fine
"""
if not os.path.isfile(filename):
raise ValueError("The given file is not valid.")
return filename
def print_v(self, message):
"""
Prints a message if verbose mode is activated.
@return None
"""
if self.verbose_mode:
self.print_(message)
def print_(self, message):
"""
Prints a message with suffix in front of the message.
@return None
"""
print "[%s] %s" % (os.path.basename(__file__), unicode(message).encode('utf8'))
def write_(self, message, file):
"""
Writes the message to file with suffix in front of the message.
@return None
"""
file.write( "[%s] %s" % (os.path.basename(__file__), unicode(message).encode('utf8')))
def main(argv):
parser = argparse.ArgumentParser(description='Commandline python script that allows reading Google Hangouts logfiles. Version: %s' % VERSION)
parser.add_argument('logfile', type=str, help='filename of the logfile')
parser.add_argument('--conversation-id', '-c', type=str, help='shows the conversation with given id')
parser.add_argument('--verbose', '-v', action="store_true", help='activates the verbose mode')
args = parser.parse_args()
hr = HangoutsReader(args.logfile, verbose_mode=args.verbose, conversation_id=args.conversation_id)
if __name__ == "__main__":
main(sys.argv)