@@ -86,7 +86,12 @@ def get_weather(location, unit="f"):
8686 'env' : 'store://datatables.org/alltableswithkeys' },
8787 headers = {'User-Agent' : 'Mozilla/5.0' })
8888 content = r .json ()
89- channel = content ['query' ]['results' ]['weather' ]['rss' ]['channel' ]
89+ # make sure we got data
90+ try :
91+ channel = content ['query' ]['results' ]['weather' ]['rss' ]['channel' ]
92+ except KeyError :
93+ # return empty Weather
94+ return None
9095 current_date = dateutil .parser .parse (
9196 channel ['item' ]['condition' ]['date' ]).date ()
9297 forecast = []
@@ -191,24 +196,37 @@ def handle(self, text, mic):
191196
192197 def _say_forecast_tomorrow (self , mic , weather ):
193198 tomorrow = None
199+
200+ if weather is None :
201+ mic .say (self .gettext (
202+ "Sorry, I had a problem retrieving the weather data." ))
203+ return
204+
194205 for fc in weather .forecast :
195206 if fc .date - weather .date == datetime .timedelta (days = 1 ):
196207 tomorrow = fc
197208 if tomorrow is not None :
198209 mic .say (self .gettext (
199210 'Tomorrow in {city}: {text} and temperatures ' +
200211 'between {temp_low} and {temp_high} degrees.' ).format (
201- city = weather .city ,
202- text = self .gettext (fc .text ),
203- temp_low = fc .temp_low ,
204- temp_high = fc .temp_high ))
212+ city = weather .city ,
213+ text = self .gettext (fc .text ),
214+ temp_low = fc .temp_low ,
215+ temp_high = fc .temp_high ))
205216 else :
206217 mic .say (self .gettext (
207218 "Sorry, I don't know what the weather in %s will " +
208219 "be like tomorrow." ) % weather .city )
209220
210221 def _say_forecast (self , mic , weather ):
211222 forecast_msgs = []
223+
224+ # no forecast available
225+ if weather is None :
226+ mic .say (self .gettext (
227+ "Sorry, I had a problem retrieving the weather data." ))
228+ return
229+
212230 for fc in weather .forecast :
213231 if fc .date - weather .date == datetime .timedelta (days = 1 ):
214232 date = self .gettext ('Tomorrow' )
0 commit comments