From 36abb4f26c94fa843a9b2f85732ce7176f7ba8d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Gia=20Phong?= Date: Tue, 12 Feb 2019 20:43:45 +0700 Subject: [PATCH] Update weather widget type to use Awesome async API Timeout for fetching data could be removed since this widget would run on another thread. --- README.md | 5 ++-- widgets/weather_all.lua | 64 ++++++++++++++++++++++++----------------- 2 files changed, 41 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index fd774ec..5ef8e75 100644 --- a/README.md +++ b/README.md @@ -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}`, diff --git a/widgets/weather_all.lua b/widgets/weather_all.lua index c81f33d..7c5a96d 100644 --- a/widgets/weather_all.lua +++ b/widgets/weather_all.lua @@ -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,35 +22,26 @@ local helpers = require("vicious.helpers") local weather_all = {} --- Initialize function tables -local _weather = { - ["{city}"] = "N/A", - ["{wind}"] = "N/A", - ["{windmph}"] = "N/A", - ["{windkmh}"] = "N/A", - ["{sky}"] = "N/A", - ["{weather}"] = "N/A", - ["{tempf}"] = "N/A", - ["{tempc}"] = "N/A", - ["{dewf}"] = "N/A", - ["{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() +local function parse(ws) + -- Initialize function tables + local _weather = { + ["{city}"] = "N/A", + ["{wind}"] = "N/A", + ["{windmph}"] = "N/A", + ["{windkmh}"] = "N/A", + ["{sky}"] = "N/A", + ["{weather}"] = "N/A", + ["{tempf}"] = "N/A", + ["{tempc}"] = "N/A", + ["{dewf}"] = "N/A", + ["{dewc}"] = "N/A", + ["{humid}"] = "N/A", + ["{press}"] = "N/A" + } -- 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 })