Phone number normalization
This commit is contained in:
parent
4ca4382a42
commit
426b32d5cb
|
@ -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)
|
||||
|
||||
|
||||
|
|
10
test/test_synctech_sms.py
Normal file
10
test/test_synctech_sms.py
Normal 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'
|
Loading…
Reference in New Issue
Block a user