-
Notifications
You must be signed in to change notification settings - Fork 2
/
gather_weather.py
executable file
·57 lines (47 loc) · 1.6 KB
/
gather_weather.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python3
# ✓
import os
import pyowm
import calendar_tools
import serialize
TZ = calendar_tools.get_local_timezone()
OWM_API_KEY = os.getenv("OWM_API_KEY")
OWM_LOCATION = os.getenv("OWM_LOCATION") # ex: "Berlin,DE"
def temp_to_thermometer(temp):
if temp < 5:
return "0"
elif temp < 10:
return "1"
elif temp < 15:
return "2"
elif temp < 20:
return "3"
else:
return "4"
def get_weather():
owm = pyowm.OWM(OWM_API_KEY)
mgr = owm.weather_manager()
observation = mgr.weather_at_place(OWM_LOCATION)
cw = observation.weather
forecast = mgr.forecast_at_place(OWM_LOCATION, "3h").forecast
forecast.actualize()
return {
"temp": cw.temperature("celsius")["temp"],
"thermometer": temp_to_thermometer(cw.temperature("celsius")["temp"]),
"sunrise_ts": cw.sunrise_time(timeformat="date").astimezone(TZ),
"sunset_ts": cw.sunset_time(timeformat="date").astimezone(TZ),
"status": cw.status if cw.status != "Rain" else cw.detailed_status.lower(),
"forecast": [
{
"temp": nw.temperature("celsius")["temp"],
"thermometer": temp_to_thermometer(nw.temperature("celsius")["temp"]),
"status": (
nw.status if nw.status != "Rain" else nw.detailed_status.lower()
),
"start_ts": nw.reference_time(timeformat="date").astimezone(TZ),
}
for nw in forecast.weathers[:4]
],
}
if __name__ == "__main__":
print(serialize.serialize({"weather": get_weather()}))