Skip to content

Commit 9c12a82

Browse files
authored
Merge pull request CloudBotIRC#177 from linuxdaemon/gonzobot+weather-fix
Add more error handling to weather.py to handle empty forecast data
2 parents a204184 + 1ad1eb7 commit 9c12a82

1 file changed

Lines changed: 45 additions & 27 deletions

File tree

plugins/weather.py

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def get_location(nick):
112112

113113

114114
@hook.command("weather", "we", autohelp=False)
115-
def weather(text, reply, db, nick, notice):
115+
def weather(text, reply, db, nick, notice_doc):
116116
"""<location> - Gets weather data for <location>."""
117117
if not wunder_key:
118118
return "This command requires a Weather Underground API key."
@@ -123,15 +123,17 @@ def weather(text, reply, db, nick, notice):
123123
if not text:
124124
location = get_location(nick)
125125
if not location:
126-
notice(weather.__doc__)
126+
notice_doc()
127127
return
128128
else:
129129
location = text
130+
130131
# use find_location to get location data from the user input
131132
try:
132133
location_data = find_location(location)
133134
except APIError as e:
134-
return e
135+
reply(str(e))
136+
raise
135137

136138
formatted_location = "{lat},{lng}".format(**location_data)
137139

@@ -141,44 +143,60 @@ def weather(text, reply, db, nick, notice):
141143

142144
response = request.json()
143145

144-
if response['response'].get('error'):
145-
return "{}".format(response['response']['error']['description'])
146+
error = response['response'].get('error')
147+
if error:
148+
return "{}".format(error['description'])
149+
150+
forecast = response["forecast"]["simpleforecast"]["forecastday"]
151+
if not forecast:
152+
return "Unable to retrieve forecast data."
153+
154+
forecast_today = forecast[0]
155+
forecast_tomorrow = forecast[1]
146156

147-
forecast_today = response["forecast"]["simpleforecast"]["forecastday"][0]
148-
forecast_tomorrow = response["forecast"]["simpleforecast"]["forecastday"][1]
157+
forecast_today_high = forecast_today['high']
158+
forecast_today_low = forecast_today['low']
159+
forecast_tomorrow_high = forecast_tomorrow['high']
160+
forecast_tomorrow_low = forecast_tomorrow['low']
161+
162+
current_observation = response['current_observation']
149163

150164
# put all the stuff we want to use in a dictionary for easy formatting of the output
151165
weather_data = {
152-
"place": response['current_observation']['display_location']['full'],
153-
"conditions": response['current_observation']['weather'],
154-
"temp_f": response['current_observation']['temp_f'],
155-
"temp_c": response['current_observation']['temp_c'],
156-
"humidity": response['current_observation']['relative_humidity'],
157-
"wind_kph": response['current_observation']['wind_kph'],
158-
"wind_mph": response['current_observation']['wind_mph'],
159-
"wind_direction": response['current_observation']['wind_dir'],
166+
"place": current_observation['display_location']['full'],
167+
"conditions": current_observation['weather'],
168+
"temp_f": current_observation['temp_f'],
169+
"temp_c": current_observation['temp_c'],
170+
"humidity": current_observation['relative_humidity'],
171+
"wind_kph": current_observation['wind_kph'],
172+
"wind_mph": current_observation['wind_mph'],
173+
"wind_direction": current_observation['wind_dir'],
160174
"today_conditions": forecast_today['conditions'],
161-
"today_high_f": forecast_today['high']['fahrenheit'],
162-
"today_high_c": forecast_today['high']['celsius'],
163-
"today_low_f": forecast_today['low']['fahrenheit'],
164-
"today_low_c": forecast_today['low']['celsius'],
175+
"today_high_f": forecast_today_high['fahrenheit'],
176+
"today_high_c": forecast_today_high['celsius'],
177+
"today_low_f": forecast_today_low['fahrenheit'],
178+
"today_low_c": forecast_today_low['celsius'],
165179
"tomorrow_conditions": forecast_tomorrow['conditions'],
166-
"tomorrow_high_f": forecast_tomorrow['high']['fahrenheit'],
167-
"tomorrow_high_c": forecast_tomorrow['high']['celsius'],
168-
"tomorrow_low_f": forecast_tomorrow['low']['fahrenheit'],
169-
"tomorrow_low_c": forecast_tomorrow['low']['celsius']
180+
"tomorrow_high_f": forecast_tomorrow_high['fahrenheit'],
181+
"tomorrow_high_c": forecast_tomorrow_high['celsius'],
182+
"tomorrow_low_f": forecast_tomorrow_low['fahrenheit'],
183+
"tomorrow_low_c": forecast_tomorrow_low['celsius'],
170184
}
171185

172186
# Get the more accurate URL if available, if not, get the generic one.
173-
if "?query=," in response["current_observation"]['ob_url']:
174-
weather_data['url'] = web.try_shorten(response["current_observation"]['forecast_url'])
187+
ob_url = current_observation['ob_url']
188+
if "?query=," in ob_url:
189+
url = current_observation['forecast_url']
175190
else:
176-
weather_data['url'] = web.try_shorten(response["current_observation"]['ob_url'])
191+
url = ob_url
192+
193+
weather_data['url'] = web.try_shorten(url)
177194

178195
reply("{place} - \x02Current:\x02 {conditions}, {temp_f}F/{temp_c}C, {humidity}, "
179196
"Wind: {wind_mph}MPH/{wind_kph}KPH {wind_direction}, \x02Today:\x02 {today_conditions}, "
180197
"High: {today_high_f}F/{today_high_c}C, Low: {today_low_f}F/{today_low_c}C. "
181198
"\x02Tomorrow:\x02 {tomorrow_conditions}, High: {tomorrow_high_f}F/{tomorrow_high_c}C, "
182-
"Low: {tomorrow_low_f}F/{tomorrow_low_c}C - {url}".format(**weather_data))
199+
"Low: {tomorrow_low_f}F/{tomorrow_low_c}C - {url}".format_map(weather_data))
200+
183201
if text:
184202
add_location(nick, location, db)

0 commit comments

Comments
 (0)