Update weather widget type to use Awesome async API

Timeout for fetching data could be removed
since this widget would run on another thread.
This commit is contained in:
Nguyễn Gia Phong 2019-02-12 20:43:45 +07:00
parent 94037efc6c
commit 36abb4f26c
2 changed files with 41 additions and 28 deletions

View File

@ -398,7 +398,8 @@ Provides number of pending updates on UNIX systems. Be aware that some package
managers need to update their local databases (as root) before showing the
correct number of updates.
Supported platforms: platform independent.
Supported platforms: platform independent, although it requires Awesome
`awful.spawn` library for non-blocking spawning.
* Argument: distribution name, e.g. `"Arch"`, `"Arch C"`, `"Arch S"`,
`"Debian"`, `"Ubuntu"`, `"Fedora"`, `"FreeBSD"`, `"Mandriva"`.
@ -481,7 +482,7 @@ Supported platforms: GNU/Linux (requiring `amixer`), FreeBSD.
Provides weather information for a requested station.
Supported platforms: any having `curl` installed.
Supported platforms: any having Awesome and `curl` installed.
* Argument: the ICAO station code, e.g. `"LDRI"`
* Returns a table with string keys: `${city}`, `${wind}`, `${windmph}`,

View File

@ -9,6 +9,10 @@ local io = { popen = io.popen }
local setmetatable = setmetatable
local math = { ceil = math.ceil }
local string = { match = string.match }
-- Awesome library for spawning programs
local spawn = require"awful.spawn"
local helpers = require("vicious.helpers")
-- }}}
@ -18,8 +22,10 @@ local helpers = require("vicious.helpers")
local weather_all = {}
-- Initialize function tables
local _weather = {
-- {{{ Weather widget type
local function parse(ws)
-- Initialize function tables
local _weather = {
["{city}"] = "N/A",
["{wind}"] = "N/A",
["{windmph}"] = "N/A",
@ -32,21 +38,10 @@ local _weather = {
["{dewc}"] = "N/A",
["{humid}"] = "N/A",
["{press}"] = "N/A"
}
-- {{{ Weather widget type
local function worker(format, warg)
if not warg then return end
-- Get weather forceast by the station ICAO code, from:
-- * US National Oceanic and Atmospheric Administration
local url = "https://tgftp.nws.noaa.gov/data/observations/metar/decoded/"..warg
local f = io.popen("curl --connect-timeout 1 -fsm 3 "..helpers.shellquote(url)..".TXT")
local ws = f:read("*all")
f:close()
}
-- Check if there was a timeout or a problem with the station
if ws == nil then return _weather end
if ws == '' then return _weather end
_weather["{city}"] = -- City and/or area
string.match(ws, "^(.+)%,.*%([%u]+%)") or _weather["{city}"]
@ -89,6 +84,23 @@ local function worker(format, warg)
return _weather
end
function weather_all.async(format, warg, callback)
if not warg then return end
-- Get weather forceast by the station ICAO code, from:
-- * US National Oceanic and Atmospheric Administration
local url = ("https://tgftp.nws.noaa.gov/data/observations/metar/decoded/%s.TXT"):format(warg)
local cmd = "curl -fs " .. helpers.shellquote(url)
spawn.easy_async(cmd, function (stdout) callback(parse(stdout)) end)
end
local function worker(format, warg)
local ret
weather_all.async(format, warg, function (weather) ret = weather end)
while ret == nil do end
return ret
end
-- }}}
return setmetatable(weather_all, { __call = function(_, ...) return worker(...) end })