def add_address_details(
        self,
        street_address: Optional[Text] = None,
        city: Optional[Text] = None,
        zip: Optional[Text] = None,
    ) -> None:
        """Adds the given address information to the current site.
        If the current site already has one of the provided fields,
        then starts a fresh site before recording the information.
        """
        # Start a new site if necessary.
        address = self._current_site.address
        if address and ((street_address and address.street1) or
                        (city and address.city) or (zip and address.zip)):
            self.next_site()

        # Create an Address object.
        site = self._current_site
        site.address = site.address or schema.Address(state="AL")
        # Add the given details.
        if street_address is not None:
            site.address.street1 = street_address
        if city is not None:
            site.address.city = city
        if zip is not None:
            site.address.zip = normalize_zip(zip)
def normalize(site: dict) -> schema.NormalizedLocation:
    """Converts the parsed `site` into a normalized site location."""
    name = site.get("name")
    address = schema.Address(
        street1=site.get("street1"),
        street2=site.get("street2"),
        city=site.get("city"),
        state=site.get("state"),
        zip=normalize_zip(site.get("zip")),
    )
    source = schema.Source(
        source=_SOURCE_NAME,
        id=_generate_id(name, address),
        fetched_from_uri=normalize_url(site.get("fetched_from_uri")),
        published_at=_normalize_date_string(site.get("published_at")),
        data=site,
    )
    county = site.get("county")
    opening_times = _normalize_opening_times(site.get("opening_times"))
    normalized_site = schema.NormalizedLocation(
        name=name,
        id=_make_site_id(source),
        source=source,
        address=address,
        active=True,  # this source updates weekly
        opening_dates=opening_times[0] if opening_times else None,
        opening_hours=opening_times[1] if opening_times else None,
        notes=[county] if county else None,
    )
    return normalized_site
示例#3
0
def _get_address(site: dict) -> Optional[schema.Address]:
    zipc = normalize_zip(site["attributes"]["SiteZip"])

    return schema.Address(
        street1=site["attributes"]["SiteAddress"],
        street2=site["attributes"]["SiteAddressDetail"],
        city=site["attributes"]["SiteCity"],
        state="SC",
        zip=zipc,
    )
def _get_zip(site: dict) -> Optional[str]:
    code = site["zip"]
    if code and code.isdecimal() and len(code) < 5:
        code = code.zfill(5)
    if re.match(r"[0-9]{5}-", code):
        code = code.rstrip("-")
    code = normalize_zip(code)
    if code and schema.ZIPCODE_RE.match(code):
        return code
    if site["zip"]:
        logger.info("ignoring invalid zip code '%s'", site["zip"])
    return None
def normalize(site: dict, timestamp: str) -> schema.NormalizedLocation:
    # addresses are typically formatted like this: 800 Boylston Street, Boston, MA 02199
    # sometimes, "MA" is not included, so there's a handler below for that
    address = site["address"]

    address_parts = [p.strip() for p in address.split(",")]

    # Remove State + ZIP from end of Address
    state_zip = address_parts[-1]
    address_parts.pop()
    state_zip_parts = [p.strip() for p in state_zip.split(" ")]

    zipcode = normalize_zip(state_zip_parts[1])
    city_or_state = state_zip_parts[0]

    # Sometimes, MA is not included in the address, so the first part of this is actually the city
    if city_or_state != "MA":
        city = city_or_state
    else:
        city = address_parts[-1]
        address_parts.pop()

    street1 = address_parts[0]
    street2 = None
    if len(address_parts) > 1:
        street2 = ", ".join(address_parts[1:])

    # The locations we get typically are formatted like:
    # "Abington: Walmart (Brockton Ave.)". We don't need the city name twice
    name_without_city = site["name"].split(":")[1].strip()

    location_id = _generate_id(name_without_city + address)

    return schema.NormalizedLocation(
        id=f"{SOURCE_NAME}:{location_id}",
        name=name_without_city,
        address=schema.Address(
            street1=street1,
            street2=street2,
            city=city,
            state="MA",
            zip=zipcode,
        ),
        location=None,
        contact=[
            schema.Contact(website="https://vaxfinder.mass.gov",
                           contact_type="booking")
        ],
        languages=None,
        opening_dates=None,
        opening_hours=None,
        availability=None,
        inventory=None,
        access=None,
        parent_organization=None,
        links=None,
        notes=None,
        active=None,
        source=schema.Source(
            source=SOURCE_NAME,
            id=location_id,
            fetched_from_uri=
            "https://www.mass.gov/doc/covid-19-vaccine-locations-for-currently-eligible-recipients-csv/download",  # noqa: E501
            fetched_at=timestamp,
            published_at=None,
            data=site,
        ),
    )
def apply_address_fixups(
        address: OrderedDict[str, str]) -> OrderedDict[str, str]:

    if "PlaceName" in address and "StateName" in address:
        problem_dakotas = [
            "Valley City, North",
            "Williston North",
            "Belle Fourche, South",
        ]
        if address["PlaceName"] in problem_dakotas and address[
                "StateName"] == "Dakota":
            pl_old = address["PlaceName"]
            address["PlaceName"] = pl_old[:-5].strip()
            address["StateName"] = pl_old[-5:] + " Dakota"

    if "StateName" in address:
        state = address["StateName"]

        if state == "ND North Dakota":
            state = "North Dakota"
        elif state == "Mich.":
            state = "Michigan"
        elif state in ["SR", "US", "HEIGHTS"]:
            # raise CustomBailError()
            del address["StateName"]
            state = None
        elif state == "GL":
            state = "FL"

        if state in ["Bay Arkansas", "Palestine Arkansas"]:
            spl = state.split(" ")
            state = spl[1]
            address["PlaceName"] = (address.get("PlaceName")
                                    or "" + " " + spl[0]).strip()

        address["StateName"] = normalize_state_name(state)

        if address["StateName"] and len(address["StateName"]) == 1:
            del address["StateName"]

        if address.get("StateName") in [
                "ANCHORAGE",
                "LAGOON",
                "C2",
                "IN SPRINGFIELD",
                "BAY",
                "JUNCTION",
                "JERSEY",
                "CAROLINA",
                "FE",
                "MEXICO",
                "OAKS",
                "GUAYAMA",
                "ISABELA",
                "HATILLO",
                "BAYAMÓN",
                "CAGUAS",
                "FAJARDO",
                "PONCE",
                "MAYAGÜEZ",
                "ISLANDS",
                "LIMA",
                "CLAYTON",
        ]:

            address["PlaceName"] = (address.get("PlaceName") or "" + " " +
                                    address["StateName"].lower()).strip()

            del address["StateName"]

        if address.get("StateName") == "ALA":
            address["StateName"] = "AL"

        if address.get("StateName") == "PA15068":
            address["StateName"] = "PA"
            address["ZipCode"] = "15068"

    if "ZipCode" in address:
        normalzip = normalize_zip(address["ZipCode"])
        if normalzip:
            address["ZipCode"] = normalzip
        else:
            del address["ZipCode"]

    return address