Forecast notification
This commit is contained in:
parent
6ea7b44fbb
commit
088612e670
1 changed files with 17 additions and 13 deletions
|
@ -16,7 +16,7 @@ local string = string
|
||||||
local type = type
|
local type = type
|
||||||
local tonumber = tonumber
|
local tonumber = tonumber
|
||||||
|
|
||||||
-- OpenWeatherMap -- TODO: Change to PirateWeather
|
-- PirateWeather
|
||||||
-- current weather and X-days forecast
|
-- current weather and X-days forecast
|
||||||
-- lina.widget.weather
|
-- lina.widget.weather
|
||||||
|
|
||||||
|
@ -27,9 +27,9 @@ local function factory(args)
|
||||||
local APPID = args.APPID -- mandatory api key
|
local APPID = args.APPID -- mandatory api key
|
||||||
local timeout = args.timeout or 900 -- 15 min
|
local timeout = args.timeout or 900 -- 15 min
|
||||||
local current_call = args.current_call
|
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
|
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 lat = args.lat or 0 -- placeholder
|
||||||
local lon = args.lon or 0 -- placeholder
|
local lon = args.lon or 0 -- placeholder
|
||||||
local units = args.units or "metric"
|
local units = args.units or "metric"
|
||||||
|
@ -90,18 +90,18 @@ local function factory(args)
|
||||||
end
|
end
|
||||||
|
|
||||||
function weather.forecast_update()
|
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)
|
helpers.async(cmd, function(f)
|
||||||
local err
|
local err
|
||||||
weather_now, _, err = json.decode(f, 1, nil)
|
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 = ""
|
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
|
weather.notification_text = weather.notification_text
|
||||||
.. notification_text_fun(weather_now["list"][i])
|
.. notification_text_fun(weather_now["daily"]["data"][i])
|
||||||
if i < weather_now["cnt"] then
|
if i < 5 then
|
||||||
weather.notification_text = weather.notification_text .. "\n"
|
weather.notification_text = weather.notification_text .. "\n"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -110,16 +110,20 @@ local function factory(args)
|
||||||
end
|
end
|
||||||
|
|
||||||
function weather.update()
|
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)
|
helpers.async(cmd, function(f)
|
||||||
local err
|
local err
|
||||||
weather_now, _, err = json.decode(f, 1, nil)
|
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
|
if not err and type(weather_now) == "table" then
|
||||||
local sunrise = tonumber(weather_now["sys"]["sunrise"])
|
local sunrise = tonumber(sun_position["daily"]["data"][1]["sunriseTime"])
|
||||||
local sunset = tonumber(weather_now["sys"]["sunset"])
|
local sunset = tonumber(weather_now["daily"]["data"][1]["sunsetTime"])
|
||||||
local icon = weather_now["weather"][1]["icon"]
|
local icon = weather_now["daily"]["icon"]
|
||||||
local loc_now = os.time()
|
local loc_now = os.time()
|
||||||
|
|
||||||
if sunrise <= loc_now and loc_now <= sunset then
|
if sunrise <= loc_now and loc_now <= sunset then
|
||||||
|
|
Loading…
Add table
Reference in a new issue