Debian Codegraph @

vicious/widgets/weather_all.lua

raw symbols SCIP
1 -- weather widget type fetching from from US NOAA
2 -- Copyright (C) 2010 Adrian C. <[email protected]>
3 -- Copyright (C) 2017 mutlusun <[email protected]>
4 -- Copyright (C) 2019 Arthur Axel 'fREW' Schmidt <[email protected]>
5 -- Copyright (C) 2019 Nguyễn Gia Phong <[email protected]m>
6 --
7 -- This file is part of Vicious.
8 --
9 -- Vicious is free software: you can redistribute it and/or modify
10 -- it under the terms of the GNU General Public License as
11 -- published by the Free Software Foundation, either version 2 of the
12 -- License, or (at your option) any later version.
13 --
14 -- Vicious is distributed in the hope that it will be useful,
15 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
16 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 -- GNU General Public License for more details.
18 --
19 -- You should have received a copy of the GNU General Public License
20 -- along with Vicious. If not, see <https://www.gnu.org/licenses/>.
21
22 -- {{{ Grab environment
23 local tonumber = tonumber
24 local math = { ceil = math.ceil }
25 local os = { date = os.date, difftime = os.difftime, time = os.time }
26 local string = { format = string.format }
27
28 local spawn = require"vicious.spawn"
29 local helpers = require"vicious.helpers"
30 -- }}}
31
32 -- Weather: provides weather information for a requested station
33 -- vicious.widgets.weather
34 local weather_all = {}
35
36 -- copied from http://lua-users.org/wiki/TimeZone
37 local function get_timezone_offset()
38 local ts = os.time()
39 local utcdate = os.date("!*t", ts)
40 local localdate = os.date("*t", ts)
41 localdate.isdst = false -- this is the trick
42 return os.difftime(os.time(localdate), os.time(utcdate))
43 end
44
45 -- {{{ Weather widget type
46 local function parse(stdout, stderr, exitreason, exitcode)
47 -- Initialize function tables
48 local _weather = {
49 ["{city}"] = "N/A",
50 ["{when}"] = "N/A",
51 ["{wind}"] = "N/A",
52 ["{windmph}"] = "N/A",
53 ["{windkmh}"] = "N/A",
54 ["{sky}"] = "N/A",
55 ["{weather}"] = "N/A",
56 ["{tempf}"] = "N/A",
57 ["{tempc}"] = "N/A",
58 ["{dewf}"] = "N/A",
59 ["{dewc}"] = "N/A",
60 ["{humid}"] = "N/A",
61 ["{press}"] = "N/A"
62 }
63
64 -- Check if there was a timeout or a problem with the station
65 if stdout == '' then return _weather end
66
67 _weather["{city}"] = -- City and/or area
68 stdout:match"^(.+)%,.*%([%u]+%)"
69 or _weather["{city}"]
70 _weather["{wind}"] = -- Wind direction and degrees if available
71 stdout:match"Wind:[%s][%a]+[%s][%a]+[%s](.+)[%s]at.+$"
72 or _weather["{wind}"]
73 _weather["{windmph}"] = -- Wind speed in MPH if available
74 stdout:match"Wind:[%s].+[%s]at[%s]([%d]+)[%s]MPH"
75 or _weather["{windmph}"]
76 _weather["{sky}"] = -- Sky conditions if available
77 stdout:match"Sky[%s]conditions:[%s](.-)[%c]"
78 or _weather["{sky}"]
79 _weather["{weather}"] = -- Weather conditions if available
80 stdout:match"Weather:[%s](.-)[%c]"
81 or _weather["{weather}"]
82 _weather["{tempf}"] = -- Temperature in fahrenheit
83 stdout:match"Temperature:[%s]([%-]?[%d%.]+).*[%c]"
84 or _weather["{tempf}"]
85 _weather["{dewf}"] = -- Dew Point in fahrenheit
86 stdout:match"Dew[%s]Point:[%s]([%-]?[%d%.]+).*[%c]"
87 or _weather["{dewf}"]
88 _weather["{humid}"] = -- Relative humidity in percent
89 stdout:match"Relative[%s]Humidity:[%s]([%d]+)%%"
90 or _weather["{humid}"]
91 _weather["{press}"] = -- Pressure in hPa
92 stdout:match"Pressure[%s].+%((.+)[%s]hPa%)"
93 or _weather["{press}"]
94
95 local year, month, day, hour, min =
96 stdout:match"(%d%d%d%d).(%d%d).(%d%d) (%d%d)(%d%d) UTC"
97 if year ~= nil then
98 local utctable = { year = year, month = month, day = day,
99 hour = hour, min = min }
100 _weather["{when}"] = os.time(utctable) + get_timezone_offset()
101 end
102
103 -- Wind speed in km/h if MPH was available
104 if _weather["{windmph}"] ~= "N/A" then
105 _weather["{windmph}"] = tonumber(_weather["{windmph}"])
106 _weather["{windkmh}"] = math.ceil(_weather["{windmph}"] * 1.6)
107 end -- Temperature in °C if °F was available
108 if _weather["{tempf}"] ~= "N/A" then
109 _weather["{tempf}"] = tonumber(_weather["{tempf}"])
110 _weather["{tempc}"] = math.ceil((_weather["{tempf}"] - 32) * 5/9)
111 end -- Dew Point in °C if °F was available
112 if _weather["{dewf}"] ~= "N/A" then
113 _weather["{dewf}"] = tonumber(_weather["{dewf}"])
114 _weather["{dewc}"] = math.ceil((_weather["{dewf}"] - 32) * 5/9)
115 end -- Capitalize some stats so they don't look so out of place
116 if _weather["{sky}"] ~= "N/A" then
117 _weather["{sky}"] = helpers.capitalize(_weather["{sky}"])
118 end
119 if _weather["{weather}"] ~= "N/A" then
120 _weather["{weather}"] = helpers.capitalize(_weather["{weather}"])
121 end
122
123 return _weather
124 end
125
126 function weather_all.async(format, warg, callback)
127 if not warg then return callback{} end
128
129 -- Get weather forceast by the station ICAO code, from:
130 -- * US National Oceanic and Atmospheric Administration
131 local url = string.format(
132 "https://tgftp.nws.noaa.gov/data/observations/metar/decoded/%s.TXT",
133 warg)
134 spawn.easy_async("curl -fs " .. url,
135 function (...) callback(parse(...)) end)
136 end
137 -- }}}
138
139 return helpers.setasyncall(weather_all)