def __init__(self, longitude=-0.418, latitude=39.360, units=weatherservice.Units()): WeatherService.__init__(self, longitude, latitude, units) self.oauth = OAuth1(API_KEY, SHARED_SECRET) self.woeid = geocodeapi.get_woeid(latitude, longitude)
def get_weather(self): weather_data = self.get_default_values() if self.woeid is None: self.woeid = geocodeapi.get_woeid(self.latitude, self.longitude) if self.woeid is None: print('wyahooapi.py: Yahoo Weather Service, not found woeid') return weather_data try: ans = self.run_query() if ans is None: print('wyahooapi.py: Yahoo Weather Service, query answer is None') return weather_data if 'query' not in list(ans.keys()): print('wyahooapi.py: Yahoo Weather Service, query answer has no element query') return weather_data if 'results' not in list(ans['query'].keys()): print('wyahooapi.py: Yahoo Weather Service, query answer has no element query.results') return weather_data if ans['query']['results'] is None: print('wyahooapi.py: Yahoo Weather Service, query answer query.results is None') return weather_data if 'channel' not in list(ans['query']['results'].keys()): print('wyahooapi.py: Yahoo Weather Service, query answer has no element query.results.channel') return weather_data weather_data['update_time'] = time.time() weather_data['ok'] = True data = ans['query']['results']['channel'] temperature = cf.f2c_print(data['item']['condition']['temp']) velocity = cf.f2c_print(data['wind']['speed']) direction = cf.f2c_print(data['wind']['direction']) pressure = cf.f2c_print(data['atmosphere']['pressure']) visibility = cf.f2c_print(data['atmosphere']['visibility']) humidity = cf.f2c_print(data['atmosphere']['humidity']) condition = CODE[int(data['item']['condition']['code'])] weather_data['current_conditions']['condition'] = condition weather_data['current_conditions']['condition_text'] =\ weatherservice.get_condition(condition, 'text') if weather_data['current_conditions']['isday']: weather_data['current_conditions']['condition_image'] =\ weatherservice.get_condition(condition, 'image') weather_data['current_conditions']['condition_icon_dark'] =\ weatherservice.get_condition(condition, 'icon-dark') weather_data['current_conditions']['condition_icon_light'] =\ weatherservice.get_condition(condition, 'icon-light') else: weather_data['current_conditions']['condition_image'] =\ weatherservice.get_condition(condition, 'image-night') weather_data['current_conditions']['condition_icon_dark'] =\ weatherservice.get_condition(condition, 'icon-night-dark') weather_data['current_conditions']['condition_icon_light'] =\ weatherservice.get_condition(condition, 'icon-night-light') weather_data['current_conditions']['temperature'] =\ cf.change_temperature(temperature, self.units.temperature) weather_data['current_conditions']['pressure'] =\ weatherservice.change_pressure(pressure, self.units.pressure) weather_data['current_conditions']['humidity'] = '%s %%' %\ (humidity) weather_data['current_conditions']['dew_point'] =\ weatherservice.get_dew_point(humidity, temperature, self.units.temperature) wind_direction = weatherservice.degToCompass2(direction) weather_data['current_conditions']['wind_condition'] =\ weatherservice.get_wind_condition2(velocity, wind_direction[0], self.units.wind) weather_data['current_conditions']['wind_icon'] = wind_direction[2] # weather_data['current_conditions']['heat_index'] =\ weatherservice.get_heat_index(temperature, humidity) weather_data['current_conditions']['windchill'] =\ weatherservice.get_wind_chill(temperature, velocity) # weather_data['current_conditions']['feels_like'] =\ weatherservice.get_feels_like(temperature, humidity, velocity, self.units.temperature) # weather_data['current_conditions']['visibility'] =\ weatherservice.change_distance(visibility, self.units.visibility) weather_data['current_conditions']['solarradiation'] = None weather_data['current_conditions']['UV'] = None weather_data['current_conditions']['precip_1hr'] = None weather_data['current_conditions']['precip_today'] = None for i, forecast_condition in enumerate(data['item']['forecast']): if i < 7: tlow = cf.f2c_print(forecast_condition['low']) thight = cf.f2c_print(forecast_condition['high']) weather_data['forecasts'][i]['low'] =\ cf.change_temperature(tlow, self.units.temperature) weather_data['forecasts'][i]['high'] =\ cf.change_temperature(thight, self.units.temperature) # weather_data['forecasts'][i]['qpf_allday'] = None weather_data['forecasts'][i]['qpf_day'] = None weather_data['forecasts'][i]['qpf_night'] = None weather_data['forecasts'][i]['snow_allday'] = None weather_data['forecasts'][i]['snow_day'] = None weather_data['forecasts'][i]['snow_night'] = None weather_data['forecasts'][i]['maxwind'] = None weather_data['forecasts'][i]['avewind'] = None weather_data['forecasts'][i]['avehumidity'] = None weather_data['forecasts'][i]['maxhumidity'] = None weather_data['forecasts'][i]['minhumidity'] = None # condition = CODE[int(forecast_condition['code'])] weather_data['forecasts'][i]['condition'] = condition weather_data['forecasts'][i]['condition_text'] =\ weatherservice.get_condition(condition, 'text') weather_data['forecasts'][i]['condition_image'] =\ weatherservice.get_condition(condition, 'image') weather_data['forecasts'][i]['condition_icon'] =\ weatherservice.get_condition(condition, 'icon-light') except Exception as e: print('wyahooapi.py: error:', str(e)) return weather_data
def get_weather(self, tries=3): weather_data = self.get_default_values() if self.woeid is None: self.woeid = geocodeapi.get_woeid(self.latitude, self.longitude) if self.woeid is None: print('Yahoo Weather Service, not found woeid') return weather_data try: ans = self.run_query() if ans is None or\ 'query' not in ans.keys() or\ 'results' not in ans['query'].keys() or\ 'channel' not in ans['query']['results'].keys(): if tries > 0: tries = tries - 1 print('************ === ************') print('Try: %s' % (tries)) print('************ === ************') weather_data = self.get_weather(tries) return weather_data data = ans['query']['results']['channel'] temperature = s2f(data['item']['condition']['temp']) velocity = s2f(data['wind']['speed']) direction = s2f(data['wind']['direction']) pressure = s2f(data['atmosphere']['pressure']) visibility = s2f(data['atmosphere']['visibility']) humidity = s2f(data['atmosphere']['humidity']) condition = CODE[int(data['item']['condition']['code'])] weather_data['current_conditions']['condition'] = condition weather_data['current_conditions']['condition_text'] =\ weatherservice.get_condition(condition, 'text') if weather_data['current_conditions']['isday']: weather_data['current_conditions']['condition_image'] =\ weatherservice.get_condition(condition, 'image') weather_data['current_conditions']['condition_icon_dark'] =\ weatherservice.get_condition(condition, 'icon-dark') weather_data['current_conditions']['condition_icon_light'] =\ weatherservice.get_condition(condition, 'icon-light') else: weather_data['current_conditions']['condition_image'] =\ weatherservice.get_condition(condition, 'image-night') weather_data['current_conditions']['condition_icon_dark'] =\ weatherservice.get_condition(condition, 'icon-night-dark') weather_data['current_conditions']['condition_icon_light'] =\ weatherservice.get_condition(condition, 'icon-night-light') weather_data['current_conditions']['temperature'] =\ weatherservice.change_temperature(temperature, self.units.temperature) weather_data['current_conditions']['pressure'] =\ weatherservice.change_pressure(pressure, self.units.pressure) weather_data['current_conditions']['humidity'] = '%s %%' %\ (humidity) weather_data['current_conditions']['dew_point'] =\ weatherservice.get_dew_point(humidity, temperature, self.units.temperature) wind_direction = weatherservice.degToCompass2(direction) weather_data['current_conditions']['wind_condition'] =\ weatherservice.get_wind_condition2(velocity, wind_direction[0], self.units.wind) weather_data['current_conditions']['wind_icon'] = wind_direction[2] # weather_data['current_conditions']['heat_index'] =\ weatherservice.get_heat_index(temperature, humidity) weather_data['current_conditions']['windchill'] =\ weatherservice.get_wind_chill(temperature, velocity) # weather_data['current_conditions']['feels_like'] =\ weatherservice.get_feels_like(temperature, humidity, velocity, self.units.temperature) # weather_data['current_conditions']['visibility'] =\ weatherservice.change_distance(visibility, self.units.visibility) weather_data['current_conditions']['solarradiation'] = None weather_data['current_conditions']['UV'] = None weather_data['current_conditions']['precip_1hr'] = None weather_data['current_conditions']['precip_today'] = None for i, forecast_condition in enumerate(data['item']['forecast']): if i < 7: tlow = s2f(forecast_condition['low']) thight = s2f(forecast_condition['high']) weather_data['forecasts'][i]['low'] =\ change_temperature(tlow, self.units.temperature) weather_data['forecasts'][i]['high'] =\ change_temperature(thight, self.units.temperature) # weather_data['forecasts'][i]['qpf_allday'] = None weather_data['forecasts'][i]['qpf_day'] = None weather_data['forecasts'][i]['qpf_night'] = None weather_data['forecasts'][i]['snow_allday'] = None weather_data['forecasts'][i]['snow_day'] = None weather_data['forecasts'][i]['snow_night'] = None weather_data['forecasts'][i]['maxwind'] = None weather_data['forecasts'][i]['avewind'] = None weather_data['forecasts'][i]['avehumidity'] = None weather_data['forecasts'][i]['maxhumidity'] = None weather_data['forecasts'][i]['minhumidity'] = None # condition = CODE[int(forecast_condition['code'])] weather_data['forecasts'][i]['condition'] = condition weather_data['forecasts'][i]['condition_text'] =\ weatherservice.get_condition(condition, 'text') weather_data['forecasts'][i]['condition_image'] =\ weatherservice.get_condition(condition, 'image') weather_data['forecasts'][i]['condition_icon'] =\ weatherservice.get_condition(condition, 'icon-light') except Exception as e: print('************ === ************') print('Try: %s' % (tries)) print(e) print('************ === ************') if tries > 0: tries = tries - 1 weather_data = self.get_weather(tries) print(e) return weather_data