def _get_weather(self):
		weather_data = self.get_default_values()
		print('-------------------------------------------------------')
		print('-------------------------------------------------------')
		print('Underground Weather Service url: %s'%URL%(self.key,self.latitude,self.longitude))
		print('-------------------------------------------------------')
		print('-------------------------------------------------------')
		parsed_json = read_json_from_url(
			URL % (self.key, self.latitude, self.longitude))
		if parsed_json is None:
			return None
		condition = gvfco('weather',parsed_json).lower()
		#
		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')
		temperature = weatherservice.s2f(gvfco('temp_f',parsed_json))
		weather_data['current_conditions']['temperature'] = weatherservice.change_temperature(temperature,self.units.temperature)
		pressure = weatherservice.s2f(gvfco('pressure_mb',parsed_json))
		weather_data['current_conditions']['pressure'] = weatherservice.change_pressure(pressure,self.units.pressure)
		humidity = weatherservice.s2f(gvfco('relative_humidity',parsed_json)[:-1])
		weather_data['current_conditions']['humidity'] = str(int(humidity))+' %'
		weather_data['current_conditions']['dew_point'] = weatherservice.get_dew_point(humidity,temperature,self.units.temperature)
		wind_velocity = weatherservice.s2f(gvfco('wind_mph',parsed_json))
		wind_direction = gvfco('wind_dir',parsed_json)
		print(wind_direction)
		weather_data['current_conditions']['wind_condition'] = weatherservice.get_wind_condition(wind_velocity,wind_direction,self.units.wind)
		weather_data['current_conditions']['wind_icon'] =weatherservice.get_wind_icon(wind_direction)
		#
		weather_data['current_conditions']['heat_index'] = weatherservice.get_heat_index(temperature,humidity)
		weather_data['current_conditions']['windchill'] = weatherservice.get_wind_chill(temperature,wind_velocity)
		#
		weather_data['current_conditions']['feels_like'] = weatherservice.get_feels_like(temperature,humidity,wind_velocity,self.units.temperature)
		#
		weather_data['current_conditions']['visibility'] = weatherservice.change_distance(gvfco('visibility_mi',parsed_json),self.units.visibility)
		weather_data['current_conditions']['solarradiation'] = gvfco('solarradiation',parsed_json)
		weather_data['current_conditions']['UV'] = gvfco('UV',parsed_json)
		weather_data['current_conditions']['precip_1hr'] = weatherservice.change_longitude(gvfco('precip_1hr_in',parsed_json),self.units.rain)
		weather_data['current_conditions']['precip_today'] = weatherservice.change_longitude(gvfco('precip_today_in',parsed_json),self.units.rain)
		for i in range(0,4):
			weather_data['forecasts'][i]['low'] = weatherservice.change_temperature(gvff('low',i,parsed_json)['fahrenheit'],self.units.temperature)
			weather_data['forecasts'][i]['high'] = weatherservice.change_temperature(gvff('high',i,parsed_json)['fahrenheit'],self.units.temperature)
			#
			weather_data['forecasts'][i]['qpf_allday'] = weatherservice.change_longitude(gvff('qpf_allday',i,parsed_json)['in'],self.units.rain)
			weather_data['forecasts'][i]['qpf_day'] = weatherservice.change_longitude(gvff('qpf_day',i,parsed_json)['in'],self.units.rain)
			weather_data['forecasts'][i]['qpf_night'] = weatherservice.change_longitude(gvff('qpf_night',i,parsed_json)['in'],self.units.rain)
			weather_data['forecasts'][i]['snow_allday'] = weatherservice.change_longitude(gvff('snow_allday',i,parsed_json)['in'],self.units.snow)
			weather_data['forecasts'][i]['snow_day'] = weatherservice.change_longitude(gvff('snow_day',i,parsed_json)['in'],self.units.snow)
			weather_data['forecasts'][i]['snow_night'] = weatherservice.change_longitude(gvff('snow_night',i,parsed_json)['in'],self.units.snow)
			wind = gvff('maxwind',i,parsed_json)
			weather_data['forecasts'][i]['maxwind'] = weatherservice.get_wind_condition(wind['mph'],wind['dir'],self.units.wind)
			wind = gvff('avewind',i,parsed_json)
			weather_data['forecasts'][i]['avewind'] = weatherservice.get_wind_condition(wind['mph'],wind['dir'],self.units.wind)
			weather_data['forecasts'][i]['avehumidity'] = '%s %%'%gvff('avehumidity',i,parsed_json)
			weather_data['forecasts'][i]['maxhumidity'] = '%s %%'%gvff('maxhumidity',i,parsed_json)
			weather_data['forecasts'][i]['minhumidity'] = '%s %%'%gvff('minhumidity',i,parsed_json)
			#
			condition = gvff('conditions',i,parsed_json).lower()
			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')
		weather_data['forecast_information']['city'] = gvfi('city',parsed_json)
		weather_data['forecast_information']['postal_code'] = gvfi('zip',parsed_json)
		weather_data['forecast_information']['latitude_e6'] = gvfi('latitude',parsed_json)
		weather_data['forecast_information']['longitude_e6'] = gvfi('longitude',parsed_json)
		return weather_data
