From 8f70143e2dca3da479a085691a422d69ba07f4c6 Mon Sep 17 00:00:00 2001 From: Andreas Weber Date: Sun, 3 Apr 2016 23:28:27 +0200 Subject: [PATCH 1/2] fixed chrash in case there is no weather data --- plugins/speechhandler/weather/locale/de-DE.po | 4 ++ plugins/speechhandler/weather/locale/en-US.po | 4 ++ plugins/speechhandler/weather/test_weather.py | 8 +++- plugins/speechhandler/weather/weather.py | 43 ++++++++++++++----- 4 files changed, 46 insertions(+), 13 deletions(-) diff --git a/plugins/speechhandler/weather/locale/de-DE.po b/plugins/speechhandler/weather/locale/de-DE.po index bedf00a68..b6f391a2a 100644 --- a/plugins/speechhandler/weather/locale/de-DE.po +++ b/plugins/speechhandler/weather/locale/de-DE.po @@ -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." diff --git a/plugins/speechhandler/weather/locale/en-US.po b/plugins/speechhandler/weather/locale/en-US.po index 3c2113cb8..4b4f424ce 100644 --- a/plugins/speechhandler/weather/locale/en-US.po +++ b/plugins/speechhandler/weather/locale/en-US.po @@ -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." diff --git a/plugins/speechhandler/weather/test_weather.py b/plugins/speechhandler/weather/test_weather.py index 9b19bca8f..2fa58beb9 100644 --- a/plugins/speechhandler/weather/test_weather.py +++ b/plugins/speechhandler/weather/test_weather.py @@ -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) @@ -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]) diff --git a/plugins/speechhandler/weather/weather.py b/plugins/speechhandler/weather/weather.py index cf787dd56..7cd1b4cc3 100644 --- a/plugins/speechhandler/weather/weather.py +++ b/plugins/speechhandler/weather/weather.py @@ -80,15 +80,23 @@ def get_weather(location, unit="f"): yql_query = YAHOO_YQL_QUERY % (location.replace('"', ''), unit.replace('"', '')) r = requests.get(YAHOO_YQL_URL, - params={ - 'q': yql_query, - 'format': 'json', - 'env': 'store://datatables.org/alltableswithkeys'}, - headers={'User-Agent': 'Mozilla/5.0'}) + params={'q': yql_query, + 'format': 'json', + '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: + # return empty Weather + return Weather(city=None, + date=None, + text=None, + temp=None, + forecast=None) current_date = dateutil.parser.parse( - channel['item']['condition']['date']).date() + channel['item']['condition']['date']).date() forecast = [] for item in channel['item']['forecast']: @@ -191,6 +199,12 @@ def handle(self, text, mic): def _say_forecast_tomorrow(self, mic, weather): tomorrow = None + + if weather.forecast 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 @@ -198,10 +212,10 @@ def _say_forecast_tomorrow(self, mic, weather): 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 " + @@ -209,6 +223,13 @@ def _say_forecast_tomorrow(self, mic, weather): def _say_forecast(self, mic, weather): forecast_msgs = [] + + # no forecast available + if weather.forecast 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') From fa4afeed175193d7adbd6c379a14d335887b256d Mon Sep 17 00:00:00 2001 From: Andreas Weber Date: Mon, 4 Apr 2016 22:32:44 +0200 Subject: [PATCH 2/2] adapted due to pull request discussion --- plugins/speechhandler/weather/weather.py | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/plugins/speechhandler/weather/weather.py b/plugins/speechhandler/weather/weather.py index 7cd1b4cc3..cbb468ff9 100644 --- a/plugins/speechhandler/weather/weather.py +++ b/plugins/speechhandler/weather/weather.py @@ -80,23 +80,20 @@ def get_weather(location, unit="f"): yql_query = YAHOO_YQL_QUERY % (location.replace('"', ''), unit.replace('"', '')) r = requests.get(YAHOO_YQL_URL, - params={'q': yql_query, - 'format': 'json', - 'env': 'store://datatables.org/alltableswithkeys'}, - headers={'User-Agent': 'Mozilla/5.0'}) + params={ + 'q': yql_query, + 'format': 'json', + 'env': 'store://datatables.org/alltableswithkeys'}, + headers={'User-Agent': 'Mozilla/5.0'}) content = r.json() # make sure we got data try: channel = content['query']['results']['weather']['rss']['channel'] - except: + except KeyError: # return empty Weather - return Weather(city=None, - date=None, - text=None, - temp=None, - forecast=None) + return None current_date = dateutil.parser.parse( - channel['item']['condition']['date']).date() + channel['item']['condition']['date']).date() forecast = [] for item in channel['item']['forecast']: @@ -200,7 +197,7 @@ def handle(self, text, mic): def _say_forecast_tomorrow(self, mic, weather): tomorrow = None - if weather.forecast is None: + if weather is None: mic.say(self.gettext( "Sorry, I had a problem retrieving the weather data.")) return @@ -225,7 +222,7 @@ def _say_forecast(self, mic, weather): forecast_msgs = [] # no forecast available - if weather.forecast is None: + if weather is None: mic.say(self.gettext( "Sorry, I had a problem retrieving the weather data.")) return