owm.py: add rain/precipitation data
authorgregor herrmann <gregor@toastfreeware.priv.at>
Sat, 9 Jun 2018 13:06:44 +0000 (15:06 +0200)
committergregor herrmann <gregor@toastfreeware.priv.at>
Sat, 9 Jun 2018 13:06:44 +0000 (15:06 +0200)
they are only in the api response when OWM has rain data

good test station: 3688451
https://openweathermap.org/city/3688451
Buenaventura, CO

owm.py

diff --git a/owm.py b/owm.py
index 2fefc6a20b3780ab3b03963b7526a2ced0e7ac0e..71f367730de84ab40b206b3ca0a3fbe44f8ad282 100755 (executable)
--- a/owm.py
+++ b/owm.py
@@ -57,6 +57,7 @@ def extractweatherdata(w):
         windspeed=w.get('wind').get('speed'),
         winddegrees=w.get('wind').get('deg'),
         cloudiness=w.get('clouds').get('all'),
+        rain=w.get('rain'),
     )
 
     data['sunrise_t'] = fromtimestamp(data['sunrise'], '%H:%M:%S')
@@ -64,6 +65,10 @@ def extractweatherdata(w):
     data['date'] = fromtimestamp(data['datetime'], '%Y-%m-%d')
     data['time'] = fromtimestamp(data['datetime'], '%H:%M:%S')
     data['winddirection'] = degToCompass(data['winddegrees'])
+    if not data['rain'] is None:
+        data['precipitation']=data['rain'].get('3h')
+    else:
+        data['precipitation']='N/A'
 
     return data
         
@@ -91,8 +96,8 @@ def main():
         weather['sunrise_t'] + ';' +
         weather['sunset_t'] + ';' +
         str(weather['temp']) + ';' +
-        ';' + # precipitation missing? or only in XML: https://openweathermap.org/current ?
-        str(weather['windspeed']) + 'km/h ' + weather['winddirection'] + ';' +
+        str(weather['precipitation']) + ' mm/h;' +
+        str(weather['windspeed']) + ' km/h ' + weather['winddirection'] + ';' +
         weather['weather'] + ';' +
         str(weather['cloudiness'])
     )