def geocode_location(location, api_key=None): """Use Google to geocode a location string. For high-volume traffic, you will need to specify an API-key. """ GEOCODE_URL = "http://maps.google.com/maps/geo" params = [('q', location), ('sensor', 'false'), ('output', 'json')] if api_key: params += [('key', api_key)] resp = utils.open_url(GEOCODE_URL, params) data = json.loads(resp.read()) if data['Status']['code'] != 200: raise exceptions.GeocodeException('Unable to geocode this location') best_match = data['Placemark'][0] address = best_match['address'] lon, lat, _ = best_match['Point']['coordinates'] location = models.Location(lat, lon, address) return location
def _daily_forecast_from_location_info(location_info, start_date=None, num_days=6, metric=False): if not start_date: start_date = datetime.date.today() # NOTE: the order of the query-string parameters seems to matter; so, # we can't use a dictionary to hold the params params = location_info + [("format", "24 hourly"), ("startDate", start_date.strftime("%Y-%m-%d")), ("numDays", str(num_days)), ("Unit", "m" if metric else "e")] FORECAST_BY_DAY_URL = ("http://www.weather.gov/forecasts/xml" "/sample_products/browser_interface" "/ndfdBrowserClientByDay.php") resp = utils.open_url(FORECAST_BY_DAY_URL, params) tree = utils.parse_xml(resp) if tree.getroot().tag == 'error': raise exceptions.NOAAException("Unable to retrieve forecast") time_layouts = _parse_time_layouts(tree) min_temp_tlk, min_temps = _parse_temperatures_for_type(tree, 'minimum') max_temp_tlk, max_temps = _parse_temperatures_for_type(tree, 'maximum') conditions_tlk, conditions = _parse_conditions(tree) # Time layout keys have to match for us to sequence and group by them assert (min_temp_tlk == max_temp_tlk == conditions_tlk) time_layout_key = min_temp_tlk time_layout = time_layouts[time_layout_key] dates = [dt.date() for dt, _ in time_layout] forecast = [] for date, min_temp_value, max_temp_value, condition in zip( dates, min_temps, max_temps, conditions): # If we're missing any data, don't create the data point if utils.any_none([min_temp_value, max_temp_value, condition]): continue temp_unit = 'C' if metric else 'F' min_temp = models.Temperature(min_temp_value, unit=temp_unit) max_temp = models.Temperature(max_temp_value, unit=temp_unit) datapoint = models.ForecastedCondition(date, min_temp, max_temp, condition) forecast.append(datapoint) return forecast
def _daily_forecast_from_location_info(location_info, start_date=None, num_days=6, metric=False): if not start_date: start_date = datetime.date.today() # NOTE: the order of the query-string parameters seems to matter; so, # we can't use a dictionary to hold the params params = location_info + [("format", "24 hourly"), ("startDate", start_date.strftime("%Y-%m-%d")), ("numDays", str(num_days)), ("Unit", "m" if metric else "e")] FORECAST_BY_DAY_URL = ("http://www.weather.gov/forecasts/xml" "/sample_products/browser_interface" "/ndfdBrowserClientByDay.php") resp = utils.open_url(FORECAST_BY_DAY_URL, params) tree = utils.parse_xml(resp) if tree.getroot().tag == 'error': raise exceptions.NOAAException("Unable to retrieve forecast") time_layouts = _parse_time_layouts(tree) min_temp_tlk, min_temps = _parse_temperatures_for_type(tree, 'minimum') max_temp_tlk, max_temps = _parse_temperatures_for_type(tree, 'maximum') conditions_tlk, conditions = _parse_conditions(tree) # Time layout keys have to match for us to sequence and group by them assert (min_temp_tlk == max_temp_tlk == conditions_tlk) time_layout_key = min_temp_tlk time_layout = time_layouts[time_layout_key] dates = [dt.date() for dt, _ in time_layout] forecast = [] for date, min_temp_value, max_temp_value, condition in zip( dates, min_temps, max_temps, conditions): # If we're missing any data, don't create the data point if utils.any_none([min_temp_value, max_temp_value, condition]): continue temp_unit = 'C' if metric else 'F' min_temp = models.Temperature(min_temp_value, unit=temp_unit) max_temp = models.Temperature(max_temp_value, unit=temp_unit) datapoint = models.ForecastedCondition( date, min_temp, max_temp, condition) forecast.append(datapoint) return forecast
def geocode_location(location, api_key=None): """Use Google to geocode a location string. For high-volume traffic, you will need to specify an API-key. """ GEOCODE_URL = "http://maps.googleapis.com/maps/api/geocode/json" params = [('address', location)] if api_key: params += [('key', api_key)] resp = utils.open_url(GEOCODE_URL, params) data = json.loads(resp.read().decode()) if data['status'] != 'OK': raise exceptions.GeocodeException('Unable to geocode this location') address = data['results'][0]['formatted_address'] lon = data['results'][0]['geometry']['location']['lng'] lat = data['results'][0]['geometry']['location']['lat'] location = models.Location(lat, lon, address) return location