Improve weather and corona daemons

They now remove the temp_file when the output of the command is not
valid in order to force an update the next time a check is made.
This commit is contained in:
elenapan 2020-04-01 14:37:22 +03:00
parent 0c156c605f
commit f4b18c6f82
3 changed files with 15 additions and 4 deletions

View file

@ -10,6 +10,7 @@ local naughty = require("naughty")
local update_interval = 60 * 60 * 12 -- 12 hours
local country = user.coronavirus_country or "usa"
local temp_file = "/tmp/awesomewm-evil-coronavirus"
local coronavirus_script = [[
sh -c '
@ -25,11 +26,18 @@ local coronavirus_script = [[
echo CTOTAL@$cases_total@CTODAY@$cases_today@DTOTAL@$deaths_total@DTODAY@$deaths_today@
']]
helpers.remote_watch(coronavirus_script, update_interval, "/tmp/awesomewm-evil-coronavirus", function(stdout)
helpers.remote_watch(coronavirus_script, update_interval, temp_file, function(stdout)
local cases_total = stdout:match('^CTOTAL@(.*)@CTODAY')
local cases_today = stdout:match('CTODAY@(.*)@DTOTAL')
local deaths_total = stdout:match('DTOTAL@(.*)@DTODAY')
local deaths_today = stdout:match('DTODAY@(.*)@')
awesome.emit_signal("evil::coronavirus", cases_total, cases_today, deaths_total, deaths_today)
-- If it is found, we assume the command succeeded
if cases_total then
awesome.emit_signal("evil::coronavirus", cases_total, cases_today, deaths_total, deaths_today)
else
-- Remove temp_file to force an update the next time
awful.spawn.with_shell("rm "..temp_file)
awesome.emit_signal("evil::coronavirus", -1, -1, -1, -1)
end
end)

View file

@ -12,6 +12,7 @@ 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 temp_file = "/tmp/awesomewm-evil-weather"
local weather_details_script = [[
bash -c '
@ -32,7 +33,7 @@ local weather_details_script = [[
fi
']]
helpers.remote_watch(weather_details_script, update_interval, "/tmp/awesomewm-evil-weather", function(stdout)
helpers.remote_watch(weather_details_script, update_interval, temp_file, function(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')
@ -43,6 +44,8 @@ helpers.remote_watch(weather_details_script, update_interval, "/tmp/awesomewm-ev
local description = weather_details:match('(.*)@@')
local temperature = weather_details:match('@@(.*)')
if icon_code == "..." then
-- Remove temp_file to force an update the next time
awful.spawn.with_shell("rm "..temp_file)
awesome.emit_signal("evil::weather", 999, "Weather unavailable", "")
else
awesome.emit_signal("evil::weather", tonumber(temperature), description, icon_code)

View file

@ -130,7 +130,7 @@ user = {
-- >> Coronavirus <<
-- Country to check for corona statistics
-- Uses the https://corona-stats.online API
coronavirus_country = "usa",
coronavirus_country = "germany",
}
-- ===================================================================