Weather: fix errors when forecast info isn't available

This commit is contained in:
James Lu 2018-01-28 16:26:57 -08:00
parent e13eb7203c
commit 0f5fdcfefd

View File

@ -1,7 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
### ###
# Copyright (c) 2012-2014, spline # Copyright (c) 2012-2014, spline
# Copyright (c) 2014-2017, James Lu <james@overdrivenetworks.com> # Copyright (c) 2014-2018, James Lu <james@overdrivenetworks.com>
# All rights reserved. # All rights reserved.
# #
# Permission is hereby granted, free of charge, to any person obtaining a copy of # Permission is hereby granted, free of charge, to any person obtaining a copy of
@ -480,15 +480,16 @@ class Weather(callbacks.Plugin):
# handle forecast data. This is internally stored as a dict with integer keys (days from now) # handle forecast data. This is internally stored as a dict with integer keys (days from now)
# with the forecast text as values. # with the forecast text as values.
forecastdata = {} forecastdata = {}
for forecastday in data['forecast']['txt_forecast']['forecastday']: if 'forecast' in data:
# Slightly different wording and results (e.g. rainfall for X inches vs. X cm) are given for forecastday in data['forecast']['txt_forecast']['forecastday']:
# depending on whether imperial or metric units are the same. # Slightly different wording and results (e.g. rainfall for X inches vs. X cm) are given
if args['imperial']: # depending on whether imperial or metric units are the same.
text = forecastday['fcttext'] if args['imperial']:
else: text = forecastday['fcttext']
text = forecastday['fcttext_metric'] else:
forecastdata[int(forecastday['period'])] = {'day': forecastday['title'], text = forecastday['fcttext_metric']
'text': text} forecastdata[int(forecastday['period'])] = {'day': forecastday['title'],
'text': text}
output = "{0} :: {1} ::".format(self._bold(outdata['location']), outdata['weather']) output = "{0} :: {1} ::".format(self._bold(outdata['location']), outdata['weather'])
output += " {0} ".format(outdata['temp']) output += " {0} ".format(outdata['temp'])
@ -509,9 +510,10 @@ class Weather(callbacks.Plugin):
if args[k]: if args[k]:
output += "| {0}: {1} ".format(self._bold(k.title()), outdata[k]) output += "| {0}: {1} ".format(self._bold(k.title()), outdata[k])
# Add in the first two forecasts item in conditions + the "last updated" time. if forecastdata:
output += "| {0}: {1}".format(self._bold(forecastdata[0]['day']), forecastdata[0]['text']) # Add in the first two forecasts item in conditions + the "last updated" time.
output += " {0}: {1}".format(self._bold(forecastdata[1]['day']), forecastdata[1]['text']) output += "| {0}: {1}".format(self._bold(forecastdata[0]['day']), forecastdata[0]['text'])
output += " {0}: {1}".format(self._bold(forecastdata[1]['day']), forecastdata[1]['text'])
if args['updated']: if args['updated']:
# Round updated time (given as a string) to the nearest unit. # Round updated time (given as a string) to the nearest unit.