Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reactions Extraction from SQL Databas #79

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Whatsapp_Chat_Exporter/data_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def __init__(self, from_me: Union[bool,int], timestamp: int, time: Union[int,flo
self.caption = None
self.thumb = None # Android specific
self.sticker = False
self.reactions = None

def to_json(self):
return {
Expand All @@ -94,5 +95,6 @@ def to_json(self):
'quoted_data' : self.quoted_data,
'caption' : self.caption,
'thumb' : self.thumb,
'sticker' : self.sticker
'sticker' : self.sticker,
'reactions' : self.reactions
}
53 changes: 53 additions & 0 deletions Whatsapp_Chat_Exporter/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,14 @@ def messages(db, data, media_folder):
group_concat(receipt_user.read_timestamp) as read_timestamp,
group_concat(receipt_user.played_timestamp) as played_timestamp,
group_concat(messages.read_device_timestamp) as read_device_timestamp
group_concat(
message_add_on_reaction.reaction || ':' ||
CASE
WHEN message_add_on.sender_jid_row_id = -1 THEN 'S' || ':' || message_add_on.from_me
ELSE 'G' || ':' || jid.raw_string
END || ':' ||
message_add_on_reaction.sender_timestamp
) as reactions
FROM messages
LEFT JOIN messages_quotes
ON messages.quoted_row_id = messages_quotes._id
Expand All @@ -225,6 +233,12 @@ def messages(db, data, media_folder):
ON jid_new._id = message_system_number_change.new_jid_row_id
LEFT JOIN receipt_user
ON receipt_user.message_row_id = messages._id
LEFT JOIN message_add_on
ON message._id = message_add_on.parent_message_row_id
LEFT JOIN message_add_on_reaction
ON message_add_on._id = message_add_on_reaction.message_add_on_row_id
LEFT JOIN jid
ON message_add_on.sender_jid_row_id = jid._id
WHERE messages.key_remote_jid <> '-1'
GROUP BY message._id;"""
)
Expand Down Expand Up @@ -258,6 +272,14 @@ def messages(db, data, media_folder):
group_concat(message.received_timestamp) as received_timestamp,
group_concat(receipt_user.read_timestamp) as read_timestamp,
group_concat(receipt_user.played_timestamp) as played_timestamp
group_concat(
message_add_on_reaction.reaction || ':' ||
CASE
WHEN message_add_on.sender_jid_row_id = -1 THEN 'S' || ':' || message_add_on.from_me
ELSE 'G' || ':' || jid.raw_string
END || ':' ||
message_add_on_reaction.sender_timestamp
) as reactions
FROM message
LEFT JOIN message_quoted
ON message_quoted.message_row_id = message._id
Expand Down Expand Up @@ -289,6 +311,12 @@ def messages(db, data, media_folder):
ON jid_new._id = message_system_number_change.new_jid_row_id
LEFT JOIN receipt_user
ON receipt_user.message_row_id = message._id
LEFT JOIN message_add_on
ON message._id = message_add_on.parent_message_row_id
LEFT JOIN message_add_on_reaction
ON message_add_on._id = message_add_on_reaction.message_add_on_row_id
LEFT JOIN jid
ON message_add_on.sender_jid_row_id = jid._id
WHERE key_remote_jid <> '-1'
GROUP BY message._id;"""
)
Expand Down Expand Up @@ -361,6 +389,31 @@ def messages(db, data, media_folder):
else:
message.reply = None

if content["reactions"] is not None:
# Split the concatenated string into elements (Reactions)
elements = content['reactions'].split(',')
# Process each element
processed_elements = []
for element in elements:
attributes = element.split(':')
# Each element has 4 attributes: reaction, self/group flag, reactor, timestamp
if len(attributes) == 4:
# Identify sender
# sg_flag: "G" is for non-self group-reactor, "S" is for self-reactor
# reactor: group reactor information; otherwise, reactor is 0 / 1, a "from_you" tag
reaction, sg_flag, reactor, timestamp = attributes
if sg_flag == 'G':
reactor_tag = reactor
else:
reactor_tag = "You" if reactor == "1" else content["key_remote_jid"]
element_dict = {
"reaction": reaction,
"reactor": reactor_tag,
"timestamp": timestamp
}
processed_elements.append(element_dict)
message.reactions = processed_elements

if not table_message and content["media_caption"] is not None:
# Old schema
message.caption = content["media_caption"]
Expand Down