handled KeyError for user prefs

This commit is contained in:
Mark McIntyre 2022-09-19 07:08:09 -04:00
parent 7ec849b463
commit 404126cc70

View File

@ -47,6 +47,11 @@ class WeatherApi:
'units': 'metric' 'units': 'metric'
}, },
} }
except KeyError:
user_preferences[nick] = {
'location': None,
'units': 'metric'
}
return user_preferences return user_preferences
@ -57,11 +62,18 @@ class WeatherApi:
if not units or (units and units not in ['metric', 'imperial']): if not units or (units and units not in ['metric', 'imperial']):
units = 'metric' units = 'metric'
try:
user_preferences[nick]['units'] = units user_preferences[nick]['units'] = units
if location: if location:
user_preferences[nick]['location'] = location user_preferences[nick]['location'] = location
except KeyError:
user_preferences[nick] = {
'location': location,
'units': units
}
with open(self.user_preferences_filename, 'w') as fp: with open(self.user_preferences_filename, 'w') as fp:
json.dump(user_preferences, fp) json.dump(user_preferences, fp)
@ -101,7 +113,7 @@ class WeatherApi:
location = user_preferences[nick]['location'] location = user_preferences[nick]['location']
# URL for current conditions # URL for forecast
forecast_url = f"{self.weather_api_url}/forecast.json?key={self.api_key}&q={location}&days={days}&qai=no" forecast_url = f"{self.weather_api_url}/forecast.json?key={self.api_key}&q={location}&days={days}&qai=no"
# Make the request and get the results # Make the request and get the results