Skip to content

Commit fda8e2d

Browse files
committed
Merge pull request #483 from andweber/fix_weatherplugin
fixed crash in case there is no weather data
2 parents 5a3dad4 + fa4afee commit fda8e2d

4 files changed

Lines changed: 37 additions & 7 deletions

File tree

plugins/speechhandler/weather/locale/de-DE.po

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ msgstr "Willst du die Vorhersage für die nächsten %d Tage hören?"
6868
msgid "Tomorrow in {city}: {text} and temperatures between {temp_low} and {temp_high} degrees."
6969
msgstr "Morgen in {city}: {text} und Temperaturen zwischen {temp_low} und {temp_high} Grad."
7070

71+
#: plugins/speechhandler/weather/weather.py:204
72+
msgid "Sorry, I had a problem retrieving the weather data."
73+
msgstr "Entschuldigung, ich konnte keine Wetterdaten laden."
74+
7175
#: plugins/speechhandler/weather/weather.py:203
7276
#, python-format
7377
msgid "Sorry, I don't know what the weather in %s will be like tomorrow."

plugins/speechhandler/weather/locale/en-US.po

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ msgstr ""
6868
msgid "Tomorrow in {city}: {text} and temperatures between {temp_low} and {temp_high} degrees."
6969
msgstr ""
7070

71+
#: plugins/speechhandler/weather/weather.py:204
72+
msgid "Sorry, I had a problem retrieving the weather data."
73+
msgstr ""
74+
7175
#: plugins/speechhandler/weather/weather.py:203
7276
#, python-format
7377
msgid "Sorry, I don't know what the weather in %s will be like tomorrow."

plugins/speechhandler/weather/test_weather.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from . import weather
55

66

7-
class TestGmailPlugin(unittest.TestCase):
7+
class TestWeatherPlugin(unittest.TestCase):
88
def setUp(self):
99
self.plugin = testutils.get_plugin_instance(
1010
weather.WeatherPlugin)
@@ -20,6 +20,10 @@ def test_handle_method(self):
2020
mic = testutils.TestMic()
2121
self.plugin.handle("What's the weather like tomorrow?", mic)
2222
self.assertEqual(len(mic.outputs), 1)
23+
24+
# FIXME delete "Sorry" line, once retrieving of data is fixed
25+
# to check that data is correct
2326
self.assertTrue(
2427
"can't see that far ahead" in mic.outputs[0] or
25-
"Tomorrow" in mic.outputs[0])
28+
"Tomorrow" in mic.outputs[0] or
29+
"Sorry" in mic.outputs[0])

plugins/speechhandler/weather/weather.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)