Exemple #2
0
 def _get_weather(self):
     weather_data = self.get_default_values()
     print('-------------------------------------------------------')
     print('-------------------------------------------------------')
     print('Underground Weather Service url: %s' % URL %
           (self.key, self.latitude, self.longitude))
     print('-------------------------------------------------------')
     print('-------------------------------------------------------')
     parsed_json = read_json_from_url(
         URL % (self.key, self.latitude, self.longitude))
     if parsed_json is None:
         return weather_data
     weather_data['update_time'] = time.time()
     weather_data['ok'] = True
     condition = gvfco('weather', parsed_json).lower()
     #
     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')
     temperature = weatherservice.s2f(gvfco('temp_f', parsed_json))
     weather_data['current_conditions']['temperature'] =\
         weatherservice.change_temperature(
             temperature, self.units.temperature)
     pressure = weatherservice.s2f(gvfco('pressure_mb', parsed_json))
     weather_data['current_conditions']['pressure'] =\
         weatherservice.change_pressure(pressure, self.units.pressure)
     humidity = weatherservice.s2f(
         gvfco('relative_humidity', parsed_json)[:-1])
     weather_data['current_conditions']['humidity'] = str(
         int(humidity)) + ' %'
     weather_data['current_conditions']['dew_point'] =\
         weatherservice.get_dew_point(
             humidity, temperature, self.units.temperature)
     wind_velocity = weatherservice.s2f(gvfco('wind_mph', parsed_json))
     wind_direction = gvfco('wind_dir', parsed_json)
     weather_data['current_conditions']['wind_condition'] =\
         weatherservice.get_wind_condition(
             wind_velocity, wind_direction, self.units.wind)
     weather_data['current_conditions']['wind_icon'] =\
         weatherservice.get_wind_icon(wind_direction)
     #
     weather_data['current_conditions']['heat_index'] =\
         weatherservice.get_heat_index(temperature, humidity)
     weather_data['current_conditions']['windchill'] =\
         weatherservice.get_wind_chill(temperature, wind_velocity)
     #
     weather_data['current_conditions']['feels_like'] =\
         weatherservice.get_feels_like(
             temperature, humidity, wind_velocity, self.units.temperature)
     #
     weather_data['current_conditions']['visibility'] =\
         weatherservice.change_distance(
             gvfco('visibility_mi', parsed_json), self.units.visibility)
     weather_data['current_conditions']['solarradiation'] = gvfco(
         'solarradiation', parsed_json)
     weather_data['current_conditions']['UV'] = gvfco('UV', parsed_json)
     weather_data['current_conditions']['precip_1hr'] =\
         weatherservice.change_longitude(
             gvfco('precip_1hr_in', parsed_json), self.units.rain)
     weather_data['current_conditions']['precip_today'] =\
         weatherservice.change_longitude(
             gvfco('precip_today_in', parsed_json), self.units.rain)
     for i in range(0, 4):
         weather_data['forecasts'][i]['low'] =\
             weatherservice.change_temperature(
                 gvff('low', i, parsed_json)['fahrenheit'],
                 self.units.temperature)
         weather_data['forecasts'][i]['high'] =\
             weatherservice.change_temperature(
                 gvff('high', i, parsed_json)['fahrenheit'],
                 self.units.temperature)
         #
         weather_data['forecasts'][i]['qpf_allday'] =\
             weatherservice.change_longitude(
                 gvff('qpf_allday', i, parsed_json)['in'], self.units.rain)
         weather_data['forecasts'][i]['qpf_day'] =\
             weatherservice.change_longitude(
                 gvff('qpf_day', i, parsed_json)['in'], self.units.rain)
         weather_data['forecasts'][i]['qpf_night'] =\
             weatherservice.change_longitude(
                 gvff('qpf_night', i, parsed_json)['in'], self.units.rain)
         weather_data['forecasts'][i]['snow_allday'] =\
             weatherservice.change_longitude(
                 gvff('snow_allday', i, parsed_json)['in'], self.units.snow)
         weather_data['forecasts'][i]['snow_day'] =\
             weatherservice.change_longitude(
                 gvff('snow_day', i, parsed_json)['in'], self.units.snow)
         weather_data['forecasts'][i]['snow_night'] =\
             weatherservice.change_longitude(
                 gvff('snow_night', i, parsed_json)['in'], self.units.snow)
         wind = gvff('maxwind', i, parsed_json)
         weather_data['forecasts'][i]['maxwind'] =\
             weatherservice.get_wind_condition(
                 wind['mph'], wind['dir'], self.units.wind)
         wind = gvff('avewind', i, parsed_json)
         weather_data['forecasts'][i]['avewind'] =\
             weatherservice.get_wind_condition(
                 wind['mph'], wind['dir'], self.units.wind)
         weather_data['forecasts'][i]['avehumidity'] = '%s %%' % gvff(
             'avehumidity', i, parsed_json)
         weather_data['forecasts'][i]['maxhumidity'] = '%s %%' % gvff(
             'maxhumidity', i, parsed_json)
         weather_data['forecasts'][i]['minhumidity'] = '%s %%' % gvff(
             'minhumidity', i, parsed_json)
         #
         condition = gvff('conditions', i, parsed_json).lower()
         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')
     weather_data['forecast_information']['city'] =\
         gvfi('city', parsed_json)
     weather_data['forecast_information']['postal_code'] =\
         gvfi('zip', parsed_json)
     weather_data['forecast_information']['latitude_e6'] =\
         gvfi('latitude', parsed_json)
     weather_data['forecast_information']['longitude_e6'] =\
         gvfi('longitude', parsed_json)
     return weather_data
