]> ToastFreeware Gitweb - chrisu/seepark.git/blobdiff - owm.py
PEP8 coding style changes.
[chrisu/seepark.git] / owm.py
diff --git a/owm.py b/owm.py
index 1e03670c7dbeb9a8b4e92b86e1b98216ae4c7d7c..237c3d771e20fee2098ec64cb68a077ed7439d82 100755 (executable)
--- a/owm.py
+++ b/owm.py
 # cityid=..
 # 3319578 for Obsteig, AT
 
-from pprint import pprint
-import requests
+# needed packaes: python3-mysqldb python3-sqlalchemy
+
+import argparse
 import configparser
-import os
+import csv
 import datetime
+import json
+import math
+import os
+from pprint import pprint
 
-baseurl = 'http://api.openweathermap.org/data/2.5/weather'
-debug = False
+import sqlalchemy
+from sqlalchemy import create_engine, Table, URL
 
-def getweather(apikey, cityid):
-    query = baseurl + '?units=metric&APPID={}&id={}&lang=de'.format(apikey, cityid)
-    try:
-        response = requests.get(query)
-        if response.status_code != 200:
-            response = 'N/A'
-            return response
-        else:
-            weatherdata = response.json()
-            return weatherdata
-    except requests.exceptions.RequestException as error:
-        print (error)
-        sys.exit(1)
+from seeparklib.openweathermap import openweathermap_json
 
 
-def fromtimestamp(timestamp, format):
-    return datetime.datetime.fromtimestamp(timestamp).strftime(format)
+def fromtimestamp(timestamp):
+    return datetime.datetime.fromtimestamp(timestamp)
 
 
 # https://stackoverflow.com/questions/7490660/converting-wind-direction-in-angles-to-text-words
-def degToCompass(num):
-    if num is None:
+def deg_to_compass(num):
+    if num is None or num is math.nan:
         return 'N/A'
     val=int((num/22.5)+.5)
     arr=["N","NNO","NO","ONO","O","OSO", "SO", "SSO","S","SSW","SW","WSW","W","WNW","NW","NNW"]
     return arr[(val % 16)]
 
 
-def extractweatherdata(w):
+def extract_weather_data(w):
     data = dict(
-        datetime = w['dt'],
-        sunrise = w['sys']['sunrise'],
-        sunset = w['sys']['sunset'],
+        datetime = fromtimestamp(w['dt']),
+        sunrise = fromtimestamp(w['sys']['sunrise']),
+        sunset = fromtimestamp(w['sys']['sunset']),
         temp = w['main']['temp'],
         pressure = w['main']['pressure'],
         humidity = w['main']['humidity'],
-        visibility = w['visibility'],
         weather = w['weather'][0]['description'],
         sky = w['weather'][0]['main'],
         windspeed = w['wind']['speed'],
-        winddegrees = w['wind']['deg'],
         cloudiness = w['clouds']['all'],
     )
 
-    data['sunrise_t'] = fromtimestamp(data['sunrise'], '%H:%M:%S')
-    data['sunset_t'] = fromtimestamp(data['sunset'], '%H:%M:%S')
-    data['date'] = fromtimestamp(data['datetime'], '%Y-%m-%d')
-    data['time'] = fromtimestamp(data['datetime'], '%H:%M:%S')
-    data['winddirection'] = degToCompass(data['winddegrees'])
-    data['precipitation'] = w['rain']['3h'] if 'rain' in w else 'N/A'
+    data['winddegrees'] = w['wind']['deg'] if 'deg' in w['wind'] else math.nan
+    data['winddirection'] = deg_to_compass(data['winddegrees'])
+    data['precipitation'] = w['rain']['3h'] if 'rain' in w and w['rain'].get('3h') else math.nan
+    data['visibility'] = w.get('visibility', math.nan)
 
     return data
-        
 
-def main():
+
+def write_csv(csv_file, weather_data):
+    """output like wetter.at.pl"""
+    with open(csv_file, "a", newline="") as file:
+        writer = csv.writer(file, dialect="excel", delimiter=';')
+        writer.writerow([
+            weather_data['datetime'].date(),
+            weather_data['datetime'].time(),
+            weather_data['sunrise'].time(),
+            weather_data['sunset'].time(),
+            "{:.2f}".format(weather_data['temp']),
+            "{:.2f} mm/h".format(weather_data['precipitation']),
+            "{:.1f} km/h {}".format(weather_data['windspeed'], weather_data['winddirection']),
+            weather_data['weather'],
+            "{}".format(weather_data['cloudiness'])
+        ])
+
+
+def write_db(config, url, weather_json, weather_data):
+    user = config.get('database', 'user')
+    pwd = config.get('database','password')
+    host = config.get('database','hostname')
+    db = config.get('database','database')
+
+    db_url = URL.create(drivername='mysql+mysqldb', username=user, password=pwd, host=host, database=db)
+    engine = create_engine(db_url, echo=False)
+    with engine.connect() as conn:
+        row = dict(cityid=config.get('openweathermap', 'cityid'), url=url, result=json.dumps(weather_json))
+        row.update(weather_data)
+        for key, value in row.items():
+            if isinstance(value, float) and math.isnan(value):
+                row[key] = None
+        metadata = sqlalchemy.MetaData()
+        openweathermap_table = Table('openweathermap', metadata, autoload_with=engine)
+        ins = openweathermap_table.insert().prefix_with('IGNORE').values(**row)
+        conn.execute(ins)
+        conn.commit()
+
+
+def main(configfile, debug):
     config = configparser.ConfigParser()
-    config.read(os.path.expanduser('~/seewasser.ini'))
-    apikey = config.get('openweathermap', 'apikey');
-    cityid = config.get('openweathermap', 'cityid');
+    config.read(configfile)
+    apikey = config.get('openweathermap', 'apikey')
+    cityid = config.get('openweathermap', 'cityid')
+    csvfile = config.get("openweathermap", 'csvfilename')
 
-    weather_raw = getweather(apikey, cityid)
+    url, weather_json = openweathermap_json(apikey, cityid)
     if debug:
-        pprint(weather_raw)
-    weather = extractweatherdata(weather_raw)
+        pprint(weather_json)
+    weather_data = extract_weather_data(weather_json)
     if debug:
-        pprint(weather)
-    # TODO:
+        pprint(weather_data)
+
     # write to db
+    write_db(config, url, weather_json, weather_data)
+
     # write to csv
-    
-    # output like wetter.at.pl
-    print(
-        weather['date'] + ';' +
-        weather['time'] + ';' +
-        weather['sunrise_t'] + ';' +
-        weather['sunset_t'] + ';' +
-        str(weather['temp']) + ';' +
-        str(weather['precipitation']) + ' mm/h;' +
-        str(weather['windspeed']) + ' km/h ' + weather['winddirection'] + ';' +
-        weather['weather'] + ';' +
-        str(weather['cloudiness'])
-    )
+    write_csv(os.path.expanduser(csvfile), weather_data)
+
 
-    
 if __name__ == '__main__':
-    main()
+    default_config_file = os.path.expanduser('~/seewasser.ini')
+    parser = argparse.ArgumentParser(description='Get OpenWeathermap data')
+    parser.add_argument('--config', default=default_config_file, help='configuration file')
+    parser.add_argument('--debug', action='store_true', default=False, help='print debug information')
+    args = parser.parse_args()
+    main(args.config, args.debug)