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):
41 val=int((num/22.5)+.5)
42 arr=["N","NNO","NO","ONO","O","OSO", "SO", "SSO","S","SSW","SW","WSW","W","WNW","NW","NNW"]
43 return arr[(val % 16)]
46 def extractweatherdata(w):
49 sunrise = w['sys']['sunrise'],
50 sunset = w['sys']['sunset'],
51 temp = w['main']['temp'],
52 pressure = w['main']['pressure'],
53 humidity = w['main']['humidity'],
54 visibility = w['visibility'],
55 weather = w['weather'][0]['description'],
56 sky = w['weather'][0]['main'],
57 windspeed = w['wind']['speed'],
58 winddegrees = w['wind']['deg'],
59 cloudiness = w['clouds']['all'],
62 data['sunrise_t'] = fromtimestamp(data['sunrise'], '%H:%M:%S')
63 data['sunset_t'] = fromtimestamp(data['sunset'], '%H:%M:%S')
64 data['date'] = fromtimestamp(data['datetime'], '%Y-%m-%d')
65 data['time'] = fromtimestamp(data['datetime'], '%H:%M:%S')
66 data['winddirection'] = degToCompass(data['winddegrees'])
67 data['precipitation'] = w['rain']['3h'] if 'rain' in w else 'N/A'
73 config = configparser.ConfigParser()
74 config.read(os.path.expanduser('~/seewasser.ini'))
75 apikey = config.get('openweathermap', 'apikey');
76 cityid = config.get('openweathermap', 'cityid');
78 weather_raw = getweather(apikey, cityid)
81 weather = extractweatherdata(weather_raw)
88 # output like wetter.at.pl
90 weather['date'] + ';' +
91 weather['time'] + ';' +
92 weather['sunrise_t'] + ';' +
93 weather['sunset_t'] + ';' +
94 str(weather['temp']) + ';' +
95 str(weather['precipitation']) + ' mm/h;' +
96 str(weather['windspeed']) + ' km/h ' + weather['winddirection'] + ';' +
97 weather['weather'] + ';' +
98 str(weather['cloudiness'])
102 if __name__ == '__main__':