awesome-wm-widgets/weather-widget/weather.lua

142 lines
4.4 KiB
Lua
Raw Normal View History

2018-01-21 05:16:38 +01:00
-------------------------------------------------
-- Weather Widget based on the OpenWeatherMap
-- https://openweathermap.org/
--
-- @author Pavel Makhov
-- @copyright 2018 Pavel Makhov
-------------------------------------------------
2017-02-08 02:31:34 +01:00
local http = require("socket.http")
local json = require("json")
local naughty = require("naughty")
2018-01-21 05:17:25 +01:00
local wibox = require("wibox")
local gears = require("gears")
local secrets = require("awesome-wm-widgets.secrets")
2017-02-08 02:31:34 +01:00
local path_to_icons = "/usr/share/icons/Arc/status/symbolic/"
local icon_widget = wibox.widget {
{
id = "icon",
resize = false,
widget = wibox.widget.imagebox,
},
2018-10-08 17:48:48 +02:00
layout = wibox.container.margin(_ , 0, 0, 3),
2017-02-08 02:31:34 +01:00
set_image = function(self, path)
self.icon.image = path
end,
}
local temp_widget = wibox.widget{
font = "Play 9",
widget = wibox.widget.textbox,
}
2018-01-21 05:16:38 +01:00
local weather_widget = wibox.widget {
2017-02-08 02:31:34 +01:00
icon_widget,
temp_widget,
layout = wibox.layout.fixed.horizontal,
}
2018-10-08 17:48:48 +02:00
--- Maps openWeatherMap icons to Arc icons
2017-02-08 02:31:34 +01:00
local icon_map = {
["01d"] = "weather-clear-symbolic.svg",
["02d"] = "weather-few-clouds-symbolic.svg",
["03d"] = "weather-clouds-symbolic.svg",
["04d"] = "weather-overcast-symbolic.svg",
["09d"] = "weather-showers-scattered-symbolic.svg",
["10d"] = "weather-showers-symbolic.svg",
["11d"] = "weather-storm-symbolic.svg",
["13d"] = "weather-snow-symbolic.svg",
["50d"] = "weather-fog-symbolic.svg",
["01n"] = "weather-clear-night-symbolic.svg",
["02n"] = "weather-few-clouds-night-symbolic.svg",
["03n"] = "weather-clouds-night-symbolic.svg",
["04n"] = "weather-overcast-symbolic.svg",
["09n"] = "weather-showers-scattered-symbolic.svg",
["10n"] = "weather-showers-symbolic.svg",
["11n"] = "weather-storm-symbolic.svg",
["13n"] = "weather-snow-symbolic.svg",
["50n"] = "weather-fog-symbolic.svg"
}
2018-10-08 17:48:48 +02:00
--- Return wind direction as a string.
local function to_direction(degrees)
-- Ref: https://www.campbellsci.eu/blog/convert-wind-directions
if degrees == nil then
return "Unknown dir"
end
local directions = {
"N",
"NNE",
"NE",
"ENE",
"E",
"ESE",
"SE",
"SSE",
"S",
"SSW",
"SW",
"WSW",
"W",
"WNW",
"NW",
"NNW",
"N",
}
return directions[math.floor((degrees % 360) / 22.5) + 1]
2018-01-14 01:41:54 +01:00
end
local weather_timer = gears.timer({ timeout = 60 })
2017-02-08 02:31:34 +01:00
local resp
weather_timer:connect_signal("timeout", function ()
local resp_json, status = http.request('https://api.openweathermap.org/data/2.5/weather?q='
.. secrets.weather_widget_city
.. '&appid=' .. secrets.weather_widget_api_key
.. '&units=' .. secrets.weather_widget_units)
2019-01-27 04:54:49 +01:00
if (status ~= 200) then
local err_resp = json.decode(resp_json)
naughty.notify{
title = 'Weather Widget Error',
text = err_resp.message,
preset = naughty.config.presets.critical,
}
elseif (resp_json ~= nil) then
resp = json.decode(resp_json)
icon_widget.image = path_to_icons .. icon_map[resp.weather[1].icon]
temp_widget:set_text(string.gsub(resp.main.temp, "%.%d+", "")
.. '°'
.. (secrets.weather_widget_units == 'metric' and 'C' or 'F'))
end
2017-02-08 02:31:34 +01:00
end)
2017-03-03 19:33:27 +01:00
weather_timer:start()
2017-02-08 02:31:34 +01:00
weather_timer:emit_signal("timeout")
2018-10-08 17:48:48 +02:00
--- Notification with weather information. Popups when mouse hovers over the icon
local notification
2017-02-08 02:31:34 +01:00
weather_widget:connect_signal("mouse::enter", function()
2017-03-15 02:53:18 +01:00
notification = naughty.notify{
2017-02-08 02:31:34 +01:00
icon = path_to_icons .. icon_map[resp.weather[1].icon],
icon_size=20,
text =
2018-03-21 19:18:11 +01:00
'<big>' .. resp.weather[1].main .. ' (' .. resp.weather[1].description .. ')</big><br>' ..
'<b>Humidity:</b> ' .. resp.main.humidity .. '%<br>' ..
'<b>Temperature:</b> ' .. resp.main.temp .. '°'
.. (secrets.weather_widget_units == 'metric' and 'C' or 'F') .. '<br>' ..
'<b>Pressure:</b> ' .. resp.main.pressure .. 'hPa<br>' ..
'<b>Clouds:</b> ' .. resp.clouds.all .. '%<br>' ..
'<b>Wind:</b> ' .. resp.wind.speed .. 'm/s (' .. to_direction(resp.wind.deg) .. ')',
2017-03-15 02:53:18 +01:00
timeout = 5, hover_timeout = 10,
width = 200
2017-02-08 02:31:34 +01:00
}
2017-02-08 14:56:51 +01:00
end)
2018-10-08 17:48:48 +02:00
weather_widget:connect_signal("mouse::leave", function()
naughty.destroy(notification)
2018-01-14 01:41:54 +01:00
end)
2018-01-21 05:16:38 +01:00
return weather_widget