bom images and xml parser

This commit is contained in:
Paul Warren 2019-11-26 23:42:51 +11:00
parent 295a9020f5
commit ede6d1bc3b
28 changed files with 73 additions and 0 deletions

73
bom_xml.py Executable file
View file

@ -0,0 +1,73 @@
#!/usr/bin/env python3
import xmltodict
import urllib
import datetime
import json
import collections
# BOM Weather XML URL for my area:
#
# see http://www.bom.gov.au/catalogue/data-feeds.shtml
# Adjust aac for your forecast region and the URL as required.
aac = "NSW_PT254";
bom_url = "ftp://ftp.bom.gov.au/anon/gen/fwo/IDN11060.xml";
bom_icons = {
'1': 'SUNNY.JPG',
'2': 'CLEAR.JPG',
'3': 'PCLOUD.JPG',
'4': 'CLOUDY.JPG',
'6': 'HAZE.JPG',
'8': 'LRAIN.JPG',
'9': 'WIND.JPG',
'10': 'FOG.JPG',
'11': 'SHOWER.JPG',
'12': 'RAIN.JPG',
'13': 'DUST.JPG',
'14': 'FROST.JPG',
'15': 'SNOW.JPG',
'16': 'STORM.JPG',
'17': 'LSHOWE.JPG',
'18': 'HSHOWE.JPG',
'19': 'CYCLON.JPG'
}
content = urllib.request.urlopen(bom_url)
xmldict = xmltodict.parse(content.read())
belco = [a for a in xmldict['product']['forecast']['area'] if a['@aac'] == aac][0]
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
if (type(belco['forecast-period'][0]) == type(collections.OrderedDict())):
start = 1
else:
start = 0
weather['today'] = belco['forecast-period'][start]
weather['tomorrow'] = belco['forecast-period'][start+1]
weather['day_after'] = belco['forecast-period'][start+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")
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()

BIN
images/0s.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

BIN
images/1s.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

BIN
images/2s.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

BIN
images/3s.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

BIN
images/4s.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

BIN
images/5s.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

BIN
images/6s.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.5 KiB

BIN
images/7s.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

BIN
images/8s.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

BIN
images/9s.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.5 KiB

BIN
images/clear.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

BIN
images/cloudy.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

BIN
images/cyclon.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

BIN
images/dust.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

BIN
images/fog.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

BIN
images/frost.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

BIN
images/haze.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

BIN
images/hshowe.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

BIN
images/lrain.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

BIN
images/lshowe.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2 KiB

BIN
images/pcloud.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

BIN
images/rain.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

BIN
images/shower.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

BIN
images/snow.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2 KiB

BIN
images/storm.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

BIN
images/sunny.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

BIN
images/wind.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB