def answer_for_postcode(loc: Tuple): # Send API request res = query_geocode_api_coords(loc[0], loc[1]) # Verify that we have at least one valid result if (not res or "results" not in res or not len(res["results"]) or not res["results"][0]): return None # Grab top result from API call top = res["results"][0] # TODO: Fall back on lower-ranked results from the API # if the top result doesn't even contain a locality. # Extract address info from top result (street, num, locality, postcode, country_code) = _addrinfo_from_api_result(top) # Only support Icelandic postcodes for now if country_code == "IS" and postcode: pc = postcodes.get(int(postcode)) pd = "{0} {1}".format(postcode, pc["stadur_nf"]) (response, answer, voice) = gen_answer(pd) voice = "Þú ert í {0}".format(pd) return response, answer, voice else: return gen_answer("Ég veit ekki í hvaða póstnúmeri þú ert.")
def answer_for_location(loc: Tuple): # Send API request res = query_geocode_api_coords(loc[0], loc[1]) # Verify that we have at least one valid result if ( not res or "results" not in res or not len(res["results"]) or not res["results"][0] ): return None # Grab top result from API call top = res["results"][0] # TODO: Fall back on lower-ranked results from the API # if the top result doesn't even contain a locality. # Extract address info from top result street, num, locality, postcode, country_code = _addrinfo_from_api_result(top) descr = "" # Special handling of Icelandic locations since we have more info # about them and street/locality names need to be declined. if country_code == "IS": # We received a street name from the API if street: descr = street_desc(street, num, locality) # We at least have a locality (e.g. "Reykjavík") elif locality: descr = _locality_desc(locality) # Only country else: descr = country_desc("IS") # The provided location is abroad. else: sdesc = ("á " + street) if street else "" if num and street: sdesc += " " + num locdesc = ( "{0} {1}".format(iceprep_for_placename(locality), locality) if locality else "" ) # "[á Boulevard St. Germain] [í París] [í Frakklandi]" descr = "{0} {1} {2}".format(sdesc, locdesc, country_desc(country_code)).strip() if not descr: # Fall back on the formatted address string provided by Google descr = "á " + top.get("formatted_address") answer = cap_first(descr) response = dict(answer=answer) voice = "Þú ert {0}".format(_addr4voice(descr)) return response, answer, voice