1
1
Fork 0
mirror of https://github.com/vicious-widgets/vicious synced 2023-12-14 07:13:07 +01:00

[contrib.openweather] added app id to warg; added {temp min c}, {temp max c}, {sunrise} and {sunset}

This commit is contained in:
Marcel Arpogaus 2020-09-26 17:13:04 +02:00
parent 82cffa8ea9
commit 8df7f42164

View file

@ -16,24 +16,18 @@
--
-- You should have received a copy of the GNU General Public License
-- along with Vicious. If not, see <https://www.gnu.org/licenses/>.
-- {{{ Grab environment
local tonumber = tonumber
local io = {popen = io.popen}
local setmetatable = setmetatable
local string = {match = string.match}
local math = {
ceil = math.ceil,
floor = math.floor
}
local math = {ceil = math.ceil, floor = math.floor}
-- }}}
-- Openweather: provides weather information for a requested station
-- vicious.widgets.openweather
local openweather_all = {}
-- Initialize function tables
local _wdirs = {"N", "NE", "E", "SE", "S", "SW", "W", "NW", "N"}
local _wdata = {
@ -45,6 +39,10 @@ local _wdata = {
["{sky}"] = "N/A",
["{weather}"] = "N/A",
["{temp c}"] = "N/A",
["{temp min c}"] = "N/A",
["{temp max c}"] = "N/A",
["{sunrise}"] = -1,
["{sunset}"] = -1,
["{humid}"] = "N/A",
["{press}"] = "N/A"
}
@ -55,8 +53,11 @@ local function worker(format, warg)
-- Get weather forceast using the city ID code, from:
-- * OpenWeatherMap.org
local openweather = "http://api.openweathermap.org/data/2.5/weather?id="..warg.."&mode=json&units=metric"
local f = io.popen("curl --connect-timeout 1 -fsm 3 '"..openweather.."'")
local openweather = "http://api.openweathermap.org/data/2.5/weather?id=" ..
warg.city_id .. "&appid=" .. warg.app_id ..
"&mode=json&units=metric"
local f =
io.popen("curl --connect-timeout 1 -fsm 3 '" .. openweather .. "'")
local ws = f:read("*all")
f:close()
@ -75,8 +76,16 @@ local function worker(format, warg)
string.match(ws, '"description":"([%a%s]+)"') or _wdata["{weather}"]
_wdata["{temp c}"] = -- Temperature in celsius
string.match(ws, '"temp":([%-]?[%d%.]+)') or _wdata["{temp c}"]
_wdata["{temp min c}"] = -- Minimal Temperature in celsius
string.match(ws, '"temp_min":([%-]?[%d%.]+)') or _wdata["{temp min c}"]
_wdata["{temp max c}"] = -- Maximal Temperature in celsius
string.match(ws, '"temp_max":([%-]?[%d%.]+)') or _wdata["{temp max c}"]
_wdata["{humid}"] = -- Relative humidity in percent
string.match(ws, '"humidity":([%d]+)') or _wdata["{humid}"]
_wdata["{sunrise}"] = -- Sunrise
tonumber(string.match(ws, '"sunrise":([%d]+)')) or _wdata["{sunrise}"]
_wdata["{sunset}"] = -- Sunset
tonumber(string.match(ws, '"sunset":([%d]+)')) or _wdata["{sunset}"]
_wdata["{press}"] = -- Pressure in hPa
string.match(ws, '"pressure":([%d%.]+)') or _wdata["{press}"]
@ -95,9 +104,10 @@ local function worker(format, warg)
if (_wdata["{wind deg}"] / 45) % 1 == 0 then
_wdata["{wind aim}"] = _wdirs[_wdata["{wind deg}"] / 45 + 1]
else
_wdata["{wind aim}"] =
_wdirs[math.ceil(_wdata["{wind deg}"] / 45) + 1]..
_wdirs[math.floor(_wdata["{wind deg}"] / 45) + 1]
_wdata["{wind aim}"] = _wdirs[math.ceil(_wdata["{wind deg}"] / 45) +
1] ..
_wdirs[math.floor(
_wdata["{wind deg}"] / 45) + 1]
end
end
@ -105,4 +115,5 @@ local function worker(format, warg)
end
-- }}}
return setmetatable(openweather_all, { __call = function(_, ...) return worker(...) end })
return setmetatable(openweather_all,
{__call = function(_, ...) return worker(...) end})