summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPavel Makhov <pmakhov@touchtunes.com>2019-01-29 14:59:09 -0500
committerPavel Makhov <pmakhov@touchtunes.com>2019-01-29 14:59:09 -0500
commitd198207ee2154258c4d24db4ed7dd0f66f711ce4 (patch)
treeb7b05d372174d520fde279337bf43ff03e9992d5
parentbf96cfa3131cb5e3c762f5274663b83557dab981 (diff)
Make temperature units customizable using the secrets.lua
-rw-r--r--secrets.lua3
-rw-r--r--weather-widget/weather.lua25
2 files changed, 15 insertions, 13 deletions
diff --git a/secrets.lua b/secrets.lua
index 98051c7..9e3dace 100644
--- a/secrets.lua
+++ b/secrets.lua
@@ -11,7 +11,8 @@ local secrets = {
-- OpenWeatherMap API key - https://openweathermap.org/appid
weather_widget_api_key = 'API_KEY',
- weather_widget_city = 'Montreal,ca'
+ weather_widget_city = 'Montreal,ca',
+ weather_widget_units = 'metric' -- for celsius, or 'imperial' for fahrenheit
}
return secrets
diff --git a/weather-widget/weather.lua b/weather-widget/weather.lua
index 06897b7..484c2c6 100644
--- a/weather-widget/weather.lua
+++ b/weather-widget/weather.lua
@@ -61,13 +61,8 @@ local icon_map = {
["50n"] = "weather-fog-symbolic.svg"
}
---- handy function to convert temperature from Kelvin to Celcius
-function to_celcius(kelvin)
- return math.floor(tonumber(kelvin) - 273.15)
-end
-
--- Return wind direction as a string.
-function to_direction(degrees)
+local function to_direction(degrees)
-- Ref: https://www.campbellsci.eu/blog/convert-wind-directions
if degrees == nil then
return "Unknown dir"
@@ -98,7 +93,10 @@ local weather_timer = gears.timer({ timeout = 60 })
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)
+ 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)
if (status ~= 200) then
local err_resp = json.decode(resp_json)
naughty.notify{
@@ -109,7 +107,9 @@ weather_timer:connect_signal("timeout", function ()
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(to_celcius(resp.main.temp) .. "°C")
+ temp_widget:set_text(string.gsub(resp.main.temp, "%.%d+", "")
+ .. '°'
+ .. (secrets.weather_widget_units == 'metric' and 'C' or 'F'))
end
end)
weather_timer:start()
@@ -124,10 +124,11 @@ weather_widget:connect_signal("mouse::enter", function()
text =
'<big>' .. resp.weather[1].main .. ' (' .. resp.weather[1].description .. ')</big><br>' ..
'<b>Humidity:</b> ' .. resp.main.humidity .. '%<br>' ..
- '<b>Temperature: </b>' .. to_celcius(resp.main.temp) .. '<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) .. ')',
+ '<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) .. ')',
timeout = 5, hover_timeout = 10,
width = 200
}