def get_weather_variables(forecast, location): """ :param forecast: forecastio object :param location: location dict with 'lat', 'lng', and 'name' keys :return: weather_data dict containing weather information """ try: weather_data = dict() if 'darksky-unavailable' in forecast.json['flags']: raise BadForecastDataError('Darksky unavailable') if not forecast.currently().temperature: raise BadForecastDataError('Temp is None') if not forecast.currently().summary: raise BadForecastDataError('Summary is None') weather_data['units'] = utils.get_units(CONFIG['basic']['units']) # forecast.io doesn't always include 'windBearing' or 'nearestStormDistance' if hasattr(forecast.currently(), 'windBearing'): weather_data['windBearing'] = utils.get_wind_direction(forecast.currently().windBearing) else: weather_data['windBearing'] = 'unknown direction' if hasattr(forecast.currently(), 'nearestStormDistance'): weather_data['nearestStormDistance'] = forecast.currently().nearestStormDistance else: weather_data['nearestStormDistance'] = 99999 weather_data['windSpeed'] = forecast.currently().windSpeed weather_data['windSpeed_and_unit'] = str(round(forecast.currently().windSpeed)) + ' ' + \ weather_data['units']['windSpeed'] weather_data['apparentTemperature'] = forecast.currently().apparentTemperature weather_data['apparentTemperature_and_unit'] = str(round(forecast.currently().apparentTemperature)) + 'º' \ + weather_data['units']['apparentTemperature'] weather_data['temp'] = forecast.currently().temperature weather_data['temp_and_unit'] = str(round(forecast.currently().temperature)) + 'º' + \ weather_data['units']['temperature'] weather_data['humidity'] = round(forecast.currently().humidity * 100) weather_data['precipIntensity'] = forecast.currently().precipIntensity weather_data['precipProbability'] = forecast.currently().precipProbability if hasattr(forecast.currently(), 'precipType'): weather_data['precipType'] = forecast.currently().precipType else: weather_data['precipType'] = 'none' weather_data['summary'] = forecast.currently().summary.lower() weather_data['icon'] = forecast.currently().icon weather_data['location'] = location['name'] weather_data['latitude'] = location['lat'] weather_data['longitude'] = location['lng'] weather_data['timezone'] = forecast.json['timezone'] weather_data['forecast'] = forecast.daily().data[0] weather_data['hour_icon'] = forecast.minutely().icon weather_data['hour_summary'] = forecast.minutely().summary weather_data['alerts'] = forecast.alerts() weather_data['valid'] = True logging.debug('Weather data: {0}'.format(weather_data)) return weather_data except (KeyError, TypeError, BadForecastDataError) as err: logging.error('Found an error in get_weather_variables') logging.error(err) return {'valid': False}
def __init__(self, forecast, location): """ :type location: WeatherLocation :type forecast: forecastio.models.Forecast """ self.__forecast = forecast try: if 'darksky-unavailable' in forecast.json['flags']: raise BadForecastDataError('Darksky unavailable') self.units = utils.get_units(forecast.json['flags']['units']) # Dark Sky doesn't always include 'windBearing' or 'nearestStormDistance' if hasattr(forecast.currently(), 'windBearing'): self.windBearing = utils.get_wind_direction( forecast.currently().windBearing) else: self.windBearing = 'unknown direction' self.windSpeed = forecast.currently().windSpeed self.apparentTemperature = forecast.currently().apparentTemperature self.temp = forecast.currently().temperature self.humidity = round(forecast.currently().humidity * 100) self.precipIntensity = forecast.currently().precipIntensity self.precipProbability = forecast.currently().precipProbability if hasattr(forecast.currently(), 'precipType'): self.precipType = forecast.currently().precipType else: self.precipType = 'none' self.summary = forecast.currently().summary self.icon = forecast.currently().icon self.location = location self.timezone = forecast.json['timezone'] self.forecast = forecast.daily().data[0] self.minutely = forecast.minutely( ) # this will return None in many parts of the world self.alerts = list() for alert in forecast.alerts(): self.alerts.append(WeatherAlert(alert)) self.valid = True except (KeyError, TypeError, BadForecastDataError, PropertyUnavailable): self.valid = False
def __init__(self, forecast, location): """ :type location: WeatherLocation :type forecast: forecastio.models.Forecast """ self.__forecast = forecast try: if 'darksky-unavailable' in forecast.json['flags']: raise BadForecastDataError('Darksky unavailable') self.units = utils.get_units(forecast.json['flags']['units']) # Dark Sky doesn't always include 'windBearing' if hasattr(forecast.currently(), 'windBearing'): self.windBearing = utils.get_wind_direction(forecast.currently().windBearing) else: self.windBearing = 'unknown direction' self.windSpeed = forecast.currently().windSpeed self.apparentTemperature = forecast.currently().apparentTemperature self.temp = forecast.currently().temperature self.humidity = round(forecast.currently().humidity * 100) self.precipIntensity = forecast.currently().precipIntensity self.precipProbability = forecast.currently().precipProbability if hasattr(forecast.currently(), 'precipType'): self.precipType = forecast.currently().precipType else: self.precipType = 'none' self.summary = forecast.currently().summary self.icon = forecast.currently().icon self.location = location self.timezone = forecast.json['timezone'] self.forecast = forecast.daily().data[0] self.minutely = forecast.minutely() # this will return None in many parts of the world self.alerts = list() for alert in forecast.alerts(): self.alerts.append(WeatherAlert(alert)) self.valid = True except (KeyError, TypeError, BadForecastDataError, PropertyUnavailable): self.valid = False
def test_get_wind_direction(self): """Testing if wind direction conversions are successful""" self.assertEqual(utils.get_wind_direction(0), 'N') self.assertEqual(utils.get_wind_direction(338), 'N') self.assertEqual(utils.get_wind_direction(65), 'NE') self.assertEqual(utils.get_wind_direction(110), 'E') self.assertEqual(utils.get_wind_direction(150), 'SE') self.assertEqual(utils.get_wind_direction(200), 'S') self.assertEqual(utils.get_wind_direction(240), 'SW') self.assertEqual(utils.get_wind_direction(290), 'W') self.assertEqual(utils.get_wind_direction(330), 'NW') self.assertEqual(utils.get_wind_direction(400), 'N') self.assertEqual(utils.get_wind_direction(-4), 'N') self.assertEqual(utils.get_wind_direction('five'), '')