Skip to content
This repository has been archived by the owner on Mar 22, 2021. It is now read-only.

Update to latest backup from Google #21

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
2 changes: 1 addition & 1 deletion attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Attachment:
photo_id = None # Google Photos ID (not used)
media_type = None # Type of attachment: PHOTO, ANIMATED_PHOTO, VIDEO
original_content_url = None
download_url = None # Not used, since it doesn't seem to work
download_url = None # This is used to get file extension

def __init__(self, album_id=None, photo_id=None, media_type=None, original_content_url=None, download_url=None):
self.album_id = album_id
Expand Down
18 changes: 11 additions & 7 deletions hangouts_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
from conversation import Conversation
from attachment import Attachment

def ns_to_dict(ns):
if isinstance(ns, list): return [ ns_to_dict(i) for i in ns ]
if isinstance(ns, Namespace): return { k: ns_to_dict(v) for k,v in vars(ns).items() }
return ns

class HangoutsParser:
"""Parses the Google Takeout JSON export for Hangouts SMS/MMS messages."""
Expand All @@ -18,13 +22,13 @@ def parse_input_file(self, hangouts_file_name, user_phone_number):
"""
conversations = []
self_gaia_id = None # gaia_id for the phone owner
with open(hangouts_file_name, 'rb') as data_file:
with open(hangouts_file_name, 'rt', encoding='utf-8') as data_file:
# Read the Hangouts JSON file and turn into objects
data = json.load(data_file, object_hook=lambda d: Namespace(**d))
# Iterate through each conversation in the list
for conversation_state in data.conversation_state:
# Iterate through each conversation in the listi
for conversation_state in data.conversations:
# Get the nested conversation_state
state = getattr(conversation_state, "conversation_state", None)
state = getattr(conversation_state, "conversation", None)
if state is not None:
# Get the conversation object
conversation = getattr(state, "conversation", None)
Expand Down Expand Up @@ -54,7 +58,7 @@ def parse_input_file(self, hangouts_file_name, user_phone_number):
read_state, user_phone_number,
self_gaia_id)
# Get the conversation messages
events = getattr(state, "event", None)
events = getattr(conversation_state, "events", None)
if events is not None:
current_conversation.messages = self._process_messages(events)
conversations.append(current_conversation)
Expand Down Expand Up @@ -168,14 +172,14 @@ def _process_message_attachments(self, attachment_list):
for current_attachment in attachment_list:
embed_item = getattr(current_attachment, "embed_item", None)
if embed_item is not None:
plus_photo = getattr(embed_item, "embeds.PlusPhoto.plus_photo", None)
plus_photo = getattr(embed_item, "plus_photo", None)
if plus_photo is not None:
current_attachment = Attachment()
current_attachment.album_id = self._try_int_attribute(plus_photo, "album_id")
current_attachment.photo_id = self._try_int_attribute(plus_photo, "photo_id")
current_attachment.media_type = getattr(plus_photo, "media_type", None)
current_attachment.original_content_url = getattr(plus_photo, "original_content_url", None)
current_attachment.download_url = getattr(plus_photo, "download_url", None)
current_attachment.download_url = getattr(plus_photo, "url", None)
attachments.append(current_attachment)
return attachments

Expand Down
8 changes: 6 additions & 2 deletions titanium_backup_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,12 @@ def create_output_file(self, conversations, self_gaia_id, output_file_name):
if attachment.original_content_url is not None:
data = self._convert_url_to_base64_data(attachment.original_content_url)
if data is not None:
message_string += MMS_PART.format("image/jpeg", order, "base64", data)
order += 1
if ".png" in attachment.download_url:
message_string += MMS_PART.format("image/png", order, "base64", data)
order += 1
else:
message_string += MMS_PART.format("image/jpeg", order, "base64", data)
order += 1
else:
print("Error: unable to download image data!")
elif attachment.media_type == "ANIMATED_PHOTO":
Expand Down