58 lines
2.3 KiB
Lua
58 lines
2.3 KiB
Lua
|
|
-- Tomorrow's forecast for the saved or auto-detected location.
|
|||
|
|
-- City stored via memory key "weather.lat" + "weather.lon" + "weather.city",
|
|||
|
|
-- or auto-detected via ipinfo on first run.
|
|||
|
|
local lat = jarvis.memory.recall("weather.lat")
|
|||
|
|
local lon = jarvis.memory.recall("weather.lon")
|
|||
|
|
local city = jarvis.memory.recall("weather.city")
|
|||
|
|
|
|||
|
|
if not lat or not lon then
|
|||
|
|
-- Auto-detect via ipinfo.io (free, no key)
|
|||
|
|
local ip = jarvis.http.json("https://ipinfo.io/json")
|
|||
|
|
if not ip or not ip.loc then
|
|||
|
|
return jarvis.cmd.error("Не получилось узнать местоположение.")
|
|||
|
|
end
|
|||
|
|
lat, lon = ip.loc:match("^([^,]+),(.*)$")
|
|||
|
|
if not lat then
|
|||
|
|
return jarvis.cmd.error("Не получилось разобрать координаты.")
|
|||
|
|
end
|
|||
|
|
city = ip.city or "ваш город"
|
|||
|
|
jarvis.memory.remember("weather.lat", lat)
|
|||
|
|
jarvis.memory.remember("weather.lon", lon)
|
|||
|
|
jarvis.memory.remember("weather.city", city)
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
local url = string.format(
|
|||
|
|
"https://api.open-meteo.com/v1/forecast?latitude=%s&longitude=%s&daily=temperature_2m_max,temperature_2m_min,precipitation_sum,weather_code&timezone=auto&forecast_days=2",
|
|||
|
|
lat, lon
|
|||
|
|
)
|
|||
|
|
local data = jarvis.http.json(url)
|
|||
|
|
if not data or not data.daily then
|
|||
|
|
return jarvis.cmd.error("Прогноз недоступен.")
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
local d = data.daily
|
|||
|
|
-- day 2 = tomorrow (day 1 = today in the array)
|
|||
|
|
local tmax = (d.temperature_2m_max and d.temperature_2m_max[2]) or nil
|
|||
|
|
local tmin = (d.temperature_2m_min and d.temperature_2m_min[2]) or nil
|
|||
|
|
local rain = (d.precipitation_sum and d.precipitation_sum[2]) or 0
|
|||
|
|
local code = (d.weather_code and d.weather_code[2]) or 0
|
|||
|
|
|
|||
|
|
if not tmax then
|
|||
|
|
return jarvis.cmd.error("Прогноз неполный.")
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
local condition = "переменно"
|
|||
|
|
if code == 0 then condition = "ясно"
|
|||
|
|
elseif code <= 3 then condition = "переменная облачность"
|
|||
|
|
elseif code >= 51 and code <= 67 then condition = "дождь"
|
|||
|
|
elseif code >= 71 and code <= 77 then condition = "снег"
|
|||
|
|
elseif code >= 95 then condition = "гроза"
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
local line = string.format("В %s завтра %s. От %d до %d градусов.",
|
|||
|
|
city or "вашем городе", condition, math.floor(tmin + 0.5), math.floor(tmax + 0.5))
|
|||
|
|
if rain > 5 then
|
|||
|
|
line = line .. " Возможны осадки."
|
|||
|
|
end
|
|||
|
|
return jarvis.cmd.ok(line)
|