Skip to content

Commit

Permalink
Merge branch 'pr/26' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
KnugiHK committed Mar 25, 2023
2 parents 2ba5571 + 8f0511a commit 430a5ec
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Whatsapp_Chat_Exporter/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from Whatsapp_Chat_Exporter import extract_new as extract
from Whatsapp_Chat_Exporter import extract_iphone
from Whatsapp_Chat_Exporter import extract_iphone_media
from Whatsapp_Chat_Exporter.data_model import ChatStore
from Whatsapp_Chat_Exporter.extract_new import Crypt
from argparse import ArgumentParser
import os
Expand Down Expand Up @@ -246,6 +247,8 @@ def main():
"Perhaps the directory is opened?")

if args.json:
if isinstance(data[next(iter(data))], ChatStore):

This comment has been minimized.

Copy link
@KnugiHK

KnugiHK Mar 25, 2023

Author Owner

I also added a check before serialize the latest data model, so that the it is compatible with old model.

data = {jik: chat.to_json() for jik, chat in data.items()}
with open(args.json, "w") as f:
data = json.dumps(data)
print(f"\nWriting JSON file...({int(len(data)/1024/1024)}MB)")
Expand Down
20 changes: 19 additions & 1 deletion Whatsapp_Chat_Exporter/data_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def delete_message(self, id):
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)
Expand All @@ -32,4 +36,18 @@ def __init__(self, from_me: Union[bool,int], timestamp: int, time: str, key_id:
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
}

0 comments on commit 430a5ec

Please sign in to comment.