1
0
libpurple-to-markdown/libpurple_to_markdown/synctech_sms.py

44 lines
1.1 KiB
Python
Raw Normal View History

"""Backend for SyncTech Backup & Restore.
[SyncTech Backup & Restore](https://www.synctech.com.au/sms-backup-restore/)
for Android is a free app for backing up your SMS and MMS messages. It uses an
XML format as backup format, which this backend reads and converts to the
standardized Message format.
"""
2024-10-31 21:38:22 +00:00
import datetime
import logging
2024-10-31 19:36:38 +00:00
from collections.abc import Iterator
from pathlib import Path
import bs4
2024-10-31 21:38:22 +00:00
from .data import MYSELF, Message
logger = logging.getLogger(__name__)
2024-10-31 19:36:38 +00:00
def sms_soup_to_message(soup: bs4.BeautifulSoup) -> Message:
# TODO: Require myself
2024-10-31 21:38:22 +00:00
sent_at = datetime.datetime.fromtimestamp(int(soup['date']) / 1000)
2024-10-31 19:36:38 +00:00
if soup['type'] == '2':
2024-10-31 21:38:22 +00:00
sender = MYSELF
2024-10-31 19:36:38 +00:00
else:
2024-10-31 21:38:22 +00:00
sender = soup.get('contact_name') or soup['address']
2024-10-31 19:36:38 +00:00
text = soup['body']
chat_id = 'SMS ' + soup['address']
2024-10-31 21:38:22 +00:00
return Message(sent_at, sender, text, chat_id=chat_id)
2024-10-31 19:36:38 +00:00
def parse_messages_in_backup_xml_file(path: Path) -> Iterator[Message]:
logger.info('Parsing %s', path)
with open(path) as f:
soup = bs4.BeautifulSoup(f, 'lxml-xml')
2024-10-31 19:36:38 +00:00
for sms in soup.find_all('sms'):
yield sms_soup_to_message(sms)
del sms