3 # needs ~/seepark.ini with
7 # 3319578 for Obsteig, AT
9 from pprint import pprint
15 baseurl = 'http://api.openweathermap.org/data/2.5/weather'
18 def getweather(apikey, cityid):
19 query = baseurl + '?units=metric&APPID={}&id={}&lang=de'.format(apikey, cityid)
21 response = requests.get(query)
22 if response.status_code != 200:
26 weatherdata = response.json()
28 except requests.exceptions.RequestException as error:
33 def fromtimestamp(timestamp, format):
34 return datetime.datetime.fromtimestamp(timestamp).strftime(format)
37 # https://stackoverflow.com/questions/7490660/converting-wind-direction-in-angles-to-text-words
38 def degToCompass(num):
39 val=int((num/22.5)+.5)
40 arr=["N","NNO","NO","ONO","O","OSO", "SO", "SSO","S","SSW","SW","WSW","W","WNW","NW","NNW"]
41 return arr[(val % 16)]
44 def extractweatherdata(w):
46 datetime = w.get('dt'),
47 sunrise = w.get('sys').get('sunrise'),
48 sunset = w.get('sys').get('sunset'),
49 temp = w.get('main').get('temp'),
50 pressure = w.get('main').get('pressure'),
51 humidity = w.get('main').get('humidity'),
52 visibility = w.get('visibility'),
53 weather=w['weather'][0]['description'],
54 sky=w['weather'][0]['main'],
55 windspeed=w.get('wind').get('speed'),
56 winddegrees=w.get('wind').get('deg'),
57 cloudiness=w.get('clouds').get('all'),
60 data['sunrise_t'] = fromtimestamp(data['sunrise'], '%H:%M:%S')
61 data['sunset_t'] = fromtimestamp(data['sunset'], '%H:%M:%S')
62 data['date'] = fromtimestamp(data['datetime'], '%Y-%m-%d')
63 data['time'] = fromtimestamp(data['datetime'], '%H:%M:%S')
64 data['winddirection'] = degToCompass(data['winddegrees'])
70 config = configparser.ConfigParser()
71 config.read(os.path.expanduser('~/seewasser.ini'))
72 apikey = config.get('openweathermap', 'apikey');
73 cityid = config.get('openweathermap', 'cityid');
75 weather_raw = getweather(apikey, cityid)
78 weather = extractweatherdata(weather_raw)
85 # output like wetter.at.pl
87 weather['date'] + ';' +
88 weather['time'] + ';' +
89 weather['sunrise_t'] + ';' +
90 weather['sunset_t'] + ';' +
91 str(weather['temp']) + ';' +
92 ';' + # precipitation missing? or only in XML: https://openweathermap.org/current ?
93 str(weather['windspeed']) + 'km/h ' + weather['winddirection'] + ';' +
94 weather['weather'] + ';' +
95 str(weather['cloudiness'])
99 if __name__ == '__main__':