Skip to content

Commit edfd512

Browse files
authored
[contrib.openweather] Support API ID and additional return values
1 parent 4ae9245 commit edfd512

File tree

2 files changed

+74
-58
lines changed

2 files changed

+74
-58
lines changed

contrib/openweather_all.lua

Lines changed: 65 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
-- contrib/openweather_all.lua
22
-- Copyright (C) 2013 NormalRa <normalrawr gmail com>
33
-- Copyright (C) 2017 Jörg Thalheim <[email protected]>
4+
-- Copyright (C) 2020 Marcel Arpogaus <marcel.arpogaus gmail com>
45
--
56
-- This file is part of Vicious.
67
--
@@ -16,69 +17,66 @@
1617
--
1718
-- You should have received a copy of the GNU General Public License
1819
-- along with Vicious. If not, see <https://www.gnu.org/licenses/>.
19-
2020
-- {{{ Grab environment
2121
local tonumber = tonumber
22-
local io = { popen = io.popen }
23-
local setmetatable = setmetatable
24-
local string = { match = string.match }
25-
local math = {
26-
ceil = math.ceil,
27-
floor = math.floor
28-
}
22+
local string = {match = string.match}
23+
local math = {ceil = math.ceil, floor = math.floor}
24+
local helpers = require "vicious.helpers"
25+
local spawn = require "vicious.spawn"
2926
-- }}}
3027

31-
3228
-- Openweather: provides weather information for a requested station
3329
-- vicious.widgets.openweather
3430
local openweather_all = {}
3531

36-
3732
-- Initialize function tables
38-
local _wdirs = { "N", "NE", "E", "SE", "S", "SW", "W", "NW", "N" }
33+
local _wdirs = {"N", "NE", "E", "SE", "S", "SW", "W", "NW", "N"}
3934
local _wdata = {
40-
["{city}"] = "N/A",
41-
["{wind deg}"] = "N/A",
42-
["{wind aim}"] = "N/A",
43-
["{wind mps}"] = "N/A",
44-
["{wind kmh}"] = "N/A",
45-
["{sky}"] = "N/A",
46-
["{weather}"] = "N/A",
47-
["{temp c}"] = "N/A",
48-
["{humid}"] = "N/A",
49-
["{press}"] = "N/A"
35+
["{city}"] = "N/A",
36+
["{wind deg}"] = "N/A",
37+
["{wind aim}"] = "N/A",
38+
["{wind mps}"] = "N/A",
39+
["{wind kmh}"] = "N/A",
40+
["{sky}"] = "N/A",
41+
["{weather}"] = "N/A",
42+
["{temp c}"] = "N/A",
43+
["{temp min c}"] = "N/A",
44+
["{temp max c}"] = "N/A",
45+
["{sunrise}"] = -1,
46+
["{sunset}"] = -1,
47+
["{humid}"] = "N/A",
48+
["{press}"] = "N/A"
5049
}
5150

