import re


def url_to_filename(url: str) -> str:
    """Converts url to a filename by removing invalid filename characters

    Note that this will not always work since windows has very particular file
    naming rules.
    """

    # Replace "*", used as geography wildcard, with "all"
    url = url.replace("*", "all")

    # Remove the beginning of the call since it is always the same
    url = url.replace("https://api.census.gov/data/", "")

    # Remove the api key if present
    key_index = url.find("key")
    if key_index != -1:
        url = url[:key_index]

    # Only keep letters, numbers, or _, ., -
    return "".join(re.findall("[a-zA-Z0-9_.-]*", url))