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: countries = database_manager.fetch_all(Country(database_manager)) output = [] for country in countries: output.append({"name": country.name, "code": country.code}) http_server.print_headers() http_server.print_json(output) else: if "code" in query_params: code = query_params["code"] country = Country.fetch_by_code(database_manager, code) if country is None: raise ApiParamInvalidCodeError if "name" in query_params: name = query_params["name"] country = Country.fetch_by_code(database_manager, name) if country is None: raise ApiParamInvalidNameError if country is not None: output = {"name": country.name, "code": country.code} http_server.print_headers() http_server.print_json(output)
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", "SEX150200D": "males_2000",
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 ApiParamNoCountryCodeProvidedError raw_country_code_string = query_params["code"] country_code_string = raw_country_code_string.upper() if country_code_string is None: raise ApiParamInvalidCountryCodeError match = re.match(r'^[A-Za-z]{2}$', country_code_string) if match is None: raise ApiParamInvalidCountryCodeError country = Country.fetch_by_code(database_manager, country_code_string) if country is None: raise ApiParamInvalidCountryCodeError country_name = country.name if country.code == "US": country_name = "United States" county_total = GenderCounty.fetch_by_country_name(database_manager, country_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(year) for year in range(2000, 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] = sum( [v for k, v in raw_output_data[year].items()]) # 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] = 100 * 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 = OrderedDict({ "gender_breakdown": output_data, "units": "percent", "location": { "country": { "name": "United States of America", "code": "US", } }, "notes": notes }) http_server.print_headers() http_server.print_json(output)
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)