| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
m> |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 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 |
|
| 33 |
|
| 34 |
local weather_all = {} |
| 35 |
|
| 36 |
|
| 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 |
| 42 |
return os.difftime(os.time(localdate), os.time(utcdate)) |
| 43 |
end |
| 44 |
|
| 45 |
|
| 46 |
local function parse(stdout, stderr, exitreason, exitcode) |
| 47 |
|
| 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 |
|
| 65 |
if stdout == '' then return _weather end |
| 66 |
|
| 67 |
_weather["{city}"] = |
| 68 |
stdout:match"^(.+)%,.*%([%u]+%)" |
| 69 |
or _weather["{city}"] |
| 70 |
_weather["{wind}"] = |
| 71 |
stdout:match"Wind:[%s][%a]+[%s][%a]+[%s](.+)[%s]at.+$" |
| 72 |
or _weather["{wind}"] |
| 73 |
_weather["{windmph}"] = |
| 74 |
stdout:match"Wind:[%s].+[%s]at[%s]([%d]+)[%s]MPH" |
| 75 |
or _weather["{windmph}"] |
| 76 |
_weather["{sky}"] = |
| 77 |
stdout:match"Sky[%s]conditions:[%s](.-)[%c]" |
| 78 |
or _weather["{sky}"] |
| 79 |
_weather["{weather}"] = |
| 80 |
stdout:match"Weather:[%s](.-)[%c]" |
| 81 |
or _weather["{weather}"] |
| 82 |
_weather["{tempf}"] = |
| 83 |
stdout:match"Temperature:[%s]([%-]?[%d%.]+).*[%c]" |
| 84 |
or _weather["{tempf}"] |
| 85 |
_weather["{dewf}"] = |
| 86 |
stdout:match"Dew[%s]Point:[%s]([%-]?[%d%.]+).*[%c]" |
| 87 |
or _weather["{dewf}"] |
| 88 |
_weather["{humid}"] = |
| 89 |
stdout:match"Relative[%s]Humidity:[%s]([%d]+)%%" |
| 90 |
or _weather["{humid}"] |
| 91 |
_weather["{press}"] = |
| 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 |
|
| 104 |
if _weather["{windmph}"] ~= "N/A" then |
| 105 |
_weather["{windmph}"] = tonumber(_weather["{windmph}"]) |
| 106 |
_weather["{windkmh}"] = math.ceil(_weather["{windmph}"] * 1.6) |
| 107 |
end le |
| 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 le |
| 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 |
| 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 |
|
| 130 |
|
| 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) |