コード例 #1
0
def handle_get(http_server, database_manager, ip, account, api_key):
    query_params = http_server.get_query_parameters()
    if "code" not in query_params and "name" not in query_params:
        raise ApiParamNoCodeOrNameProvidedError

    if "code" in query_params:
        code = query_params["code"]
        state = UsState.fetch_by_code(database_manager, code)
        if state is None:
            raise ApiParamInvalidCodeError

    if "name" in query_params:
        name = query_params["name"]
        state = UsState.fetch_by_code(database_manager, name)
        if state is None:
            raise ApiParamInvalidNameError

    if state is not None:
        output = {"name": state.name, "code": state.code}
        http_server.print_headers()
        http_server.print_json(output)
コード例 #2
0
ファイル: city.py プロジェクト: satoriforos/data-api-website
def handle_get(http_server, database_manager, ip, account, api_key):
    query_params = http_server.get_query_parameters()
    if "city" not in query_params:
        raise ApiParamNoCityProvidedError
    if "state" not in query_params:
        raise ApiParamNoStateCodeProvidedError

    raw_city_string = query_params["city"]
    raw_state_code_string = query_params["state"]
    city_string = raw_city_string.title()
    state_code_string = raw_state_code_string.upper()

    match = re.match(r'^[A-Z]{2}$', state_code_string)
    if match is None:
        raise ApiParamInvalidStateCodeError

    zip_conditions = [
        {
            'column': 'city',
            'equivalence': '=',
            'value': city_string.upper()
        },
        {
            'column': 'state_code',
            'equivalence': '=',
            'value': state_code_string
        }
    ]
    zip_codes = database_manager.fetch_by(
        ZipCode(database_manager),
        zip_conditions
    )
    if len(zip_codes) == 0:
        raise ApiParamInvalidStateCodeError

    state = UsState.fetch_by_code(database_manager, state_code_string)

    zip_code_counties_conditions = [{
        'column': 'zip',
        'equivalence': ' IN ',
        'value': [z.zip_code for z in zip_codes]
    }]
    county_percentages = database_manager.fetch_by(
        county2zipCode(database_manager),
        zip_code_counties_conditions
    )
    county_fips = []
    if len(county_percentages) > 0:
        county_fips = [c.get_state_county_fips() for c in county_percentages]
        county_fips = list(set(county_fips))

    subscription_plan = SubscriptionPlan.fetch_by_id(
        database_manager,
        account.subscription_plan_id
    )
    for header in eligible_headers:
        eligible_headers[header] = get_is_eligible(
            subscription_plan,
            header
        )

    zip_totals = {}
    if len(county_fips) > 0:
        voting_county_conditions = [{
            "column": "county_code",
            "equivalence": "IN",
            "value": county_fips
        }]
        county_totals = database_manager.fetch_by(
            VotesCounty(database_manager),
            voting_county_conditions
        )
    else:
        raise ApiParamInvalidStateCodeError

    for header, is_eligible in eligible_headers.items():
        if is_eligible is True:
            zip_totals[header] = 0
            for county_total in county_totals:
                for county_percentage in county_percentages:
                    if county_percentage.get_state_county_fips() == \
                            county_total.county_code:
                        total = getattr(
                                county_total,
                                header
                            )
                        if total is None:
                            total = 0
                        zip_totals[header] += (
                            round(
                                total * county_percentage.zip_population_percent
                            )
                        )
                        break

    years = [str(x) for x in range(1980, 2010)]
    raw_output_data = OrderedDict({})
    totals = {}
    for year in years:
        output_year = {}
        for header, value in zip_totals.items():
            if year in header:
                output_year[header[:-len(year)-1]] = value
            if len(output_year) > 0:
                raw_output_data[year] = output_year
        if year not in totals:
            if year in raw_output_data:
                totals[year] = raw_output_data[year]["total"]

    # get percentages
    output_data = OrderedDict({})
    notes = []
    for year in raw_output_data:
        output_data[year] = {"breakdown":{}}
        for key, value in raw_output_data[year].items():
            if key != "total":
                output_data[year]["breakdown"][key] = 100 * value / totals[year]
        if get_is_eligible(subscription_plan, "totals") is True:
            output_data[year]['total_votes'] = totals[year]
        else:
            notes.append("Upgrade to a paid plan for totals")

    if len(output_data) == 1:
        notes.append("Upgrade plan for historical data")

    notes = list(set(notes))

    state_name = ""
    state_code = ""
    if state.code is not None:
        state_code = state.code
    if state.name is not None:
        state_name = state.name
    output = OrderedDict({
        "voting_ranges": output_data,
        "units": "percent",
        "location": {
            "city": {
                "name": city_string,
            },
            "state": {
                "name": state_name,
                "code": state_code.upper(),
                "api_endpoint": '/v1/race/state?code={}'.format(state_code.upper()),
            },
            "country": {
                "name": "United States of America",
                "code": "US",
                "api_endpoint": '/v1/race/country?code=US',
            }
        },
        "notes": notes
    })
    http_server.print_headers()
    http_server.print_json(output)
