def _curr_observations(query, result): """ Fetch latest weather observation data from weather station closest to the location associated with the query (i.e. either user location coordinates or a specific placename) """ loc = query.location # User asked about a specific location # Try to find a matching Icelandic placename if "location" in result and result.location != "Ísland": if result.location == "capital": loc = _RVK_COORDS result.subject = "Í Reykjavík" else: # First, check if it could be a location in Iceland if result.location not in ICE_PLACENAME_BLACKLIST: info = placename_lookup(result.location) if info: i = info[0] loc = (i.get("lat_wgs84"), i.get("long_wgs84")) # OK, could be a location abroad if not loc: # TODO: Finish this! return None # If it's a country name, get coordinates for capital city # and look that up # If not a country name, maybe a foreign city. Look up city # name and get coordinates # if loc within iceland: # talk to iceweather module # else # fetch data from openweathermap api # Talk to weather API try: if loc: res = observation_for_closest(loc[0], loc[1]) else: res = observation_for_station(_RVK_STATION_ID) # Default to Reykjavík result.subject = "Í Reykjavík" except Exception as e: logging.warning("Failed to fetch weather info: {0}".format(str(e))) return None # Verify that response from server is sane if ( not res or "results" not in res or not len(res["results"]) or res["results"][0].get("err") ): return None return res["results"][0]
def _curr_observations(query, result): """ Fetch latest weather observation data from weather station closest to the location associated with the query (i.e. either user location coordinates or a specific placename) """ loc = query.location # User asked about a specific location # Try to find a matching Icelandic placename if "location" in result and result.location != "Ísland": if result.location == "capital": loc = _RVK_COORDS result.subject = "Í Reykjavík" else: # Some strings should never be interpreted as Icelandic placenames if result.location in ICE_PLACENAME_BLACKLIST: return None # Unfortunately, many foreign country names are also Icelandic # placenames, so we automatically exclude country names. cc = isocode_for_country_name(result.location) if cc: return None info = placename_lookup(result.location) if info: i = info[0] loc = (i.get("lat_wgs84"), i.get("long_wgs84")) else: return None # Talk to weather API try: if loc: res = observation_for_closest(loc[0], loc[1]) else: res = observation_for_station( _RVK_STATION_ID) # Default to Reykjavík result.subject = "Í Reykjavík" except Exception as e: logging.warning("Failed to fetch weather info: {0}".format(str(e))) return None # Verify that response from server is sane if (not res or "results" not in res or not len(res["results"]) or res["results"][0].get("err")): return None return res["results"][0]
def location_info(name, kind, placename_hints=None): """ Returns dict with info about a location, given name and kind. Info includes ISO country and continent code, GPS coordinates, etc. """ # Continents are marked as "lönd" in BÍN, so we set kind manually if name in CONTINENTS: kind = "continent" elif name in ALWAYS_STREET_ADDR: kind = "street" loc = dict(name=name, kind=kind) coords = None # Heimilisfang if kind == "address": # We currently assume all addresses are Icelandic ones loc["country"] = ICELAND_ISOCODE info = icelandic_addr_info(name, placename_hints=placename_hints) if info: coords = coords_from_addr_info(info) loc["data"] = info # Land elif kind == "country": code = isocode_for_country_name(name) if code: loc["country"] = code coords = coords_for_country(code) # Heimsálfa elif kind == "continent": # Get continent ISO code loc["continent"] = CONTINENTS.get(name) # Götuheiti elif kind == "street": # All street names in BÍN are Icelandic if name not in ICE_STREETNAME_BLACKLIST: loc["country"] = ICELAND_ISOCODE coords = coords_for_street_name(name, placename_hints=placename_hints) # Örnefni elif kind == "placename": info = None # Check if it's an Icelandic placename if name in ICE_REGIONS: loc["country"] = ICELAND_ISOCODE elif name not in ICE_PLACENAME_BLACKLIST: # Try to find a matching Icelandic placename info = placename_lookup(name) if info: loc["country"] = ICELAND_ISOCODE # Pick first matching placename coords = coords_from_addr_info(info[0]) # OK, not Icelandic. Let's see if it's a foreign city if not info: cities = lookup_city_info(name) if cities: # Pick first match. Cityloc package should give us a match list # ordered by population, with capital cities given precedence c = cities[0] loc["country"] = c.get("country") coords = coords_from_addr_info(c) # Look up continent code for country if "country" in loc: loc["continent"] = continent_for_country(loc["country"]) if coords: (loc["latitude"], loc["longitude"]) = coords return loc