[contrib.openweather] Support API ID and additional return values
This commit is contained in:
parent
4ae9245246
commit
edfd51215e
|
@ -1,6 +1,7 @@
|
||||||
-- contrib/openweather_all.lua
|
-- contrib/openweather_all.lua
|
||||||
-- Copyright (C) 2013 NormalRa <normalrawr gmail com>
|
-- Copyright (C) 2013 NormalRa <normalrawr gmail com>
|
||||||
-- Copyright (C) 2017 Jörg Thalheim <joerg@higgsboson.tk>
|
-- Copyright (C) 2017 Jörg Thalheim <joerg@higgsboson.tk>
|
||||||
|
-- Copyright (C) 2020 Marcel Arpogaus <marcel.arpogaus gmail com>
|
||||||
--
|
--
|
||||||
-- This file is part of Vicious.
|
-- This file is part of Vicious.
|
||||||
--
|
--
|
||||||
|
@ -16,69 +17,66 @@
|
||||||
--
|
--
|
||||||
-- You should have received a copy of the GNU General Public License
|
-- You should have received a copy of the GNU General Public License
|
||||||
-- along with Vicious. If not, see <https://www.gnu.org/licenses/>.
|
-- along with Vicious. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
-- {{{ Grab environment
|
-- {{{ Grab environment
|
||||||
local tonumber = tonumber
|
local tonumber = tonumber
|
||||||
local io = { popen = io.popen }
|
local string = {match = string.match}
|
||||||
local setmetatable = setmetatable
|
local math = {ceil = math.ceil, floor = math.floor}
|
||||||
local string = { match = string.match }
|
local helpers = require "vicious.helpers"
|
||||||
local math = {
|
local spawn = require "vicious.spawn"
|
||||||
ceil = math.ceil,
|
|
||||||
floor = math.floor
|
|
||||||
}
|
|
||||||
-- }}}
|
-- }}}
|
||||||
|
|
||||||
|
|
||||||
-- Openweather: provides weather information for a requested station
|
-- Openweather: provides weather information for a requested station
|
||||||
-- vicious.widgets.openweather
|
-- vicious.widgets.openweather
|
||||||
local openweather_all = {}
|
local openweather_all = {}
|
||||||
|
|
||||||
|
|
||||||
-- Initialize function tables
|
-- Initialize function tables
|
||||||
local _wdirs = { "N", "NE", "E", "SE", "S", "SW", "W", "NW", "N" }
|
local _wdirs = {"N", "NE", "E", "SE", "S", "SW", "W", "NW", "N"}
|
||||||
local _wdata = {
|
local _wdata = {
|
||||||
["{city}"] = "N/A",
|
["{city}"] = "N/A",
|
||||||
["{wind deg}"] = "N/A",
|
["{wind deg}"] = "N/A",
|
||||||
["{wind aim}"] = "N/A",
|
["{wind aim}"] = "N/A",
|
||||||
["{wind mps}"] = "N/A",
|
["{wind mps}"] = "N/A",
|
||||||
["{wind kmh}"] = "N/A",
|
["{wind kmh}"] = "N/A",
|
||||||
["{sky}"] = "N/A",
|
["{sky}"] = "N/A",
|
||||||
["{weather}"] = "N/A",
|
["{weather}"] = "N/A",
|
||||||
["{temp c}"] = "N/A",
|
["{temp c}"] = "N/A",
|
||||||
["{humid}"] = "N/A",
|
["{temp min c}"] = "N/A",
|
||||||
["{press}"] = "N/A"
|
["{temp max c}"] = "N/A",
|
||||||
|
["{sunrise}"] = -1,
|
||||||
|
["{sunset}"] = -1,
|
||||||
|
["{humid}"] = "N/A",
|
||||||
|
["{press}"] = "N/A"
|
||||||
}
|
}
|
||||||
|
|
||||||
-- {{{ Openweather widget type
|
-- {{{ Openweather widget type
|
||||||
local function worker(format, warg)
|
local function parse(stdout, stderr, exitreason, exitcode)
|
||||||
if not warg then return end
|
|
||||||
|
|
||||||
-- 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 ws = f:read("*all")
|
|
||||||
f:close()
|
|
||||||
|
|
||||||
-- Check if there was a timeout or a problem with the station
|
-- Check if there was a timeout or a problem with the station
|
||||||
if ws == nil then return _wdata end
|
if stdout == nil or exitcode ~= 0 then return _wdata end
|
||||||
|
|
||||||
_wdata["{city}"] = -- City name
|
_wdata["{city}"] = -- City name
|
||||||
string.match(ws, '"name":"([%a%s%-]+)"') or _wdata["{city}"]
|
string.match(stdout, '"name":"([%a%s%-]+)"') or _wdata["{city}"]
|
||||||
_wdata["{wind deg}"] = -- Wind degrees
|
_wdata["{wind deg}"] = -- Wind degrees
|
||||||
string.match(ws, '"deg":([%d]+)') or _wdata["{wind deg}"]
|
string.match(stdout, '"deg":([%d]+)') or _wdata["{wind deg}"]
|
||||||
_wdata["{wind mps}"] = -- Wind speed in meters per second
|
_wdata["{wind mps}"] = -- Wind speed in meters per second
|
||||||
string.match(ws, '"speed":([%d%.]+)') or _wdata["{wind mps}"]
|
string.match(stdout, '"speed":([%d%.]+)') or _wdata["{wind mps}"]
|
||||||
_wdata["{sky}"] = -- Sky conditions
|
_wdata["{sky}"] = -- Sky conditions
|
||||||
string.match(ws, '"main":"([%a]+)"') or _wdata["{sky}"]
|
string.match(stdout, '"main":"([%a]+)"') or _wdata["{sky}"]
|
||||||
_wdata["{weather}"] = -- Weather description
|
_wdata["{weather}"] = -- Weather description
|
||||||
string.match(ws, '"description":"([%a%s]+)"') or _wdata["{weather}"]
|
string.match(stdout, '"description":"([%a%s]+)"') or _wdata["{weather}"]
|
||||||
_wdata["{temp c}"] = -- Temperature in celsius
|
_wdata["{temp c}"] = -- Temperature in celsius
|
||||||
string.match(ws, '"temp":([%-]?[%d%.]+)') or _wdata["{temp c}"]
|
string.match(stdout, '"temp":([%-]?[%d%.]+)') or _wdata["{temp c}"]
|
||||||
_wdata["{humid}"] = -- Relative humidity in percent
|
_wdata["{temp min c}"] = -- Minimal Temperature in celsius
|
||||||
string.match(ws, '"humidity":([%d]+)') or _wdata["{humid}"]
|
string.match(stdout, '"temp_min":([%-]?[%d%.]+)') or _wdata["{temp min c}"]
|
||||||
_wdata["{press}"] = -- Pressure in hPa
|
_wdata["{temp max c}"] = -- Maximal Temperature in celsius
|
||||||
string.match(ws, '"pressure":([%d%.]+)') or _wdata["{press}"]
|
string.match(stdout, '"temp_max":([%-]?[%d%.]+)') or _wdata["{temp max c}"]
|
||||||
|
_wdata["{humid}"] = -- Relative humidity in percent
|
||||||
|
string.match(stdout, '"humidity":([%d]+)') or _wdata["{humid}"]
|
||||||
|
_wdata["{sunrise}"] = -- Sunrise
|
||||||
|
tonumber(string.match(stdout, '"sunrise":([%d]+)')) or _wdata["{sunrise}"]
|
||||||
|
_wdata["{sunset}"] = -- Sunset
|
||||||
|
tonumber(string.match(stdout, '"sunset":([%d]+)')) or _wdata["{sunset}"]
|
||||||
|
_wdata["{press}"] = -- Pressure in hPa
|
||||||
|
string.match(stdout, '"pressure":([%d%.]+)') or _wdata["{press}"]
|
||||||
|
|
||||||
-- Wind speed in km/h
|
-- Wind speed in km/h
|
||||||
if _wdata["{wind mps}"] ~= "N/A" then
|
if _wdata["{wind mps}"] ~= "N/A" then
|
||||||
|
@ -92,17 +90,32 @@ local function worker(format, warg)
|
||||||
_wdata["{wind deg}"] = tonumber(_wdata["{wind deg}"])
|
_wdata["{wind deg}"] = tonumber(_wdata["{wind deg}"])
|
||||||
|
|
||||||
-- Lua tables start at [1]
|
-- Lua tables start at [1]
|
||||||
if (_wdata["{wind deg}"] / 45)%1 == 0 then
|
if (_wdata["{wind deg}"] / 45) % 1 == 0 then
|
||||||
_wdata["{wind aim}"] = _wdirs[_wdata["{wind deg}"] / 45 + 1]
|
_wdata["{wind aim}"] = _wdirs[_wdata["{wind deg}"] / 45 + 1]
|
||||||
else
|
else
|
||||||
_wdata["{wind aim}"] =
|
_wdata["{wind aim}"] = _wdirs[math.ceil(_wdata["{wind deg}"] / 45) +
|
||||||
_wdirs[math.ceil(_wdata["{wind deg}"] / 45) + 1]..
|
1] ..
|
||||||
_wdirs[math.floor(_wdata["{wind deg}"] / 45) + 1]
|
_wdirs[math.floor(
|
||||||
|
_wdata["{wind deg}"] / 45) + 1]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return _wdata
|
return _wdata
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function openweather_all.async(format, warg, callback)
|
||||||
|
if not warg then return callback {} end
|
||||||
|
if type(warg) ~= "table" then return callback {} end
|
||||||
|
|
||||||
|
-- Get weather forceast using the city ID code, from:
|
||||||
|
-- * OpenWeatherMap.org
|
||||||
|
local openweather = "http://api.openweathermap.org/data/2.5/weather?id=" ..
|
||||||
|
warg.city_id .. "&appid=" .. warg.app_id ..
|
||||||
|
"&mode=json&units=metric"
|
||||||
|
|
||||||
|
spawn.easy_async("curl --connect-timeout 1 -fsm 3 '" .. openweather .. "'",
|
||||||
|
function(...) callback(parse(...)) end)
|
||||||
|
end
|
||||||
-- }}}
|
-- }}}
|
||||||
|
|
||||||
return setmetatable(openweather_all, { __call = function(_, ...) return worker(...) end })
|
return helpers.setasyncall(openweather_all)
|
||||||
|
|
|
@ -133,12 +133,15 @@ vicious.contrib.net
|
||||||
vicious.contrib.openweather
|
vicious.contrib.openweather
|
||||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
Provides weather information for a requested city
|
Provides weather information for a requested city from OpenWeatherMap (OWM)
|
||||||
|
|
||||||
* Argument: OpenWeatherMap city ID, e.g. ``"1275339"``
|
* Argument: a table containing the fields ``city_id`` with the OWM city ID, e.g.
|
||||||
* Returns a table with string keys: ``${city}``, ``${wind deg}``,
|
``"2643743"`` and ``app_id`` with the the OWM app ID, e.g
|
||||||
``${wind aim}``, ``${wind kmh}``, ``${wind mps}``, ``${sky}``,
|
``"4c57f0c88d9844630327623633ce269cf826ab99"``
|
||||||
``${weather}``, ``${temp c}``, ``${humid}`` and ``${press}``
|
* Returns a table with string keys: ``${city}``, ``${humid}``, ``${press}``,
|
||||||
|
``${sky}``, ``${sunrise}``, ``${sunset}``, ``${temp c}``, ``${temp max c}``,
|
||||||
|
``${temp min c}``, ``${weather}``, ``${wind aim}``, ``${wind deg}``,
|
||||||
|
``${wind kmh}`` and ``${wind mps}``,
|
||||||
|
|
||||||
vicious.contrib.nvinf
|
vicious.contrib.nvinf
|
||||||
^^^^^^^^^^^^^^^^^^^^^
|
^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
Loading…
Reference in New Issue