Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed crash in case there is no weather data #483

Merged
merged 2 commits into from
May 3, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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])
43 changes: 32 additions & 11 deletions plugins/speechhandler/weather/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be except KeyError:.

# return empty Weather
return Weather(city=None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just return None instead.

date=None,
text=None,
temp=None,
forecast=None)
current_date = dateutil.parser.parse(
channel['item']['condition']['date']).date()
channel['item']['condition']['date']).date()
forecast = []
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you undo these whitespace changes since they don't belong into this commit.


for item in channel['item']['forecast']:
Expand Down Expand Up @@ -191,24 +199,37 @@ 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
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.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')
Expand Down