]> ToastFreeware Gitweb - chrisu/seepark.git/blobdiff - owm.py
README: add python3-flask-sqlalchemy
[chrisu/seepark.git] / owm.py
diff --git a/owm.py b/owm.py
index ada256794cf031357f5eb691256433bb0bf082f6..c57b2422b24550b012bd415458048e99357f2a6a 100755 (executable)
--- a/owm.py
+++ b/owm.py
@@ -8,30 +8,12 @@
 
 from pprint import pprint
 import argparse
-import requests
 import configparser
 import os
 import csv
 import datetime
 import math
-import sys
-
-baseurl = 'http://api.openweathermap.org/data/2.5/weather'
-debug = False
-
-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):
@@ -48,7 +30,6 @@ def degToCompass(num):
 
 
 def extractweatherdata(w):
-    w['wind']['deg'] = math.nan
     data = dict(
         datetime = w['dt'],
         sunrise = w['sys']['sunrise'],
@@ -91,14 +72,14 @@ def write_csv(csv_file, weather_data):
         ])
 
 
-def main(configfile):
+def main(configfile, debug):
     config = configparser.ConfigParser()
     config.read(configfile)
     apikey = config.get('openweathermap', 'apikey')
     cityid = config.get('openweathermap', 'cityid')
     csvfile = config.get("openweathermap", 'csvfilename')
 
-    weather_raw = getweather(apikey, cityid)
+    weather_raw = openweathermap_json(apikey, cityid)
     if debug:
         pprint(weather_raw)
     weather = extractweatherdata(weather_raw)
@@ -116,5 +97,6 @@ if __name__ == '__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)
+    main(args.config, args.debug)