-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathwhatsapp_chat_parser.py
More file actions
81 lines (63 loc) · 2.5 KB
/
Copy pathwhatsapp_chat_parser.py
File metadata and controls
81 lines (63 loc) · 2.5 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
#!/usr/bin/env python3
# coding=utf-8
# *****************************************************
# whatsapp-chat-parser: Parse WhatsApp exported chats.
# Author:
# Mazin Ahmed <mazin at mazinahmed dot net>
# https://github.com/mazen160/whatsapp-chat-parser
# *****************************************************
from datetime import datetime
import re
REGEX_DATE = """^(\u200e){0,1}\[[0-9\-\:]+, [0-9:]+\W(AM|PM)\]"""
REGEX_CONTACT = f"{REGEX_DATE}(.+?):\W"
REGEX_MESSAGE = f"""{REGEX_DATE}.+:\W(.+)"""
DATE_FORMAT = '[%Y-%m-%d, %I:%M:%S %p]'
def __parse_timestamp(s, date_format):
return datetime.strptime(s, date_format)
def __remove_chars(s):
chars = ["\u202a", "\u202c", "\xa0", "\u2011", "\u200e", "\u202f"]
for c in chars:
s = s.replace(c, "")
return s
def get_messages(chat_export_path, date_format=DATE_FORMAT):
f = open(chat_export_path, "r")
original_messages = f.read().split("\n")
f.close()
messages = []
descriptive_messages = []
for m in original_messages:
data = {"timestamp": "",
"original_date": "",
"author": "",
"message": ""}
if not re.match(REGEX_DATE, m):
# Newline
messages[-1]["message"] += "\n" + m
continue
if m.startswith("\u200e"):
m = m[1:]
original_timestamp = re.search(REGEX_DATE, m).group(0)
data["original_date"] = original_timestamp
data["timestamp"] = __parse_timestamp(original_timestamp, date_format)
contact_original = re.search(REGEX_CONTACT, m)
message_original = re.search(REGEX_MESSAGE, m)
if not message_original:
message_original = ""
else:
message_original = message_original.groups()[-1]
if contact_original is None:
descriptive_messages.append(__remove_chars(m))
continue
if "security code changed." in m or "Your security code with" in m:
# Security code changed
descriptive_messages.append(__remove_chars(m))
continue
data["author"] = contact_original.group(3)
data["message"] = message_original
if data["message"].startswith("\u200e"):
data["message"] = data["message"][1:]
data["author"] = __remove_chars(data["author"])
if data["author"].startswith(" "):
data["author"] = data["author"][1:]
messages.append(data)
return {"chats": messages, "descriptive_messages": descriptive_messages}