Skip to content

Commit

Permalink
AndWeber's : fixed crash in case there is no weather data jasperproje…
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoBeBee committed Apr 24, 2016
1 parent 0b78dc5 commit e579cc6
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
4 changes: 4 additions & 0 deletions plugins/speechhandler/weather/locale/de-DE.po
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ msgstr "Willst du die Vorhersage für die nächsten %d Tage hören?"
msgid "Tomorrow in {city}: {text} and temperatures between {temp_low} and {temp_high} degrees."
msgstr "Morgen in {city}: {text} und Temperaturen zwischen {temp_low} und {temp_high} Grad."

#: plugins/speechhandler/weather/weather.py:204
msgid "Sorry, I had a problem retrieving the weather data."
msgstr "Entschuldigung, ich konnte keine Wetterdaten laden."

#: plugins/speechhandler/weather/weather.py:203
#, python-format
msgid "Sorry, I don't know what the weather in %s will be like tomorrow."
Expand Down
4 changes: 4 additions & 0 deletions plugins/speechhandler/weather/locale/en-US.po
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ msgstr ""
msgid "Tomorrow in {city}: {text} and temperatures between {temp_low} and {temp_high} degrees."
msgstr ""

#: plugins/speechhandler/weather/weather.py:204
msgid "Sorry, I had a problem retrieving the weather data."
msgstr ""

#: plugins/speechhandler/weather/weather.py:203
#, python-format
msgid "Sorry, I don't know what the weather in %s will be like tomorrow."
Expand Down
8 changes: 6 additions & 2 deletions plugins/speechhandler/weather/test_weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from . import weather


class TestGmailPlugin(unittest.TestCase):
class TestWeatherPlugin(unittest.TestCase):
def setUp(self):
self.plugin = testutils.get_plugin_instance(
weather.WeatherPlugin)
Expand All @@ -20,6 +20,10 @@ def test_handle_method(self):
mic = testutils.TestMic()
self.plugin.handle("What's the weather like tomorrow?", mic)
self.assertEqual(len(mic.outputs), 1)

# FIXME delete "Sorry" line, once retrieving of data is fixed
# to check that data is correct
self.assertTrue(
"can't see that far ahead" in mic.outputs[0] or
"Tomorrow" in mic.outputs[0])
"Tomorrow" in mic.outputs[0] or
"Sorry" in mic.outputs[0])
28 changes: 23 additions & 5 deletions plugins/speechhandler/weather/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,12 @@ def get_weather(location, unit="f"):
'env': 'store://datatables.org/alltableswithkeys'},
headers={'User-Agent': 'Mozilla/5.0'})
content = r.json()
channel = content['query']['results']['weather']['rss']['channel']
# make sure we got data
try:
channel = content['query']['results']['weather']['rss']['channel']
except KeyError:
# return empty Weather
return None
current_date = dateutil.parser.parse(
channel['item']['condition']['date']).date()
forecast = []
Expand Down Expand Up @@ -191,24 +196,37 @@ def handle(self, text, mic):

def _say_forecast_tomorrow(self, mic, weather):
tomorrow = None

if weather is None:
mic.say(self.gettext(
"Sorry, I had a problem retrieving the weather data."))
return

for fc in weather.forecast:
if fc.date - weather.date == datetime.timedelta(days=1):
tomorrow = fc
if tomorrow is not None:
mic.say(self.gettext(
'Tomorrow in {city}: {text} and temperatures ' +
'between {temp_low} and {temp_high} degrees.').format(
city=weather.city,
text=self.gettext(fc.text),
temp_low=fc.temp_low,
temp_high=fc.temp_high))
city=weather.city,
text=self.gettext(fc.text),
temp_low=fc.temp_low,
temp_high=fc.temp_high))
else:
mic.say(self.gettext(
"Sorry, I don't know what the weather in %s will " +
"be like tomorrow.") % weather.city)

def _say_forecast(self, mic, weather):
forecast_msgs = []

# no forecast available
if weather is None:
mic.say(self.gettext(
"Sorry, I had a problem retrieving the weather data."))
return

for fc in weather.forecast:
if fc.date - weather.date == datetime.timedelta(days=1):
date = self.gettext('Tomorrow')
Expand Down

0 comments on commit e579cc6

Please sign in to comment.