コード例 #3
0
def get_database_connection(mysql_settings):
    database_manager = DatabaseManager(host=mysql_settings["server"],
                                       port=mysql_settings["port"],
                                       user=mysql_settings["username"],
                                       password=mysql_settings["password"],
                                       db=mysql_settings["schema"],
                                       charset=mysql_settings["charset"])
    return database_manager


database_manager = get_database_connection(settings["mysql"])

us_counties = database_manager.fetch_all(UsCounty(database_manager))
cities = database_manager.fetch_all(City(database_manager))
us_states = database_manager.fetch_all(UsState(database_manager))
countries = database_manager.fetch_all(Country(database_manager))

country_id = None
for country in countries:
    if country.code == "US":
        country_id = country.id
        break

sex_file_path = Path(
    "~/Downloads/County Demographic Datasets/sex-condensed.xlsx")
xls = pd.ExcelFile(sex_file_path.expanduser().as_posix())

header_translations = {
    "Areaname": "city_state",
    "STCOU": "county_code",
コード例 #4
0
ファイル: zip.py プロジェクト: satoriforos/data-api-website
def handle_get(http_server, database_manager, ip, account, api_key):
    query_params = http_server.get_query_parameters()
    if "zip" not in query_params:
        raise ApiParamNoZipCodeProvidedError

    raw_zip_code_string = query_params["zip"]
    zip_code_string = raw_zip_code_string
    if "-" in raw_zip_code_string:
        dash_position = raw_zip_code_string.find("-")
        zip_code_string = raw_zip_code_string[0:dash_position]

    match = re.match(r'^\d+$', zip_code_string)
    if match is None:
        raise ApiParamInvalidZipCodeError

    zip_code_int = int(zip_code_string)
    zip_code = ZipCode.fetch_by_zip_int(database_manager,
                                        zip_code_int,
                                        is_decommisioned=False)

    if zip_code is None:
        raise ApiParamInvalidZipCodeError

    state = UsState.fetch_by_id(database_manager, zip_code.state_id)
    city_name = zip_code.city
    if zip_code.city is not None:
        city_name = zip_code.city.title()

    county_percentages = zip_code.get_county_percentages()
    county_fips = []
    if len(county_percentages) > 0:
        county_fips = [c.get_state_county_fips() for c in county_percentages]
        county_fips = list(set(county_fips))

    subscription_plan = SubscriptionPlan.fetch_by_id(
        database_manager, account.subscription_plan_id)
    for header in eligible_headers:
        eligible_headers[header] = get_is_eligible(subscription_plan, header)

    zip_totals = {}
    if len(county_fips) > 0:
        population_county_conditions = [{
            "column": "county_code",
            "equivalence": "IN",
            "value": county_fips
        }]
        county_totals = database_manager.fetch_by(
            PopulationCounty(database_manager), population_county_conditions)
    else:
        raise ApiParamInvalidZipCodeError

    for header, is_eligible in eligible_headers.items():
        if is_eligible is True:
            zip_totals[header] = 0
            for county_total in county_totals:
                for county_percentage in county_percentages:
                    if county_percentage.get_state_county_fips() == \
                            county_total.county_code:
                        total = getattr(county_total, header)
                        if total is None:
                            total = 0
                        zip_totals[header] += (round(
                            total * county_percentage.zip_population_percent))
                        break

    years = [str(x) for x in range(1930, 2011)]
    raw_output_data = OrderedDict({})
    totals = {}
    for year in years:
        output_year = {}
        for header, value in zip_totals.items():
            if year in header:
                output_year[header[:-len(year) - 1]] = value
            if len(output_year) > 0:
                raw_output_data[year] = output_year

    # get percentages
    output_data = OrderedDict({})
    notes = []
    for year in raw_output_data:
        output_data[year] = None
        for key, value in raw_output_data[year].items():
            output_data[year] = value

    if len(output_data) == 1:
        notes.append("Upgrade plan for historical data")

    notes = list(set(notes))

    state_name = ""
    state_code = ""
    if state.code is not None:
        state_code = state.code
    if state.name is not None:
        state_name = state.name
    output = OrderedDict({
        "population_estimates": output_data,
        "units": "people",
        "location": {
            "city": {
                "name":
                city_name,
                "api_endpoint":
                '/v1/population/city?city={}&state={}'.format(
                    requote_uri(city_name), state_code.upper()),
            },
            "state": {
                "name":
                state_name,
                "code":
                state_code.upper(),
                "api_endpoint":
                '/v1/population/state?code={}'.format(state_code.upper()),
            },
            "country": {
                "name": "United States of America",
                "code": "US",
                "api_endpoint": '/v1/population/country?code=US',
            }
        },
        "notes": notes
    })
    http_server.print_headers()
    http_server.print_json(output)
コード例 #5
0
def handle_get(http_server, database_manager, ip, account, api_key):
    query_params = http_server.get_query_parameters()
    if "code" not in query_params:
        raise ApiParamNoStateCodeProvidedError

    raw_state_code_string = query_params["code"]
    state_code_string = raw_state_code_string.upper()

    match = re.match(r'^[A-Za-z]{2}$', state_code_string)
    if match is None:
        raise ApiParamInvalidStateCodeError

    state = UsState.fetch_by_code(database_manager, state_code_string)

    if state is None:
        raise ApiParamInvalidStateCodeError

    county_total = AncestryCounty.fetch_by_state_name(database_manager,
                                                      state.name.upper())
    subscription_plan = SubscriptionPlan.fetch_by_id(
        database_manager, account.subscription_plan_id)
    for header in eligible_headers:
        eligible_headers[header] = get_is_eligible(subscription_plan, header)

    zip_totals = {}
    for header, is_eligible in eligible_headers.items():
        if is_eligible is True:
            zip_totals[header] = 0
            total = getattr(county_total, header)
            if total is None:
                total = 0
            zip_totals[header] = (round(total))

    years = [str(x) for x in range(1980, 2010)]
    raw_output_data = OrderedDict({})
    totals = {}
    for year in years:
        output_year = {}
        for header, value in zip_totals.items():
            if year in header:
                output_year[header[:-len(year) - 1]] = value
            if len(output_year) > 0:
                raw_output_data[year] = output_year
                if year not in totals:
                    totals[year] = 0
                else:
                    totals[year] += value

    # get percentages
    output_data = OrderedDict({})
    notes = []
    for year in raw_output_data:
        output_data[year] = {"breakdown": {}}
        for key, value in raw_output_data[year].items():
            output_data[year]["breakdown"][key] = value / totals[year]
        if get_is_eligible(subscription_plan, "totals") is True:
            output_data[year]['total'] = totals[year]
        else:
            notes.append("Upgrade to a paid plan for totals")

    if len(output_data) == 1:
        notes.append("Upgrade plan for historical data")

    notes = list(set(notes))

    output = {
        "ancestry_ranges": output_data,
        "units": "percent",
        "location": {
            "state": {
                "name": state.name,
                "code": state.code.upper(),
            },
            "country": {
                "name": "United States of America",
                "code": "US",
                "api_endpoint": '/v1/ancestry/country?code=US',
            }
        },
        "notes": notes
    }
    http_server.print_headers()
    http_server.print_json(output)
コード例 #6
0
ファイル: state.py プロジェクト: satoriforos/data-api-website
def handle_get(http_server, database_manager, ip, account, api_key):
    query_params = http_server.get_query_parameters()
    if "code" not in query_params:
        raise ApiParamNoStateCodeProvidedError

    raw_state_code_string = query_params["code"]
    state_code_string = raw_state_code_string.upper()

    match = re.match(r'^[A-Za-z]{2}$', state_code_string)
    if match is None:
        raise ApiParamInvalidStateCodeError

    state = UsState.fetch_by_code(database_manager, state_code_string)

    if state is None:
        raise ApiParamInvalidStateCodeError

    county_total = EducationCounty.fetch_by_state_name(database_manager,
                                                       state.name.upper())
    subscription_plan = SubscriptionPlan.fetch_by_id(
        database_manager, account.subscription_plan_id)
    for header in eligible_headers:
        eligible_headers[header] = get_is_eligible(subscription_plan, header)

    zip_totals = {}
    for header, is_eligible in eligible_headers.items():
        if is_eligible is True:
            zip_totals[header] = 0
            total = getattr(county_total, header)
            if total is None:
                total = 0
            zip_totals[header] = (round(total))

    years = ["1980", "1990", "2000", "2009"]
    raw_output_data = OrderedDict({})
    totals = {}
    for year in years:
        output_year = {}
        for header, value in zip_totals.items():
            if "num_adults" not in header:
                if year in header:
                    ending = year
                    end = len(ending) + 1
                    if "_2005_" in header:
                        ending = "2005_{}".format(year)
                        end = len(ending) + 1
                    output_year[header[:-end]] = 100 * value / zip_totals[
                        "num_adults_{}".format(ending)]
            if len(output_year) > 0:
                raw_output_data[year] = output_year
        if year not in totals:
            if year in raw_output_data:
                ending = year
                if year == "2009":
                    ending = "2005_2009"
                totals[year] = zip_totals["num_adults_{}".format(ending)]

    # get percentages
    output_data = OrderedDict({})
    notes = []
    for year in raw_output_data:
        output_data[year] = {"breakdown": {}}
        for key, value in raw_output_data[year].items():
            output_data[year]["breakdown"][key] = value
        if get_is_eligible(subscription_plan, "totals") is True:
            output_data[year]['total_adults'] = totals[year]
        else:
            notes.append("Upgrade to a paid plan for totals")

    notes = list(set(notes))

    state_name = ""
    state_code = ""
    if state.code is not None:
        state_code = state.code
    if state.name is not None:
        state_name = state.name
    output = {
        "education_ranges": output_data,
        "location": {
            "state": {
                "name": state.name,
                "code": state.code.upper(),
            },
            "country": {
                "name": "United States of America",
                "code": "US",
                "api_endpoint": '/v1/education/country?code=US',
            }
        },
        "notes": notes
    }
    http_server.print_headers()
    http_server.print_json(output)
コード例 #7
0
ファイル: zip.py プロジェクト: satoriforos/data-api-website
def handle_get(http_server, database_manager, ip, account, api_key):
    query_params = http_server.get_query_parameters()
    if "zip" not in query_params:
        raise ApiParamNoZipCodeProvidedError

    raw_zip_code_string = query_params["zip"]
    zip_code_string = raw_zip_code_string
    if "-" in raw_zip_code_string:
        dash_position = raw_zip_code_string.find("-")
        zip_code_string = raw_zip_code_string[0:dash_position]

    match = re.match(r'^\d+$', zip_code_string)
    if match is None:
        raise ApiParamInvalidZipCodeError

    zip_code_int = int(zip_code_string)
    zip_code = ZipCode.fetch_by_zip_int(
        database_manager,
        zip_code_int,
        is_decommisioned=False
    )

    if zip_code is None:
        raise ApiParamInvalidZipCodeError

    state = UsState.fetch_by_id(database_manager, zip_code.state_id)
    city_name = zip_code.city
    if zip_code.city is not None:
        city_name = zip_code.city.title()

    county_percentages = zip_code.get_county_percentages()
    county_fips = []
    if len(county_percentages) > 0:
        county_fips = [c.get_state_county_fips() for c in county_percentages]
        county_fips = list(set(county_fips))

    subscription_plan = SubscriptionPlan.fetch_by_id(
        database_manager,
        account.subscription_plan_id
    )
    for header in eligible_headers:
        eligible_headers[header] = get_is_eligible(
            subscription_plan,
            header
        )

    zip_totals = {}
    if len(county_fips) > 0:
        education_county_conditions = [{
            "column": "county_code",
            "equivalence": "IN",
            "value": county_fips
        }]
        county_totals = database_manager.fetch_by(
            EducationCounty(database_manager),
            education_county_conditions
        )
    else:
        raise ApiParamInvalidZipCodeError

    for header, is_eligible in eligible_headers.items():
        if is_eligible is True:
            zip_totals[header] = 0
            for county_total in county_totals:
                for county_percentage in county_percentages:
                    if county_percentage.get_state_county_fips() == \
                            county_total.county_code:
                        total = getattr(
                                county_total,
                                header
                            )
                        if total is None:
                            total = 0
                        zip_totals[header] = (
                            round(
                                total * county_percentage.zip_population_percent
                            )
                        )
                        break

    years = ["1980", "1990", "2000", "2009"]
    raw_output_data = OrderedDict({})
    totals = {}
    for year in years:
        output_year = {}
        for header, value in zip_totals.items():
            if "num_adults" not in header:
                if year in header:
                    ending = year
                    end = len(ending) + 1
                    if "_2005_" in header:
                        ending = "2005_{}".format(year)
                        end = len(ending) + 1
                    output_year[header[:-end]] = 100 * value / zip_totals[
                        "num_adults_{}".format(ending)
                    ]
            if len(output_year) > 0:
                raw_output_data[year] = output_year
        if year not in totals:
            if year in raw_output_data:
                ending = year
                if year == "2009":
                    ending = "2005_2009"
                totals[year] = zip_totals["num_adults_{}".format(ending)]

    # get percentages
    output_data = OrderedDict({})
    notes = []
    for year in raw_output_data:
        output_data[year] = {"breakdown":{}}
        for key, value in raw_output_data[year].items():
            output_data[year]["breakdown"][key] = value
        if get_is_eligible(subscription_plan, "totals") is True:
            output_data[year]['total_adults'] = totals[year]
        else:
            notes.append("Upgrade to a paid plan for totals")

    if len(output_data) == 1:
        notes.append("Upgrade plan for historical data")

    notes = list(set(notes))

    state_name = ""
    state_code = ""
    if state.code is not None:
        state_code = state.code
    if state.name is not None:
        state_name = state.name
    output = OrderedDict({
        "education_ranges": output_data,
        "city": {
            "name": city_name,
            "api_endpoint": '/v1/education/city?city={}&state={}'.format(
                requote_uri(city_name),
                state_code.upper()
            ),
        },
        "state": {
            "name": state_name,
            "code": state_code.upper(),
            "api_endpoint": '/v1/education/state?code={}'.format(state_code.upper()),
        },
        "country": {
            "name": "United States of America",
            "code": "US",
            "api_endpoint": '/v1/education/country?code=US',
        }
    })
    http_server.print_headers()
    http_server.print_json(output)
コード例 #8
0
ファイル: index.py プロジェクト: satoriforos/data-api-website
def handle_get(http_server, database_manager, ip, account, api_key):
    query_params = http_server.get_query_parameters()
    if "zip" not in query_params:
        raise ApiParamNoZipCodeeProvidedError

    http_server.print_headers()
    raw_zip_code_string = query_params["zip"]
    zip_code_string = raw_zip_code_string
    if "-" in raw_zip_code_string:
        dash_position = raw_zip_code_string.find("-")
        zip_code_string = raw_zip_code_string[0:dash_position]

    match = re.match(r'^\d+$', zip_code_string)
    if match is None:
        raise ApiParamInvalidZipCodeError

    zip_code_int = int(zip_code_string)
    zip_code = ZipCode.fetch_by_zip_int(database_manager,
                                        zip_code_int,
                                        is_decommisioned=False)

    if zip_code is None:
        raise ApiParamInvalidZipCodeError

    state = UsState.fetch_by_code(database_manager, zip_code.state_code)
    is_po_box = zip_code.zip_code_type == "PO BOX"
    city_name = zip_code.city
    if zip_code.city is not None:
        city_name = zip_code.city.capitalize()

    mean_income = zip_code.get_mean_income()
    mean_national_income = zip_code.get_mean_national_income()
    mean_state_income = state.get_mean_income()
    mean_city_income = zip_code.get_mean_city_income()
    estimated_national_population = zip_code.get_estimated_national_population(
    )
    estimated_state_population = state.get_estimated_population()
    estimated_city_population = zip_code.get_estimated_city_population()

    area_km2 = zip_code.area_m2 / 1000000
    city_area_km2 = zip_code.get_city_area_km2()
    state_area_km2 = state.area_km2
    national_area_km2 = 9147420

    output = {
        "zip": zip_code_string,
        "geolocation": {
            "latitude": zip_code.latitude,
            "longitude": zip_code.longitude,
        },
        "is_po_box": is_po_box,
        "estimated_population": zip_code.estimated_population,
        "mean_income": mean_income,
        "utc_offset": zip_code.utc_offset,
        "area_km2": area_km2,
        "city": {
            "name": city_name,
            "mean_income": mean_city_income,
            "estimated_population": estimated_city_population,
            "area_km2": city_area_km2,
        },
        "state": {
            "name": state.name.capitalize(),
            "code": state.code.upper(),
            "mean_income": mean_state_income,
            "estimated_population": estimated_state_population,
            "area_km2": state_area_km2,
        },
        "country": {
            "name": "United States of America",
            "code": "US",
            "mean_income": mean_national_income,
            "estimated_population": estimated_national_population,
            "area_km2": national_area_km2,
        }
    }
    http_server.print_json(output)
コード例 #9
0
def handle_get(http_server, database_manager, ip, account, api_key):
    query_params = http_server.get_query_parameters()
    if "phone" not in query_params:
        raise ApiParamNoPhoneProvidedError

    http_server.print_headers()
    phone_string = query_params["phone"]

    matches = re.match(
        r"^\s*(?:\+?(\d{1,3}))?[-. (]*(\d{3})[-. )]*(\d{3})[-. ]*(\d{4})(?: *x(\d+))?\s*$",
        phone_string)
    if matches is None:
        raise ApiParamInvalidPhoneError

    try:
        phone = phonenumbers.parse(phone_string, "US")
        formatted_number = phonenumbers.format_number(
            phone, phonenumbers.PhoneNumberFormat.NATIONAL)
    except:
        raise ApiParamInvalidPhoneError

    area_code_string = re.findall(r"\(([^\)]{3})\)", formatted_number)
    if len(area_code_string) == 0:
        raise ApiParamInvalidPhoneError

    area_code_conditions = [{
        "column": "area_code",
        "equivalence": "=",
        "value": area_code_string[0]
    }]
    area_codes = database_manager.fetch_by(AreaCode(database_manager),
                                           area_code_conditions)
    locations = []
    if len(area_codes) > 0:
        city_ids = [area_code.city_id for area_code in area_codes]
        state_ids = [area_code.state_id for area_code in area_codes]
        country_id = area_codes[0].country_id

        city_conditions = [{
            "column": "id",
            "equivalence": "IN",
            "value": city_ids
        }]
        cities = database_manager.fetch_by(City(database_manager),
                                           city_conditions)
        city_names = set([city.name for city in cities])

        state_conditions = [{
            "column": "id",
            "equivalence": "IN",
            "value": state_ids
        }]
        states = database_manager.fetch_by(UsState(database_manager),
                                           state_conditions)
        state_names = set([state.name for state in states])

        country_conditions = [{
            "column": "id",
            "equivalence": "=",
            "value": country_id
        }]
        country_info = None
        country = Country.fetch_by_id(database_manager,
                                      area_codes[0].country_id)
        if country is not None:
            country_info = {"name": country.name, "code": country.code}

        for city in cities:
            city_state = None
            for state in states:
                if city.state_id == state.id:
                    city_state = {"name": state.name, "code": state.code}
                    break

            location = {
                "is_overlay":
                DatabaseObject.get_boolean_from_string(
                    area_codes[0].is_overlay),
                "city":
                city.name,
                "state":
                city_state,
                "country":
                country_info
            }
            locations.append(location)

    output = {"locations": locations}
    http_server.print_json(output)