1
0
libpurple-to-markdown/libpurple_to_markdown/synctech_sms.py
Jon Michael Aanes 57cac8daa1
All checks were successful
Run Python tests (through Pytest) / Test (push) Successful in 25s
Verify Python project can be installed, loaded and have version checked / Test (push) Successful in 22s
Implement SMS to message conversoin
2024-10-31 20:36:38 +01:00

43 lines
1.1 KiB
Python

"""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.
"""
import datetime
import logging
from collections.abc import Iterator
from pathlib import Path
import bs4
from .data import Message, MYSELF
logger = logging.getLogger(__name__)
def sms_soup_to_message(soup: bs4.BeautifulSoup) -> Message:
# TODO: Require myself
sent_at = datetime.datetime.fromtimestamp(int(soup['date'])/1000)
if soup['type'] == '2':
sender=MYSELF
else:
sender=soup.get('contact_name') or soup['address']
text = soup['body']
chat_id = 'SMS ' + soup['address']
return Message(sent_at,sender, text, chat_id = chat_id)
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')
for sms in soup.find_all('sms'):
yield sms_soup_to_message(sms)
del sms