From cc126550f28fa8fc19b903d155600b0c63fadcd4 Mon Sep 17 00:00:00 2001 From: Paul Warren Date: Wed, 27 Nov 2019 18:34:17 +1100 Subject: [PATCH] Missed date, and use the proper icon! --- bom_xml.py | 56 ++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/bom_xml.py b/bom_xml.py index 8bed4d2..29be14a 100755 --- a/bom_xml.py +++ b/bom_xml.py @@ -44,30 +44,50 @@ new = {} #current = a['forecast-period'][0] weather = {} -# THe first forecast perios doesn't always have the temperature and other bits -# I chose to just switch to tomorrow +# The first forecast period doesn't always have the temperature and other bits +# I get the overnight low and forecast to substitute when this happens. if (type(belco['forecast-period'][0]) == type(collections.OrderedDict())): - start = 1 + night = 1 else: - start = 0 + night = 0 -weather['today'] = belco['forecast-period'][start] -weather['tomorrow'] = belco['forecast-period'][start+1] -weather['day_after'] = belco['forecast-period'][start+2] +weather['today'] = belco['forecast-period'][0] +weather['tomorrow'] = belco['forecast-period'][1] +weather['day_after'] = belco['forecast-period'][2] -for day in weather.keys(): - new[day] = {} - new[day]['day'] = datetime.datetime.strptime(weather[day]['@start-time-local'], "%Y-%m-%dT%H:%M:%S%z").strftime("%A") + +if night: + for day in weather.keys(): + new[day] = {} + if (day == 'today'): + new[day]['day'] = 'Tonight' + new[day]['max'] = [a for a in weather['tomorrow']['element'] if a['@type'] == 'air_temperature_minimum'][0]['#text'] + new[day]['icon'] = bom_icons[weather['today']['element']['#text']] + #bom_icons[[a for a in weather['tomorrow']['element'] if a['@type'] == 'forecast_icon_code'][0]['#text']] + else: + new[day]['day'] = datetime.datetime.strptime(weather[day]['@start-time-local'], "%Y-%m-%dT%H:%M:%S%z").strftime("%A") + for el in weather[day]['element']: + if type(el) == type(collections.OrderedDict()): + if el['@type'] == "forecast_icon_code": + new[day]['icon'] = bom_icons[el['#text']] + elif el['@type'] == 'air_temperature_maximum': + new[day]['max'] = el['#text'] + +else: + + for day in weather.keys(): + new[day] = {} + new[day]['day'] = datetime.datetime.strptime(weather[day]['@start-time-local'], "%Y-%m-%dT%H:%M:%S%z").strftime("%A") - for el in weather[day]['element']: - if type(el) == type(collections.OrderedDict()): - if el['@type'] == "forecast_icon_code": - new[day]['icon'] = bom_icons[el['#text']] - elif el['@type'] == 'air_temperature_maximum': - new[day]['max'] = el['#text'] - - + for el in weather[day]['element']: + if type(el) == type(collections.OrderedDict()): + if el['@type'] == "forecast_icon_code": + new[day]['icon'] = bom_icons[el['#text']] + elif el['@type'] == 'air_temperature_maximum': + new[day]['max'] = el['#text'] + + with open('bom.json', 'w') as output: output.write(json.dumps(new)) output.close()