X-Git-Url: https://git.toastfreeware.priv.at/chrisu/seepark.git/blobdiff_plain/37d9d91c89b12b4c559f9e0ef10420acd1531c50..ca2ff71b9a5b9d5efcdec8ec56c43c74f0818f4f:/owm.py diff --git a/owm.py b/owm.py index d42ebed..c57b242 100755 --- a/owm.py +++ b/owm.py @@ -8,29 +8,12 @@ from pprint import pprint import argparse -import requests import configparser import os import csv import datetime import math - -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): @@ -39,7 +22,7 @@ def fromtimestamp(timestamp, format): # https://stackoverflow.com/questions/7490660/converting-wind-direction-in-angles-to-text-words def degToCompass(num): - if num is None: + 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"] @@ -58,7 +41,6 @@ def extractweatherdata(w): weather = w['weather'][0]['description'], sky = w['weather'][0]['main'], windspeed = w['wind']['speed'], - winddegrees = w['wind']['deg'], cloudiness = w['clouds']['all'], ) @@ -66,6 +48,7 @@ def extractweatherdata(w): 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['winddegrees'] = w['wind']['deg'] if 'deg' in w['wind'] else math.nan data['winddirection'] = degToCompass(data['winddegrees']) data['precipitation'] = w['rain']['3h'] if 'rain' in w else math.nan @@ -89,13 +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) @@ -106,12 +90,13 @@ def main(configfile): # write to db # write to csv - write_csv(config.get("openweathermap", 'csvfilename'), weather) + write_csv(os.path.expanduser(csvfile), weather) 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)