Handle KeyErrors in common weather segment

If the query doesn't return valid weather information (e.g. if the
location query is invalid) it will return an empty weather segment.

Closes #128.
This commit is contained in:
Kim Silkebækken 2013-01-25 17:30:20 +01:00
parent 1564e338b3
commit 38195f490e
1 changed files with 5 additions and 2 deletions

View File

@ -140,8 +140,11 @@ def weather(unit='c', location_query=None):
}
url = 'http://query.yahooapis.com/v1/public/yql?' + _urllib_urlencode(query_data)
response = json.loads(_urllib_read(url))
try:
condition = response['query']['results']['weather']['rss']['channel']['item']['condition']
condition_code = int(condition['code'])
except KeyError:
return None
icon = u''
for icon, codes in weather_conditions_codes.items():
if condition_code in codes: