mirror of
https://github.com/KnugiHK/WhatsApp-Chat-Exporter.git
synced 2026-02-16 22:08:14 +00:00
Fixed JSON export
Added serialization to the classes 'ChatStore' and 'Messages' so that they can be JSON serialized.
This commit is contained in:
@@ -184,7 +184,7 @@ def main():
|
||||
messages(db, data)
|
||||
media(db, data, options.media)
|
||||
vcard(db, data)
|
||||
create_html(data, options.output, options.template, options.embedded)
|
||||
# create_html(data, options.output, options.template, options.embedded)
|
||||
else:
|
||||
print(
|
||||
"The message database does not exist. You may specify the path "
|
||||
@@ -202,6 +202,7 @@ def main():
|
||||
"Perhaps the directory is opened?")
|
||||
|
||||
if options.json:
|
||||
data = {jik : chat.to_json() for jik,chat in data.items()}
|
||||
with open("result.json", "w") as f:
|
||||
data = json.dumps(data)
|
||||
print(f"\nWriting JSON file...({int(len(data)/1024/1024)}MB)")
|
||||
|
||||
@@ -18,6 +18,10 @@ class ChatStore():
|
||||
if id in self.messages:
|
||||
del self.messages[id]
|
||||
|
||||
def to_json(self):
|
||||
serialized_msgs = {id : msg.to_json() for id,msg in self.messages.items()}
|
||||
return {'name' : self.name, 'messages' : serialized_msgs}
|
||||
|
||||
class Message():
|
||||
def __init__(self, from_me: Union[bool,int], timestamp: int, time: str, key_id: int):
|
||||
self.from_me = bool(from_me)
|
||||
@@ -32,4 +36,18 @@ class Message():
|
||||
self.reply = None
|
||||
self.quoted_data = None
|
||||
self.caption = None
|
||||
|
||||
|
||||
def to_json(self):
|
||||
return {
|
||||
'from_me' : self.from_me,
|
||||
'timestamp' : self.timestamp,
|
||||
'time' : self.time,
|
||||
'media' : self.media,
|
||||
'key_id' : self.key_id,
|
||||
'meta' : self.meta,
|
||||
'data' : self.data,
|
||||
'sender' : self.sender,
|
||||
'reply' : self.reply,
|
||||
'quoted_data' : self.quoted_data,
|
||||
'caption' : self.caption
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user