From 426b32d5cb40e5a1897e8cb697d41fb2b4ade86f Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Fri, 1 Nov 2024 00:36:30 +0100 Subject: [PATCH] Phone number normalization --- libpurple_to_markdown/synctech_sms.py | 16 ++++++++++++++-- test/test_synctech_sms.py | 10 ++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 test/test_synctech_sms.py diff --git a/libpurple_to_markdown/synctech_sms.py b/libpurple_to_markdown/synctech_sms.py index 62a3b32..a1a9114 100644 --- a/libpurple_to_markdown/synctech_sms.py +++ b/libpurple_to_markdown/synctech_sms.py @@ -17,18 +17,30 @@ from .data import MYSELF, Message logger = logging.getLogger(__name__) +def normalize_phone_number(num: str) -> str: + num = num.replace(' ', '') + if num.startswith('00'): + num = '+' + num.removeprefix('00') + if len(num) == 8: + num = '+45' + num + elif len(num) >= 10 and num[0] != '+': + num = '+' + num + return num + def sms_soup_to_message(soup: bs4.BeautifulSoup) -> Message: # TODO: Require myself sent_at = datetime.datetime.fromtimestamp(int(soup['date']) / 1000) + phone_num = normalize_phone_number(soup['address']) + contact_name = soup.get('contact_name') or phone_num if soup['type'] == '2': sender = MYSELF else: - sender = soup.get('contact_name') or soup['address'] + sender = contact_name text = soup['body'] - chat_id = 'SMS ' + soup['address'] + chat_id = f'SMS {contact_name} {phone_num}' return Message(sent_at, sender, text, chat_id=chat_id) diff --git a/test/test_synctech_sms.py b/test/test_synctech_sms.py new file mode 100644 index 0000000..c9349ad --- /dev/null +++ b/test/test_synctech_sms.py @@ -0,0 +1,10 @@ +from libpurple_to_markdown import synctech_sms + +def test_normalize_phone_number(): + assert synctech_sms.normalize_phone_number('+45 12 34 56 78') == '+4512345678' + assert synctech_sms.normalize_phone_number('+4512345678') == '+4512345678' + assert synctech_sms.normalize_phone_number('4512345678') == '+4512345678' + assert synctech_sms.normalize_phone_number('12345678') == '+4512345678' + assert synctech_sms.normalize_phone_number('12 34 56 78') == '+4512345678' + assert synctech_sms.normalize_phone_number('441234567890') == '+441234567890' + assert synctech_sms.normalize_phone_number('004712345678') == '+4712345678'