diff options
-rw-r--r-- | snaparser.py | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/snaparser.py b/snaparser.py new file mode 100644 index 0000000..eead386 --- /dev/null +++ b/snaparser.py @@ -0,0 +1,70 @@ +import json + +path = '' + +chat_history = json.load(open(path + 'chat_history.json')) + +received_saved = chat_history['Received Saved Chat History'] + +sent_saved = chat_history['Sent Saved Chat History'] + +conversations = {} + +def clean_message_in_chat(chat): + message = chat['Text'] + cleaned = message.replace("'", "'") + chat['Text'] = cleaned + return chat + +for received in received_saved: + if (received['Media Type'] != 'MEDIA'): + chat = received['From'] + cleaned_chat = clean_message_in_chat(received) + if chat in conversations: + conversations[chat]['messages'] += [cleaned_chat] + else: + conversations[chat] = { + 'messages': [cleaned_chat] + } + +for sent in sent_saved: + if (sent['Media Type'] != 'MEDIA'): + chat = sent['To'] + cleaned_chat = clean_message_in_chat(sent) + if chat in conversations: + conversations[chat]['messages'] += [cleaned_chat] + else: + conversations[chat] = { + 'messages': [cleaned_chat] + } + +for chat, content in conversations.items(): + messages = content['messages'] + msgs_sorted_by_date = sorted(messages, key = lambda x:x['Created']) + content['messages'] = msgs_sorted_by_date + +for idx, chat in enumerate(conversations): + print(f"{idx}: {chat} (Messages: {len(conversations[chat]['messages'])})") + +selected_chat_idx = input("Select a chat: ") +selected_chat = list(conversations.values())[int(selected_chat_idx)] + +messages = selected_chat['messages'] +for message in messages: + print("\n") + message_content = message['Text'] + time = message['Created'] + if 'From' in message: + recipient = message['From'] + print(message_content) + print(f"From: {recipient}") + print(time) + else: + recipient = message['To'] + if (len(message_content) >= 150): + for submessage in list(message_content[0+i:150+i] for i in range(0, len(message_content), 150)): + print(submessage.rjust(150)) + else: + print(message_content.rjust(150)) + print(f"To: {recipient}".rjust(150)) + print(time.rjust(150)) |