Add riverconditions.py
This commit is contained in:
parent
2fc42b56f7
commit
a20d1bb422
32
riverconditions.py
Normal file
32
riverconditions.py
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
#!/usr/home/homeassistant/bin/python3
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
import requests
|
||||||
|
|
||||||
|
mappings = {
|
||||||
|
'temperature': 'watertemp',
|
||||||
|
'streamflow': 'flow',
|
||||||
|
'height': 'height',
|
||||||
|
}
|
||||||
|
if __name__ == '__main__':
|
||||||
|
exit_code = 1
|
||||||
|
for i in range(4):
|
||||||
|
try:
|
||||||
|
result = dict()
|
||||||
|
station = sys.argv[1]
|
||||||
|
url = f'https://waterservices.usgs.gov/nwis/iv/?format=json&sites={station}&siteStatus=all'
|
||||||
|
req = requests.get (url, timeout=3)
|
||||||
|
if req.ok:
|
||||||
|
j = req.json()
|
||||||
|
for v in j['value']['timeSeries']:
|
||||||
|
variable_name = v['variable']['variableName'].lower()
|
||||||
|
for k in mappings.keys():
|
||||||
|
if k in variable_name:
|
||||||
|
result[mappings[k]] = float(v['values'][0]['value'][0]['value'])
|
||||||
|
print (json.dumps ({'data': result}))
|
||||||
|
exit_code = 0
|
||||||
|
break
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
sys.exit (exit_code)
|
Loading…
Reference in New Issue
Block a user