elenapan/config/awesome/evil/weather.lua
elenapan c738169ca5 New theme: ephemeral, anti-aliased corners, app drawer, lock screen,
evil daemon system, dependency list update, README improvements.


Former-commit-id: db310f8e49
Former-commit-id: 4f25f9200c3ac9f9385daca5a68378249ff0329e
Former-commit-id: 69fa3954e5738446a59b409b7e326233e7c3ef55
Former-commit-id: 673176f7857e39f3455f4ccb426eef2789c0e891
2019-08-01 02:17:59 +03:00

50 lines
1.8 KiB
Lua

-- Provides:
-- evil::weather
-- temperature (integer)
-- description (string)
-- icon_code (string)
local awful = require("awful")
-- Configuration
local key = user.openweathermap_key
local city_id = user.openweathermap_city_id
local units = user.weather_units
-- Don't update too often, because your requests might get blocked for 24 hours
local update_interval = 1200
local weather_details_script = [[
bash -c '
KEY="]]..key..[["
CITY="]]..city_id..[["
UNITS="]]..units..[["
weather=$(curl -sf "http://api.openweathermap.org/data/2.5/weather?APPID=$KEY&id=$CITY&units=$UNITS")
if [ ! -z "$weather" ]; then
weather_temp=$(echo "$weather" | jq ".main.temp" | cut -d "." -f 1)
weather_icon=$(echo "$weather" | jq -r ".weather[].icon" | head -1)
weather_description=$(echo "$weather" | jq -r ".weather[].description" | head -1)
echo "$weather_icon" "$weather_description"@@"$weather_temp"
else
echo "..."
fi
']]
-- Periodically get weather info
awful.widget.watch(weather_details_script, update_interval, function(widget, stdout)
local icon_code = string.sub(stdout, 1, 3)
local weather_details = string.sub(stdout, 5)
weather_details = string.gsub(weather_details, '^%s*(.-)%s*$', '%1')
-- Replace "-0" with "0" degrees
weather_details = string.gsub(weather_details, '%-0', '0')
-- Capitalize first letter of the description
weather_details = weather_details:sub(1,1):upper()..weather_details:sub(2)
local description = weather_details:match('(.*)@@')
local temperature = weather_details:match('@@(.*)')
if icon_code == "..." then
awesome.emit_signal("evil::weather", 999, "Weather unavailable", "")
else
awesome.emit_signal("evil::weather", tonumber(temperature), description, icon_code)
end
end)