Skip to content

Commit

Permalink
add entity address parser
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-kleiner committed Jul 30, 2024
1 parent 13e667e commit 6da04d8
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions whoisit/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,31 @@ def clean(s):
return s.strip()


def clean_address(a):
if a is None:
a = ''
if isinstance(a, list):
a = ' '.join(a)
if not isinstance(a, str):
a = str(a)
return a.strip()


class VCardArrayDataDict(TypedDict, total=False):
name: str
email: str
tel: str
address: list[str]


class VCardArrayAddressDataDict(TypedDict, total=False):
po_box: str
ext_address: str
street_address: str
locality: str
region: str
postal_code: str
country: str


class Parser:
Expand Down Expand Up @@ -95,6 +116,16 @@ def parse_vcard_array(self, vcard) -> Optional[VCardArrayDataDict]:
v_card_array_data_dict["email"] = clean(entry_label)
elif entry_field == 'tel':
v_card_array_data_dict["tel"] = clean(entry_label)
elif entry_field == 'adr' and isinstance(entry_label, list) and len(entry_label) == 7:
v_card_array_data_dict['address'] = VCardArrayAddressDataDict(
po_box= clean_address(entry_label[0]),
ext_address= clean_address(entry_label[1]),
street_address= clean_address(entry_label[2]),
locality= clean_address(entry_label[3]),
region= clean_address(entry_label[4]),
postal_code= clean_address(entry_label[5]),
country= clean_address(entry_label[6])
)

return v_card_array_data_dict or None

Expand Down

0 comments on commit 6da04d8

Please sign in to comment.