Exemple #3
0
    def get_weather(self):
        weather_data = self.get_default_values()
        print('-------------------------------------------------------')
        print('-------------------------------------------------------')
        print('WorldWeatherOnline Weather Service url:%s' %
              (URL % (self.latitude, self.longitude, self.key)))
        print('-------------------------------------------------------')
        print('-------------------------------------------------------')
        try:
            parsed_json = read_json_from_url(
                URL % (self.latitude, self.longitude, self.key))

            if parsed_json is None:
                return None
            if 'weather' not in parsed_json.keys(
            ) or 'main' not in parsed_json.keys(
            ) or 'wind' not in parsed_json.keys(
            ) or 'clouds' not in parsed_json.keys():
                return None
            number_condition = gvfco('weatherCode', parsed_json).lower()
            condition = get_condition(number_condition)
            #
            weather_data['current_conditions'][
                'condition_text'] = weatherservice.get_condition_wwa(
                    condition, 'text')
            if weather_data['current_conditions']['isday']:
                weather_data['current_conditions'][
                    'condition_image'] = weatherservice.get_condition_wwa(
                        condition, 'image')
                weather_data['current_conditions'][
                    'condition_icon_dark'] = weatherservice.get_condition_wwa(
                        condition, 'icon-dark')
                weather_data['current_conditions'][
                    'condition_icon_light'] = weatherservice.get_condition_wwa(
                        condition, 'icon-light')
            else:
                weather_data['current_conditions'][
                    'condition_image'] = weatherservice.get_condition_wwa(
                        condition, 'image-night')
                weather_data['current_conditions'][
                    'condition_icon_dark'] = weatherservice.get_condition_wwa(
                        condition, 'icon-night-dark')
                weather_data['current_conditions'][
                    'condition_icon_light'] = weatherservice.get_condition_wwa(
                        condition, 'icon-night-light')
            temperature = weatherservice.s2f(gvfco('temp_F', parsed_json))
            weather_data['current_conditions'][
                'temperature'] = weatherservice.change_temperature(
                    temperature, self.units.temperature)
            pressure = weatherservice.s2f(gvfco('pressure', parsed_json))
            weather_data['current_conditions'][
                'pressure'] = weatherservice.change_pressure(
                    pressure, self.units.pressure)
            humidity = weatherservice.s2f(gvfco('humidity', parsed_json))
            weather_data['current_conditions']['humidity'] = '%s %%' % (
                int(humidity))
            weather_data['current_conditions'][
                'dew_point'] = weatherservice.get_dew_point(
                    humidity, temperature, self.units.temperature)
            wind_velocity = weatherservice.s2f(
                gvfco('windspeedMiles', parsed_json))
            wind_direction = weatherservice.degToCompass2(
                gvfco('winddirDegree', parsed_json))
            weather_data['current_conditions'][
                'wind_condition'] = weatherservice.get_wind_condition2(
                    wind_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, wind_velocity)
            #
            weather_data['current_conditions'][
                'feels_like'] = weatherservice.get_feels_like(
                    temperature, humidity, wind_velocity,
                    self.units.temperature)
            #
            weather_data['current_conditions'][
                'visibility'] = weatherservice.change_distance(
                    gvfco('visibility', parsed_json), 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'] = weatherservice.change_longitude(
                    weatherservice.s2f(gvfco('precipMM', parsed_json)) / 25.4,
                    self.units.rain)
            for i in range(0, 5):
                t1 = weatherservice.s2f(gvff('tempMinF', i, parsed_json))
                t2 = weatherservice.s2f(gvff('tempMaxF', i, parsed_json))
                if t1 < t2:
                    tmin = str(t1)
                    tmax = str(t2)
                else:
                    tmin = str(t2)
                    tmax = str(t1)
                weather_data['forecasts'][i][
                    'low'] = weatherservice.change_temperature(
                        tmin, self.units.temperature)
                weather_data['forecasts'][i][
                    'high'] = weatherservice.change_temperature(
                        tmax, self.units.temperature)
                #
                weather_data['forecasts'][i][
                    'qpf_allday'] = weatherservice.change_longitude(
                        weatherservice.s2f(gvff('precipMM', i, parsed_json)) /
                        25.4, self.units.rain)
                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
                winddir = gvff('winddirDegree', i, parsed_json)
                winsped = gvff('windspeedMiles', i, parsed_json)
                wind_direction = weatherservice.degToCompass2(winddir)
                weather_data['forecasts'][i][
                    'avewind'] = weatherservice.get_wind_condition2(
                        winsped, wind_direction[0], self.units.wind)
                weather_data['forecasts'][i]['wind_icon'] = wind_direction[2]
                weather_data['forecasts'][i]['avehumidity'] = None
                weather_data['forecasts'][i]['maxhumidity'] = None
                weather_data['forecasts'][i]['minhumidity'] = None
                #
                number_condition = gvff('weatherCode', i, parsed_json).lower()
                condition = get_condition(number_condition)
                weather_data['forecasts'][i]['condition'] = condition
                weather_data['forecasts'][i][
                    'condition_text'] = weatherservice.get_condition_wwa(
                        condition, 'text')
                weather_data['forecasts'][i][
                    'condition_image'] = weatherservice.get_condition_wwa(
                        condition, 'image')
                weather_data['forecasts'][i][
                    'condition_icon'] = weatherservice.get_condition_wwa(
                        condition, 'icon-light')
            weather_data['forecast_information']['city'] = None
            weather_data['forecast_information']['postal_code'] = None
            weather_data['forecast_information']['latitude_e6'] = None
            weather_data['forecast_information']['longitude_e6'] = None
        except Exception as e:
            print(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
	def get_weather(self):
		weather_data = self.get_default_values()
		print('-------------------------------------------------------')
		print('-------------------------------------------------------')
		print('WorldWeatherOnline Weather Service url:%s'%(URL%(self.latitude,self.longitude,self.key)))
		print('-------------------------------------------------------')
		print('-------------------------------------------------------')
		try:
			parsed_json = read_json_from_url(URL%(self.latitude,self.longitude,self.key))

			if parsed_json is None:
				return None
			if  'weather' not in parsed_json.keys() or 'main' not in parsed_json.keys() or 'wind' not in parsed_json.keys() or 'clouds' not in parsed_json.keys():
				return None
			number_condition = gvfco('weatherCode',parsed_json).lower()
			condition = get_condition(number_condition)
			#
			weather_data['current_conditions']['condition_text'] = weatherservice.get_condition_wwa(condition,'text')
			if weather_data['current_conditions']['isday']:
				weather_data['current_conditions']['condition_image'] = weatherservice.get_condition_wwa(condition,'image')
				weather_data['current_conditions']['condition_icon_dark'] = weatherservice.get_condition_wwa(condition,'icon-dark')
				weather_data['current_conditions']['condition_icon_light'] = weatherservice.get_condition_wwa(condition,'icon-light')
			else:
				weather_data['current_conditions']['condition_image'] = weatherservice.get_condition_wwa(condition,'image-night')
				weather_data['current_conditions']['condition_icon_dark'] = weatherservice.get_condition_wwa(condition,'icon-night-dark')
				weather_data['current_conditions']['condition_icon_light'] = weatherservice.get_condition_wwa(condition,'icon-night-light')
			temperature = weatherservice.s2f(gvfco('temp_F',parsed_json))
			weather_data['current_conditions']['temperature'] = weatherservice.change_temperature(temperature,self.units.temperature)
			pressure = weatherservice.s2f(gvfco('pressure',parsed_json))
			weather_data['current_conditions']['pressure'] = weatherservice.change_pressure(pressure,self.units.pressure)
			humidity = weatherservice.s2f(gvfco('humidity',parsed_json))
			weather_data['current_conditions']['humidity'] = '%s %%'%(int(humidity))
			weather_data['current_conditions']['dew_point'] = weatherservice.get_dew_point(humidity,temperature,self.units.temperature)
			wind_velocity = weatherservice.s2f(gvfco('windspeedMiles',parsed_json))
			wind_direction = weatherservice.degToCompass2(gvfco('winddirDegree',parsed_json))
			weather_data['current_conditions']['wind_condition'] = weatherservice.get_wind_condition2(wind_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,wind_velocity)
			#
			weather_data['current_conditions']['feels_like'] = weatherservice.get_feels_like(temperature,humidity,wind_velocity,self.units.temperature)
			#
			weather_data['current_conditions']['visibility'] = weatherservice.change_distance(gvfco('visibility',parsed_json),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'] = weatherservice.change_longitude(weatherservice.s2f(gvfco('precipMM',parsed_json))/25.4,self.units.rain)
			for i in range(0,5):
				t1 = weatherservice.s2f(gvff('tempMinF',i,parsed_json))
				t2 = weatherservice.s2f(gvff('tempMaxF',i,parsed_json))
				if t1<t2:
					tmin=str(t1)
					tmax=str(t2)
				else:
					tmin=str(t2)
					tmax=str(t1)
				weather_data['forecasts'][i]['low'] = weatherservice.change_temperature(tmin,self.units.temperature)
				weather_data['forecasts'][i]['high'] = weatherservice.change_temperature(tmax,self.units.temperature)
				#
				weather_data['forecasts'][i]['qpf_allday'] = weatherservice.change_longitude(weatherservice.s2f(gvff('precipMM',i,parsed_json))/25.4,self.units.rain)
				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
				winddir = gvff('winddirDegree',i,parsed_json)
				winsped = gvff('windspeedMiles',i,parsed_json)
				wind_direction = weatherservice.degToCompass2(winddir)
				weather_data['forecasts'][i]['avewind'] = weatherservice.get_wind_condition2(winsped,wind_direction[0],self.units.wind)
				weather_data['forecasts'][i]['wind_icon'] = wind_direction[2]
				weather_data['forecasts'][i]['avehumidity'] = None
				weather_data['forecasts'][i]['maxhumidity'] = None
				weather_data['forecasts'][i]['minhumidity'] = None
				#
				number_condition = gvff('weatherCode',i,parsed_json).lower()
				condition = get_condition(number_condition)
				weather_data['forecasts'][i]['condition'] = condition
				weather_data['forecasts'][i]['condition_text'] = weatherservice.get_condition_wwa(condition,'text')
				weather_data['forecasts'][i]['condition_image'] = weatherservice.get_condition_wwa(condition,'image')
				weather_data['forecasts'][i]['condition_icon'] = weatherservice.get_condition_wwa(condition,'icon-light')
			weather_data['forecast_information']['city'] = None
			weather_data['forecast_information']['postal_code'] = None
			weather_data['forecast_information']['latitude_e6'] = None
			weather_data['forecast_information']['longitude_e6'] = None
		except Exception as e:
			print(e)
		return weather_data
Exemple #6
0
    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
Exemple #7
0
 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