def updateforecast(self): if not self.checkweatherconfiguration(): return logging.debug('Initializing weather forecast update process. Forecasts will update every 12 hours at noon and midnight') while not exitapp[0]: updateFlag = False logging.debug('Requesting weather forecast from {0}'.format(pydPiper_config.WEATHER_SERVICE)) if pydPiper_config.WEATHER_SERVICE == 'accuweather': querystr = 'http://dataservice.accuweather.com/forecasts/v1/daily/1day/' + pydPiper_config.WEATHER_LOCATION r = requests.get(querystr, { 'apikey': pydPiper_config.WEATHER_API, }) if self.checkaccuweatherreturn(r.status_code): try: res = r.json() todaysForecast = res['DailyForecasts'][0] temp_max_f = todaysForecast['Temperature']['Maximum']['Value'] if todaysForecast['Temperature']['Maximum']['Unit'] == 'F' else round((todaysForecast['Temperature']['Maximum']['Value']*1.8)+32,1) temp_min_f = todaysForecast['Temperature']['Minimum']['Value'] if todaysForecast['Temperature']['Minimum']['Unit'] == 'F' else round((todaysForecast['Temperature']['Minimum']['Value']*1.8)+32,1) outside_temp_max = temp_max_f if pydPiper_config.TEMPERATURE.lower() == 'fahrenheit' else round((temp_max_f-32)*0.55555556,1) outside_temp_min = temp_min_f if pydPiper_config.TEMPERATURE.lower() == 'fahrenheit' else round((temp_min_f-32)*0.55555556,1) outside_temp_max_formatted = u"{0}°{1}".format(int(outside_temp_max),{'fahrenheit':'F', 'celsius': 'C'}.get(pydPiper_config.TEMPERATURE.lower())) outside_temp_min_formatted = u"{0}°{1}".format(int(outside_temp_min),{'fahrenheit':'F', 'celsius': 'C'}.get(pydPiper_config.TEMPERATURE.lower())) outside_conditions = todaysForecast['Day']['IconPhrase'] updateFlag = True except (KeyError, IndexError, ValueError): logging.warning('AccuWeather provided a response in an unexpected format. Received [{0}]'.format(res)) if updateFlag: logging.debug('Forecast calls for a high of {0}, a low of {1}. Condition is {2}'.format(outside_temp_max_formatted, outside_temp_min_formatted, outside_conditions)) with self.musicdata_lock: self.musicdata[u'outside_temp_max'] = outside_temp_max self.musicdata[u'outside_temp_min'] = outside_temp_min self.musicdata[u'outside_temp_max_formatted'] = outside_temp_max_formatted self.musicdata[u'outside_temp_min_formatted'] = outside_temp_min_formatted self.musicdata[u'outside_conditions'] = outside_conditions # Sleep until next update which occurs every half day pause.sleepUntil(time.time()+pause.nextHalfday(60), exitapp)