1
0

Phone number normalization

This commit is contained in:
Jon Michael Aanes 2024-11-01 00:36:30 +01:00
parent 4ca4382a42
commit 426b32d5cb
Signed by: Jmaa
SSH Key Fingerprint: SHA256:Ab0GfHGCblESJx7JRE4fj4bFy/KRpeLhi41y4pF3sNA
2 changed files with 24 additions and 2 deletions

View File

@ -17,18 +17,30 @@ from .data import MYSELF, Message
logger = logging.getLogger(__name__) 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: def sms_soup_to_message(soup: bs4.BeautifulSoup) -> Message:
# TODO: Require myself # TODO: Require myself
sent_at = datetime.datetime.fromtimestamp(int(soup['date']) / 1000) 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': if soup['type'] == '2':
sender = MYSELF sender = MYSELF
else: else:
sender = soup.get('contact_name') or soup['address'] sender = contact_name
text = soup['body'] 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) return Message(sent_at, sender, text, chat_id=chat_id)

10
test/test_synctech_sms.py Normal file
View File

@ -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'