Forecast notification

This commit is contained in:
Adam Cooper 2025-03-08 09:45:29 -05:00
parent 6ea7b44fbb
commit 088612e670

View file

@ -16,7 +16,7 @@ local string = string
local type = type
local tonumber = tonumber
-- OpenWeatherMap -- TODO: Change to PirateWeather
-- PirateWeather
-- current weather and X-days forecast
-- lina.widget.weather
@ -27,9 +27,9 @@ local function factory(args)
local APPID = args.APPID -- mandatory api key
local timeout = args.timeout or 900 -- 15 min
local current_call = args.current_call
or "curl -s 'https://api.openweathermap.org/data/2.5/weather?lat=%s&lon=%s&APPID=%s&units=%s&lang=%s'"
or 'curl -s "https://api.pirateweather.net/forecast/%s/%s,%s?units=%s&exclude=daily,minutely,hourly"'
local forecast_call = args.forecast_call
or "curl -s 'https://api.openweathermap.org/data/2.5/forecast?lat=%s&lon=%s&APPID=%s&cnt=%s&units=%s&lang=%s'"
or 'curl -s "https://api.pirateweather.net/forecast/%s/%s,%s?units=%s&exclude=currently,minutely,hourly"'
local lat = args.lat or 0 -- placeholder
local lon = args.lon or 0 -- placeholder
local units = args.units or "metric"
@ -90,18 +90,18 @@ local function factory(args)
end
function weather.forecast_update()
local cmd = string.format(forecast_call, lat, lon, APPID, cnt, units, lang)
local cmd = string.format(forecast_call, APPID, lat, lon, units)
helpers.async(cmd, function(f)
local err
weather_now, _, err = json.decode(f, 1, nil)
if not err and type(weather_now) == "table" and tonumber(weather_now["cod"]) == 200 then
if not err and type(weather_now) == "table" then
weather.notification_text = ""
for i = 1, weather_now["cnt"], math.floor(weather_now["cnt"] / cnt) do
for i = 1, 5 do -- TODO: Don't hardcode this table length at 5
weather.notification_text = weather.notification_text
.. notification_text_fun(weather_now["list"][i])
if i < weather_now["cnt"] then
.. notification_text_fun(weather_now["daily"]["data"][i])
if i < 5 then
weather.notification_text = weather.notification_text .. "\n"
end
end
@ -110,16 +110,20 @@ local function factory(args)
end
function weather.update()
local cmd = string.format(current_call, lat, lon, APPID, units, lang)
local cmd = string.format(current_call, APPID, lat, lon, units)
helpers.async(cmd, function(f)
local err
weather_now, _, err = json.decode(f, 1, nil)
--[[ TODO: Try nesting async calls; otherwise write a new function for the forecast call.
local sun_position_cmd = string.format(forecast_call, APPID, lat, lon, units)
sun_position, _, err = json.decode(sun_position_cmd, 1, nil)
--]]
if not err and type(weather_now) == "table" and tonumber(weather_now["cod"]) == 200 then
local sunrise = tonumber(weather_now["sys"]["sunrise"])
local sunset = tonumber(weather_now["sys"]["sunset"])
local icon = weather_now["weather"][1]["icon"]
if not err and type(weather_now) == "table" then
local sunrise = tonumber(sun_position["daily"]["data"][1]["sunriseTime"])
local sunset = tonumber(weather_now["daily"]["data"][1]["sunsetTime"])
local icon = weather_now["daily"]["icon"]
local loc_now = os.time()
if sunrise <= loc_now and loc_now <= sunset then