5251
-- {{{ Openweather widget type
53-
local function worker(format, warg)
54-
if not warg then return end
55-
56-
-- Get weather forceast using the city ID code, from:
57-
-- * OpenWeatherMap.org
58-
local openweather = "http://api.openweathermap.org/data/2.5/weather?id="..warg.."&mode=json&units=metric"
59-
local f = io.popen("curl --connect-timeout 1 -fsm 3 '"..openweather.."'")
60-
local ws = f:read("*all")
61-
f:close()
62-
52+
local function parse(stdout, stderr, exitreason, exitcode)
6353
-- Check if there was a timeout or a problem with the station
64-
if ws == nil then return _wdata end
54+
if stdout == nil or exitcode ~= 0 then return _wdata end
6555

66-
_wdata["{city}"] = -- City name
67-
string.match(ws, '"name":"([%a%s%-]+)"') or _wdata["{city}"]
56+
_wdata["{city}"] = -- City name
57+
string.match(stdout, '"name":"([%a%s%-]+)"') or _wdata["{city}"]
6858
_wdata["{wind deg}"] = -- Wind degrees
69-
string.match(ws, '"deg":([%d]+)') or _wdata["{wind deg}"]
70-
_wdata["{wind mps}"] = -- Wind speed in meters per second
71-
string.match(ws, '"speed":([%d%.]+)') or _wdata["{wind mps}"]
72-
_wdata["{sky}"] = -- Sky conditions
73-
string.match(ws, '"main":"([%a]+)"') or _wdata["{sky}"]
74-
_wdata["{weather}"] = -- Weather description
75-
string.match(ws, '"description":"([%a%s]+)"') or _wdata["{weather}"]
76-
_wdata["{temp c}"] = -- Temperature in celsius
77-
string.match(ws, '"temp":([%-]?[%d%.]+)') or _wdata["{temp c}"]
78-
_wdata["{humid}"] = -- Relative humidity in percent
79-
string.match(ws, '"humidity":([%d]+)') or _wdata["{humid}"]
80-
_wdata["{press}"] = -- Pressure in hPa
81-
string.match(ws, '"pressure":([%d%.]+)') or _wdata["{press}"]
59+
string.match(stdout, '"deg":([%d]+)') or _wdata["{wind deg}"]
60+
_wdata["{wind mps}"] = -- Wind speed in meters per second
61+
string.match(stdout, '"speed":([%d%.]+)') or _wdata["{wind mps}"]
62+
_wdata["{sky}"] = -- Sky conditions
63+
string.match(stdout, '"main":"([%a]+)"') or _wdata["{sky}"]
64+
_wdata["{weather}"] = -- Weather description
65+
string.match(stdout, '"description":"([%a%s]+)"') or _wdata["{weather}"]
66+
_wdata["{temp c}"] = -- Temperature in celsius
67+
string.match(stdout, '"temp":([%-]?[%d%.]+)') or _wdata["{temp c}"]
68+
_wdata["{temp min c}"] = -- Minimal Temperature in celsius
69+
string.match(stdout, '"temp_min":([%-]?[%d%.]+)') or _wdata["{temp min c}"]
70+
_wdata["{temp max c}"] = -- Maximal Temperature in celsius
71+
string.match(stdout, '"temp_max":([%-]?[%d%.]+)') or _wdata["{temp max c}"]
72+
_wdata["{humid}"] = -- Relative humidity in percent
73+
string.match(stdout, '"humidity":([%d]+)') or _wdata["{humid}"]
74+
_wdata["{sunrise}"] = -- Sunrise
75+
tonumber(string.match(stdout, '"sunrise":([%d]+)')) or _wdata["{sunrise}"]
76+
_wdata["{sunset}"] = -- Sunset
77+
tonumber(string.match(stdout, '"sunset":([%d]+)')) or _wdata["{sunset}"]
78+
_wdata["{press}"] = -- Pressure in hPa
79+
string.match(stdout, '"pressure":([%d%.]+)') or _wdata["{press}"]
8280

8381
-- Wind speed in km/h
8482
if _wdata["{wind mps}"] ~= "N/A" then
@@ -92,17 +90,32 @@ local function worker(format, warg)
9290
_wdata["{wind deg}"] = tonumber(_wdata["{wind deg}"])
9391

9492
-- Lua tables start at [1]
95-
if (_wdata["{wind deg}"] / 45)%1 == 0 then
93+
if (_wdata["{wind deg}"] / 45) % 1 == 0 then
9694
_wdata["{wind aim}"] = _wdirs[_wdata["{wind deg}"] / 45 + 1]
9795
else
98-
_wdata["{wind aim}"] =
99-
_wdirs[math.ceil(_wdata["{wind deg}"] / 45) + 1]..
100-
_wdirs[math.floor(_wdata["{wind deg}"] / 45) + 1]
96+
_wdata["{wind aim}"] = _wdirs[math.ceil(_wdata["{wind deg}"] / 45) +
97+
1] ..
98+
_wdirs[math.floor(
99+
_wdata["{wind deg}"] / 45) + 1]
101100
end
102101
end
103102

104103
return _wdata
105104
end
105+
106+
function openweather_all.async(format, warg, callback)
107+
if not warg then return callback {} end
108+
if type(warg) ~= "table" then return callback {} end
109+
110+
-- Get weather forceast using the city ID code, from:
111+
-- * OpenWeatherMap.org
112+
local openweather = "http://api.openweathermap.org/data/2.5/weather?id=" ..
113+
warg.city_id .. "&appid=" .. warg.app_id ..
114+
"&mode=json&units=metric"
115+
116+
spawn.easy_async("curl --connect-timeout 1 -fsm 3 '" .. openweather .. "'",
117+
function(...) callback(parse(...)) end)
118+
end
106119
-- }}}
107120

108-
return setmetatable(openweather_all, { __call = function(_, ...) return worker(...) end })
121+
return helpers.setasyncall(openweather_all)

docs/source/contrib.rst

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,15 @@ vicious.contrib.net
133133
vicious.contrib.openweather
134134
^^^^^^^^^^^^^^^^^^^^^^^^^^^
135135

136-
Provides weather information for a requested city
137-
138-
* Argument: OpenWeatherMap city ID, e.g. ``"1275339"``
139-
* Returns a table with string keys: ``${city}``, ``${wind deg}``,
140-
``${wind aim}``, ``${wind kmh}``, ``${wind mps}``, ``${sky}``,
141-
``${weather}``, ``${temp c}``, ``${humid}`` and ``${press}``
136+
Provides weather information for a requested city from OpenWeatherMap (OWM)
137+
138+
* Argument: a table containing the fields ``city_id`` with the OWM city ID, e.g.
139+
``"2643743"`` and ``app_id`` with the the OWM app ID, e.g
140+
``"4c57f0c88d9844630327623633ce269cf826ab99"``
141+
* Returns a table with string keys: ``${city}``, ``${humid}``, ``${press}``,
142+
``${sky}``, ``${sunrise}``, ``${sunset}``, ``${temp c}``, ``${temp max c}``,
143+
``${temp min c}``, ``${weather}``, ``${wind aim}``, ``${wind deg}``,
144+
``${wind kmh}`` and ``${wind mps}``,
142145

143146
vicious.contrib.nvinf
144147
^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)