# 3319578 for Obsteig, AT
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
# 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"]
def extractweatherdata(w):
+ w['wind']['deg'] = math.nan
data = dict(
datetime = w['dt'],
sunrise = w['sys']['sunrise'],
weather = w['weather'][0]['description'],
sky = w['weather'][0]['main'],
windspeed = w['wind']['speed'],
- winddegrees = w['wind']['deg'],
cloudiness = w['clouds']['all'],
)
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 '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')
+ csvfile = config.get("openweathermap", 'csvfilename')
weather_raw = getweather(apikey, cityid)
if debug:
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(os.path.expanduser(csvfile), 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)