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 -*-
###
# 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.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
@ -480,6 +480,7 @@ class Weather(callbacks.Plugin):
# handle forecast data. This is internally stored as a dict with integer keys (days from now)
# with the forecast text as values.
forecastdata = {}
if 'forecast' in data:
for forecastday in data['forecast']['txt_forecast']['forecastday']:
# Slightly different wording and results (e.g. rainfall for X inches vs. X cm) are given
# depending on whether imperial or metric units are the same.
@ -509,6 +510,7 @@ class Weather(callbacks.Plugin):
if args[k]:
output += "| {0}: {1} ".format(self._bold(k.title()), outdata[k])
if forecastdata:
# Add in the first two forecasts item in conditions + the "last updated" time.
output += "| {0}: {1}".format(self._bold(forecastdata[0]['day']), forecastdata[0]['text'])
output += " {0}: {1}".format(self._bold(forecastdata[1]['day']), forecastdata[1]['text'])