2015-07-02 12:06:07 +02:00
|
|
|
--[[
|
2017-09-04 12:43:00 +02:00
|
|
|
|
|
|
|
Licensed under GNU General Public License v2
|
2017-09-11 06:51:52 +02:00
|
|
|
* (c) 2015, Luca CPZ
|
2017-09-04 12:43:00 +02:00
|
|
|
|
2015-07-02 12:06:07 +02:00
|
|
|
--]]
|
|
|
|
|
2022-03-22 19:45:38 +01:00
|
|
|
local helpers = require(tostring(...):match(".*lain") .. ".helpers")
|
|
|
|
local json = require(tostring(...):match(".*lain") .. ".util").dkjson
|
2017-02-08 20:45:11 +01:00
|
|
|
local focused = require("awful.screen").focused
|
|
|
|
local naughty = require("naughty")
|
|
|
|
local wibox = require("wibox")
|
2018-11-28 20:43:04 +01:00
|
|
|
local math = math
|
|
|
|
local os = os
|
|
|
|
local string = string
|
2019-04-08 12:37:38 +02:00
|
|
|
local type = type
|
2018-11-28 20:43:04 +01:00
|
|
|
local tonumber = tonumber
|
2015-07-02 12:06:07 +02:00
|
|
|
|
|
|
|
-- OpenWeatherMap
|
|
|
|
-- current weather and X-days forecast
|
2017-02-08 14:15:48 +01:00
|
|
|
-- lain.widget.weather
|
2015-07-02 12:06:07 +02:00
|
|
|
|
2017-02-08 20:45:11 +01:00
|
|
|
local function factory(args)
|
2020-11-29 22:48:32 +01:00
|
|
|
args = args or {}
|
|
|
|
|
2020-10-31 18:11:29 +01:00
|
|
|
local weather = { widget = args.widget or wibox.widget.textbox() }
|
2022-12-27 00:38:35 +01:00
|
|
|
local APPID = args.APPID -- mandatory api key
|
2023-01-06 13:54:14 +01:00
|
|
|
local timeout = args.timeout or 900 -- 15 min
|
2022-12-27 00:38:35 +01:00
|
|
|
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'"
|
|
|
|
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'"
|
|
|
|
local lat = args.lat or 0 -- placeholder
|
|
|
|
local lon = args.lon or 0 -- placeholder
|
2015-08-29 15:03:24 +02:00
|
|
|
local units = args.units or "metric"
|
|
|
|
local lang = args.lang or "en"
|
2016-03-30 14:11:33 +02:00
|
|
|
local cnt = args.cnt or 5
|
2017-01-25 17:13:14 +01:00
|
|
|
local icons_path = args.icons_path or helpers.icons_dir .. "openweathermap/"
|
2015-08-29 15:03:24 +02:00
|
|
|
local notification_preset = args.notification_preset or {}
|
2015-08-29 19:46:18 +02:00
|
|
|
local notification_text_fun = args.notification_text_fun or
|
2016-03-30 13:04:26 +02:00
|
|
|
function (wn)
|
2017-01-20 20:58:22 +01:00
|
|
|
local day = os.date("%a %d", wn["dt"])
|
2021-03-29 23:23:56 +02:00
|
|
|
local temp = math.floor(wn["main"]["temp"])
|
2016-03-30 13:04:26 +02:00
|
|
|
local desc = wn["weather"][1]["description"]
|
2021-03-29 23:23:56 +02:00
|
|
|
return string.format("<b>%s</b>: %s, %d ", day, desc, temp)
|
2015-08-29 15:03:24 +02:00
|
|
|
end
|
2015-11-02 22:50:32 +01:00
|
|
|
local weather_na_markup = args.weather_na_markup or " N/A "
|
2017-01-20 20:58:22 +01:00
|
|
|
local followtag = args.followtag or false
|
2017-07-25 17:21:36 +02:00
|
|
|
local showpopup = args.showpopup or "on"
|
2015-08-29 15:03:24 +02:00
|
|
|
local settings = args.settings or function() end
|
2015-07-02 12:06:07 +02:00
|
|
|
|
2017-01-25 17:13:14 +01:00
|
|
|
weather.widget:set_markup(weather_na_markup)
|
2015-11-02 22:50:32 +01:00
|
|
|
weather.icon_path = icons_path .. "na.png"
|
2017-01-25 17:13:14 +01:00
|
|
|
weather.icon = wibox.widget.imagebox(weather.icon_path)
|
2015-07-02 12:06:07 +02:00
|
|
|
|
2019-04-08 12:37:38 +02:00
|
|
|
function weather.show(seconds)
|
2015-07-02 12:06:07 +02:00
|
|
|
weather.hide()
|
2015-07-29 13:21:59 +02:00
|
|
|
|
2017-01-06 11:21:19 +01:00
|
|
|
if followtag then
|
|
|
|
notification_preset.screen = focused()
|
2015-07-29 13:21:59 +02:00
|
|
|
end
|
|
|
|
|
2016-03-30 16:34:41 +02:00
|
|
|
if not weather.notification_text then
|
2017-02-03 21:00:00 +01:00
|
|
|
weather.update()
|
2016-03-30 16:34:41 +02:00
|
|
|
weather.forecast_update()
|
|
|
|
end
|
|
|
|
|
2019-04-08 12:37:38 +02:00
|
|
|
weather.notification = naughty.notify {
|
|
|
|
preset = notification_preset,
|
2016-03-30 13:04:26 +02:00
|
|
|
text = weather.notification_text,
|
2015-07-02 12:06:07 +02:00
|
|
|
icon = weather.icon_path,
|
2020-12-16 11:40:26 +01:00
|
|
|
timeout = type(seconds) == "number" and seconds or notification_preset.timeout
|
2019-04-08 12:37:38 +02:00
|
|
|
}
|
2015-07-02 12:06:07 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function weather.hide()
|
2016-03-30 13:04:26 +02:00
|
|
|
if weather.notification then
|
2015-07-02 12:06:07 +02:00
|
|
|
naughty.destroy(weather.notification)
|
|
|
|
weather.notification = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function weather.attach(obj)
|
|
|
|
obj:connect_signal("mouse::enter", function()
|
|
|
|
weather.show(0)
|
|
|
|
end)
|
|
|
|
obj:connect_signal("mouse::leave", function()
|
|
|
|
weather.hide()
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
function weather.forecast_update()
|
2022-12-27 00:38:35 +01:00
|
|
|
local cmd = string.format(forecast_call, lat, lon, APPID, cnt, units, lang)
|
2023-01-06 13:54:14 +01:00
|
|
|
|
2017-01-25 17:13:14 +01:00
|
|
|
helpers.async(cmd, function(f)
|
2020-11-29 22:48:32 +01:00
|
|
|
local err
|
|
|
|
weather_now, _, err = json.decode(f, 1, nil)
|
2015-07-02 12:06:07 +02:00
|
|
|
|
2016-10-12 19:14:48 +02:00
|
|
|
if not err and type(weather_now) == "table" and tonumber(weather_now["cod"]) == 200 then
|
2017-08-24 12:49:17 +02:00
|
|
|
weather.notification_text = ""
|
2022-10-20 21:38:51 +02:00
|
|
|
for i = 1, weather_now["cnt"], math.floor(weather_now["cnt"] / cnt) do
|
2015-07-02 12:06:07 +02:00
|
|
|
weather.notification_text = weather.notification_text ..
|
2016-03-30 13:04:26 +02:00
|
|
|
notification_text_fun(weather_now["list"][i])
|
2015-07-02 12:06:07 +02:00
|
|
|
if i < weather_now["cnt"] then
|
|
|
|
weather.notification_text = weather.notification_text .. "\n"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
function weather.update()
|
2022-12-27 00:38:35 +01:00
|
|
|
local cmd = string.format(current_call, lat, lon, APPID, units, lang)
|
|
|
|
|
2017-01-25 17:13:14 +01:00
|
|
|
helpers.async(cmd, function(f)
|
2020-11-29 22:48:32 +01:00
|
|
|
local err
|
|
|
|
weather_now, _, err = json.decode(f, 1, nil)
|
2015-07-02 12:06:07 +02:00
|
|
|
|
2016-10-12 19:14:48 +02:00
|
|
|
if not err and type(weather_now) == "table" and tonumber(weather_now["cod"]) == 200 then
|
2016-05-31 10:00:28 +02:00
|
|
|
local sunrise = tonumber(weather_now["sys"]["sunrise"])
|
|
|
|
local sunset = tonumber(weather_now["sys"]["sunset"])
|
|
|
|
local icon = weather_now["weather"][1]["icon"]
|
2019-05-13 01:08:46 +02:00
|
|
|
local loc_now = os.time()
|
2016-05-29 17:13:52 +02:00
|
|
|
|
2017-09-06 03:28:18 +02:00
|
|
|
if sunrise <= loc_now and loc_now <= sunset then
|
2016-05-29 17:13:52 +02:00
|
|
|
icon = string.gsub(icon, "n", "d")
|
|
|
|
else
|
|
|
|
icon = string.gsub(icon, "d", "n")
|
|
|
|
end
|
|
|
|
|
|
|
|
weather.icon_path = icons_path .. icon .. ".png"
|
2015-07-02 12:06:07 +02:00
|
|
|
widget = weather.widget
|
|
|
|
settings()
|
2015-07-05 11:30:24 +02:00
|
|
|
else
|
2015-11-02 22:50:32 +01:00
|
|
|
weather.icon_path = icons_path .. "na.png"
|
|
|
|
weather.widget:set_markup(weather_na_markup)
|
2015-07-02 12:06:07 +02:00
|
|
|
end
|
2015-11-02 22:50:32 +01:00
|
|
|
|
|
|
|
weather.icon:set_image(weather.icon_path)
|
2015-07-02 12:06:07 +02:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2017-07-25 17:21:36 +02:00
|
|
|
if showpopup == "on" then weather.attach(weather.widget) end
|
2015-07-05 11:12:41 +02:00
|
|
|
|
2022-12-27 00:38:35 +01:00
|
|
|
weather.timer = helpers.newtimer("weather-" .. lat .. ":" .. lon, timeout, weather.update, false, true)
|
|
|
|
weather.timer_forecast = helpers.newtimer("weather_forecast-" .. lat .. ":" .. lon, timeout, weather.forecast_update, false, true)
|
2015-07-02 12:06:07 +02:00
|
|
|
|
2017-01-25 17:13:14 +01:00
|
|
|
return weather
|
2015-07-02 12:06:07 +02:00
|
|
|
end
|
|
|
|
|
2017-02-08 20:45:11 +01:00
|
|
|
return factory
|