@@ 1,3 1,5 @@
+from datetime import datetime
+
import requests
# Translation: https://erikflowers.github.io/weather-icons/api-list.html
@@ 60,11 62,44 @@ ICONS = {
}
+def format_temp(temp):
+ return str(round(temp))
+
+
+def get_day(ts):
+ return datetime.utcfromtimestamp(ts).strftime("%a")
+
+
+def get_icon(id, dt=1, sunrise=1, sunset=1):
+ time = ""
+ if sunrise <= dt <= sunset:
+ time = "day"
+ else:
+ time = "night"
+ return ICONS[id][time]
+
+
def get_weather(token, lat, lon):
+ current, daily = [], []
r = requests.get(
"https://api.openweathermap.org/data/2.5/onecall?lat={}&lon={}&appid={}&eclude=minutely,hourly&units=metric".format(
lat, lon, token
)
)
- data = r.json()
- return ICONS[data["current"]["weather"][0]["id"]]["day"]
+ data_current = r.json()["current"]
+ data_daily = r.json()["daily"]
+
+ dt = data_current["dt"]
+ sr = data_current["sunrise"]
+ ss = data_current["sunset"]
+ current.append(get_icon(data_current["current"]["weather"][0]["id"], dt, sr, ss))
+ current.append(format_temp(data_current["temp"]))
+
+ for i in range(1, 4):
+ day = get_day(data_daily[i]["dt"])
+ min = format_temp(data_daily[i]["temp"]["min"])
+ max = format_temp(data_daily[i]["temp"]["max"])
+ icon = get_icon(data_daily[i]["weather"][0]["id"])
+ daily.append([day, min, max, icon])
+
+ return current, daily