Skip to content

Instantly share code, notes, and snippets.

@Br3nda
Last active December 30, 2021 21:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Br3nda/5da3fabab6fd78a0442f001be6145b28 to your computer and use it in GitHub Desktop.
Save Br3nda/5da3fabab6fd78a0442f001be6145b28 to your computer and use it in GitHub Desktop.
Python script for poop levels at wellington beaches
#!/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))
@Br3nda
Copy link
Author

Br3nda commented Feb 25, 2020

sample output:

{
  "Enterococci": "4",
  "Lab Sample Number": "20/8801-12",
  "Method": "Enteroc-2",
  "GWRC Programme": "Rec WQ",
  "Sample type": "Routine Sample",
  "Weather": "Overcast",
  "Wind Direction": "NE",
  "Wind strength": "Moderate",
  "Tide": "Flood",
  "Tidal height": "High",
  "Sewage Overflow": "N",
  "Seaweed %": "0"
}

Note that the Enterococci never gets to zero, and instead says <4. e.g. { "Enterococci": "<4"}

@Br3nda
Copy link
Author

Br3nda commented Feb 25, 2020

sample home assistant config:

switch:
- platform: command_line
  name: "Seatoun Beach Enterococci Bacteria"
  command: "path-to-script/wellington-beach-poop.py  'Seatoun Beach at Inglis Street' 'Enterococci'"
  scan_interval: 43200 #12 Hours
  unit_of_measurement: "n/100ml"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment