X-Git-Url: https://git.toastfreeware.priv.at/chrisu/seepark.git/blobdiff_plain/e4f2c3354b555c39305f4d91c3be50f875e7ed72..f884244feef20d1216cbbbaf123d15d9732758cb:/owm.py?ds=sidebyside diff --git a/owm.py b/owm.py index 71f3677..d42ebed 100755 --- a/owm.py +++ b/owm.py @@ -7,10 +7,13 @@ # 3319578 for Obsteig, AT 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 @@ -45,19 +48,18 @@ def degToCompass(num): def extractweatherdata(w): data = dict( - datetime = w.get('dt'), - sunrise = w.get('sys').get('sunrise'), - sunset = w.get('sys').get('sunset'), - temp = w.get('main').get('temp'), - pressure = w.get('main').get('pressure'), - humidity = w.get('main').get('humidity'), - visibility = w.get('visibility'), - weather=w['weather'][0]['description'], - sky=w['weather'][0]['main'], - windspeed=w.get('wind').get('speed'), - winddegrees=w.get('wind').get('deg'), - cloudiness=w.get('clouds').get('all'), - rain=w.get('rain'), + datetime = w['dt'], + sunrise = w['sys']['sunrise'], + sunset = 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') @@ -65,19 +67,33 @@ 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' + data['precipitation'] = w['rain']['3h'] if 'rain' in w else 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['date'], + weather_data['time'], + weather_data['sunrise_t'], + weather_data['sunset_t'], + "{:.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 main(configfile): 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') weather_raw = getweather(apikey, cityid) if debug: @@ -85,23 +101,17 @@ def main(): weather = extractweatherdata(weather_raw) if debug: pprint(weather) + # TODO: # write to db + # 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(config.get("openweathermap", 'csvfilename'), weather) + - 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') + args = parser.parse_args() + main(args.config)