- Read the address from the Member's information block on the member details page and save street, city, state and zip to the patient - The portal prints no comma between street and city, so split on street words and unit markers; when no boundary is found the whole text stays in the street field with city blank - Poll up to 10s for the details to load; ignore the office address in the page header Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
77 lines
3.0 KiB
Python
77 lines
3.0 KiB
Python
import re
|
|
import time
|
|
|
|
from selenium.webdriver.common.by import By
|
|
|
|
# DentaQuest prints the address without commas between street and city, e.g.
|
|
# Address: 311 CAPTAIN EAMES CIRCLE ASHLAND MA 01721
|
|
_ADDRESS_RE = re.compile(r"Address:\s*([^:]{5,150}?)[,\s]+([A-Z]{2})\s+(\d{5})(?:-?\d{4})?\b")
|
|
|
|
# Words that end a street. Leaves out words common at the end of city names in
|
|
# MA (HILL, PARK, BAY, FALLS, CENTER...) so "HYDE PARK" stays in the city.
|
|
_STREET_SUFFIXES = {
|
|
"ST", "STREET", "AVE", "AVENUE", "RD", "ROAD", "DR", "DRIVE", "LN", "LANE",
|
|
"CT", "COURT", "CIR", "CIRCLE", "BLVD", "BOULEVARD", "WAY", "PL", "PLACE",
|
|
"TER", "TERRACE", "PKWY", "PARKWAY", "HWY", "HIGHWAY", "TPKE", "TURNPIKE",
|
|
"SQ", "SQUARE", "ALY", "ALLEY", "EXT",
|
|
}
|
|
_UNIT_WORDS = {"APT", "UNIT", "STE", "SUITE", "RM", "ROOM", "FL", "FLOOR", "BLDG"}
|
|
|
|
|
|
def _split_street_city(text):
|
|
"""Split "311 CAPTAIN EAMES CIRCLE ASHLAND" into (street, city). The city
|
|
is empty when the boundary can't be told, so the whole text stays in street."""
|
|
parts = [p.strip() for p in re.split(r"\s*(?:,|\n)\s*", text) if p.strip()]
|
|
if len(parts) > 1:
|
|
return " ".join(parts[:-1]), parts[-1]
|
|
|
|
# Search from the end: "5 MAIN ST APT 2 ASHLAND" splits after the unit
|
|
words = text.split()
|
|
for i in range(len(words) - 1, 0, -1):
|
|
word = words[i].upper()
|
|
if word in _STREET_SUFFIXES:
|
|
end = i + 1
|
|
elif word in _UNIT_WORDS:
|
|
end = i + 2 # the unit number follows
|
|
elif word.startswith("#"):
|
|
end = i + 1
|
|
else:
|
|
continue
|
|
if end < len(words):
|
|
return " ".join(words[:end]), " ".join(words[end:])
|
|
break
|
|
return " ".join(words), ""
|
|
|
|
|
|
def parse_member_address(text):
|
|
"""Address fields from the page text: address, city, state, zipCode.
|
|
Empty dict when the page shows no member address."""
|
|
# Skip anything above the member block; the label may be on its own line
|
|
section = re.search(r"Member.s information", text, re.IGNORECASE)
|
|
m = _ADDRESS_RE.search(text[section.start():] if section else text)
|
|
if not m:
|
|
return {}
|
|
street, city = _split_street_city(m.group(1).strip())
|
|
result = {"address": street, "state": m.group(2), "zipCode": m.group(3)}
|
|
if city:
|
|
result["city"] = city
|
|
return result
|
|
|
|
|
|
def extract_member_address(driver, timeout=10):
|
|
"""Read the member's address from the DentaQuest member details page,
|
|
polling because the details fill in after the page reports complete."""
|
|
result = {}
|
|
try:
|
|
deadline = time.time() + timeout
|
|
while True:
|
|
text = driver.find_element(By.TAG_NAME, "body").get_attribute("innerText") or ""
|
|
result = parse_member_address(text)
|
|
if result or time.time() >= deadline:
|
|
break
|
|
time.sleep(1)
|
|
print(f"[extract_member_address] {result}")
|
|
except Exception as e:
|
|
print(f"[extract_member_address] failed: {e}")
|
|
return result
|