luca-awesome-dotfiles/home/.config/awesome/ui/sidebar/weather.lua
Hydroxycarbamide 2401cfcee2 Add revelation
Update and add bling task_preview, tag_preview and window_switcher
Add catppuccin-macchiato
Use my weather key
Update keybindings
Update autorun
Fix sliders
2023-03-20 09:51:48 +01:00

221 lines
5.7 KiB
Lua

-- Requirements :
-- ~~~~~~~~~~~~~~
local awful = require("awful")
local wibox = require("wibox")
local gears = require("gears")
local beautiful = require("beautiful")
local dpi = beautiful.xresources.apply_dpi
local filesystem = gears.filesystem
local icon_dir = filesystem.get_configuration_dir() .. "themes/icons/weather/"
local naughty = require("naughty")
-- # Libs :
-- ~~~~~~~~
local json = require("libs.json")
--- Weather Widget
--- ~~~~~~~~~~~~~~
local GET_FORECAST_CMD = [[bash -c "curl -s --show-error -X GET '%s'"]]
local icon_map = {
["01d"] = "weather-clear-sky",
["02d"] = "weather-few-clouds",
["03d"] = "weather-clouds",
["04d"] = "weather-few-clouds",
["09d"] = "weather-showers-scattered",
["10d"] = "weather-showers",
["11d"] = "weather-strom",
["13d"] = "weather-snow",
["50d"] = "weather-fog",
["01n"] = "weather-clear-night",
["02n"] = "weather-few-clouds-night",
["03n"] = "weather-clouds-night",
["04n"] = "weather-clouds-night",
["09n"] = "weather-showers-scattered",
["10n"] = "weather-showers",
["11n"] = "weather-strom",
["13n"] = "weather-snow",
["50n"] = "weather-fog",
}
local current_weather_widget = wibox.widget({
{
{
id = "icon",
image = icon_dir .. "weather-showers.svg",
resize = true,
forced_height = dpi(42),
forced_width = dpi(42),
widget = wibox.widget.imagebox,
},
{
{
{
id = "description",
text = "Mostly cloudy",
font = "Roboto Medium 10",
widget = wibox.widget.textbox,
},
{
id = "humidity",
text = "Humidity: 80%",
font = "Roboto Medium 10",
widget = wibox.widget.textbox,
},
layout = wibox.layout.fixed.vertical,
},
widget = wibox.container.place,
},
spacing = dpi(10),
layout = wibox.layout.fixed.horizontal,
},
nil,
{
{
{
id = "tempareture_current",
markup = "20<sup><span>°</span></sup>",
align = "right",
font = "Roboto Medium 14",
widget = wibox.widget.textbox,
},
{
id = "feels_like",
markup = "Feels like: 19<sup><span>°C</span></sup>",
font = "Roboto Medium 10",
widget = wibox.widget.textbox,
},
spacing = dpi( -6),
layout = wibox.layout.fixed.vertical,
},
widget = wibox.container.place,
},
layout = wibox.layout.align.horizontal,
})
local hourly_widget = function()
local widget = wibox.widget({
{
{
id = "time",
text = "12PM",
font = "Roboto Medium 10",
widget = wibox.widget.textbox,
},
widget = wibox.container.place,
},
{
{
id = "icon",
image = icon_dir .. "weather-clear-sky.svg",
resize = true,
forced_height = dpi(16),
forced_width = dpi(16),
widget = wibox.widget.imagebox,
},
widget = wibox.container.place,
},
{
{
id = "tempareture",
markup = "1<sup><span>°</span></sup>",
font = theme.font,
widget = wibox.widget.textbox,
},
widget = wibox.container.place,
},
spacing = dpi(6),
layout = wibox.layout.fixed.vertical,
})
widget.update = function(result)
local time = widget:get_children_by_id("time")[1]
local icon = widget:get_children_by_id("icon")[1]
local temp = widget:get_children_by_id("tempareture")[1]
temp:set_markup(math.floor(result.main.temp) .. "<sup><span>°</span></sup>")
time:set_text(os.date("%H", tonumber(result.dt)))
icon.image = icon_dir .. icon_map[result.weather[1].icon] .. ".svg"
icon:emit_signal("widget::redraw_needed")
end
return widget
end
local hourly_widget_1 = hourly_widget()
local hourly_widget_2 = hourly_widget()
local hourly_widget_3 = hourly_widget()
local hourly_widget_4 = hourly_widget()
local hourly_widget_5 = hourly_widget()
local hourly_widget_6 = hourly_widget()
local weather_widget = wibox.widget({
{
text = "Weather",
font = theme.font,
align = "center",
widget = wibox.widget.textbox,
},
current_weather_widget,
{
hourly_widget_1,
hourly_widget_2,
hourly_widget_3,
hourly_widget_4,
hourly_widget_5,
hourly_widget_6,
spacing = dpi(10),
layout = wibox.layout.flex.horizontal,
},
spacing = dpi(10),
layout = wibox.layout.fixed.vertical,
})
local api_key = "7641c17cda8ed75684ed55704226c565"
local city_id = "2972315"
local units = "metric"
local url = (
"https://api.openweathermap.org/data/2.5/forecast"
.. "?appid="
.. api_key
.. "&id="
.. city_id
.. "&units="
.. units
)
awful.widget.watch(string.format(GET_FORECAST_CMD, url), 600, function(_, stdout, stderr)
if stderr == "" then
local result = json.decode(stdout)
-- Current weather setup
local icon = current_weather_widget:get_children_by_id("icon")[1]
local description = current_weather_widget:get_children_by_id("description")[1]
local humidity = current_weather_widget:get_children_by_id("humidity")[1]
local temp_current = current_weather_widget:get_children_by_id("tempareture_current")[1]
local feels_like = current_weather_widget:get_children_by_id("feels_like")[1]
local current = result.list[1]
icon.image = icon_dir .. icon_map[current.weather[1].icon] .. ".svg"
icon:emit_signal("widget::redraw_needed")
description:set_text(current.weather[1].description:gsub("^%l", string.upper))
humidity:set_text("Humidity: " .. current.main.humidity .. "%")
temp_current:set_markup(math.floor(current.main.temp) .. "<sup><span>°</span></sup>")
feels_like:set_markup("Feels like: " .. math.floor(current.main.feels_like) .. "<sup><span>°</span></sup>")
-- Hourly widget setup
hourly_widget_1.update(result.list[1])
hourly_widget_2.update(result.list[2])
hourly_widget_3.update(result.list[3])
hourly_widget_4.update(result.list[4])
hourly_widget_5.update(result.list[5])
hourly_widget_6.update(result.list[6])
else
naughty.notify({
title = "Weather error",
text = stderr
})
end
end)
return weather_widget