Last active
December 30, 2021 21:07
-
-
Save Br3nda/5da3fabab6fd78a0442f001be6145b28 to your computer and use it in GitHub Desktop.
Python script for poop levels at wellington beaches
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from xml.etree import ElementTree | |
import requests | |
import json | |
import sys | |
import urllib | |
site = sys.argv[1] # e.g.'Seatoun Beach at Inglis Street', | |
if (len(sys.argv) > 2): | |
key = sys.argv[2] | |
else: | |
key = None | |
url = "http://hilltop.gw.govt.nz/Data.hts" | |
params = { | |
'Service': 'Hilltop', | |
'Request': 'GetData', | |
'Site': site, | |
'Measurement': 'Enterococci Bacteria' | |
} | |
# WRC's site only accepts the old %20 style of encoding | |
params = urllib.parse.urlencode(params, quote_via=urllib.parse.quote) | |
response = requests.get(url, params=params) | |
tree = ElementTree.fromstring(response.content) | |
reading = tree.find('Measurement').find('Data').find('E') | |
data = {'Enterococci': reading.find('Value').text} | |
for p in reading.findall("Parameter"): | |
data[p.attrib['Name']] = p.attrib['Value'] | |
if key: | |
print(data.get(key)) | |
else: | |
print(json.dumps(data)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
sample home assistant config: