2009-09-29 22:33:19 +02:00
|
|
|
---------------------------------------------------
|
|
|
|
-- Licensed under the GNU General Public License v2
|
2010-01-02 21:21:54 +01:00
|
|
|
-- * (c) 2010, Adrian C. <anrxc@sysphere.org>
|
2009-09-29 22:33:19 +02:00
|
|
|
---------------------------------------------------
|
2009-07-29 18:46:43 +02:00
|
|
|
|
|
|
|
-- {{{ Grab environment
|
2010-03-06 03:20:43 +01:00
|
|
|
local tonumber = tonumber
|
2009-07-29 18:46:43 +02:00
|
|
|
local io = { popen = io.popen }
|
2009-08-01 23:11:41 +02:00
|
|
|
local setmetatable = setmetatable
|
2010-03-06 03:20:43 +01:00
|
|
|
local math = { ceil = math.ceil }
|
2009-07-29 18:46:43 +02:00
|
|
|
local string = { match = string.match }
|
|
|
|
-- }}}
|
|
|
|
|
|
|
|
|
|
|
|
-- Weather: provides weather information for a requested station
|
|
|
|
module("vicious.weather")
|
|
|
|
|
|
|
|
|
|
|
|
-- {{{ Weather widget type
|
2009-08-07 17:41:10 +02:00
|
|
|
local function worker(format, station)
|
2009-09-14 17:25:23 +02:00
|
|
|
-- Default values
|
2009-07-29 18:46:43 +02:00
|
|
|
local weather = {
|
|
|
|
["{city}"] = "N/A",
|
|
|
|
["{wind}"] = "N/A",
|
|
|
|
["{windmph}"] = "N/A",
|
2009-08-01 06:20:31 +02:00
|
|
|
["{windkmh}"] = "N/A",
|
2009-07-29 18:46:43 +02:00
|
|
|
["{sky}"] = "N/A",
|
|
|
|
["{weather}"] = "N/A",
|
|
|
|
["{tempf}"] = "N/A",
|
|
|
|
["{tempc}"] = "N/A",
|
|
|
|
["{humid}"] = "N/A",
|
|
|
|
["{press}"] = "N/A"
|
|
|
|
}
|
|
|
|
|
2010-03-06 03:20:43 +01:00
|
|
|
-- Get weather forceast by the station ICAO code, from:
|
|
|
|
-- * US National Oceanic and Atmospheric Administration
|
|
|
|
local noaa = "http://weather.noaa.gov/pub/data/observations/metar/decoded/"
|
|
|
|
local f = io.popen("curl --connect-timeout 1 -fsm 3 "..noaa..station..".TXT")
|
|
|
|
local ws = f:read("*all")
|
|
|
|
f:close()
|
|
|
|
|
2009-07-29 18:46:43 +02:00
|
|
|
-- Check if there was a timeout or a problem with the station
|
2009-10-04 00:54:27 +02:00
|
|
|
if ws == nil then return weather end
|
2009-08-05 21:58:04 +02:00
|
|
|
|
2009-10-04 00:54:27 +02:00
|
|
|
weather["{city}"] = -- City and/or area
|
2010-03-06 03:20:43 +01:00
|
|
|
string.match(ws, "^(.+)%,.*%([%u]+%)") or weather["{city}"]
|
2009-10-04 00:54:27 +02:00
|
|
|
weather["{wind}"] = -- Wind direction and degrees if available
|
2010-03-06 03:20:43 +01:00
|
|
|
string.match(ws, "Wind:[%s][%a]+[%s][%a]+[%s](.+)[%s]at.+$") or weather["{wind}"]
|
2009-10-04 00:54:27 +02:00
|
|
|
weather["{windmph}"] = -- Wind speed in MPH if available
|
2010-03-06 03:20:43 +01:00
|
|
|
string.match(ws, "Wind:[%s].+[%s]at[%s]([%d]+)[%s]MPH") or weather["{windmph}"]
|
2009-10-04 00:54:27 +02:00
|
|
|
weather["{sky}"] = -- Sky conditions if available
|
2010-03-06 03:20:43 +01:00
|
|
|
string.match(ws, "Sky[%s]conditions:[%s](.-)[%c]") or weather["{sky}"]
|
2009-10-04 00:54:27 +02:00
|
|
|
weather["{weather}"] = -- Weather conditions if available
|
2010-03-06 03:20:43 +01:00
|
|
|
string.match(ws, "Weather:[%s](.-)[%c]") or weather["{weather}"]
|
2009-10-04 00:54:27 +02:00
|
|
|
weather["{tempf}"] = -- Temperature in fahrenheit
|
2010-03-06 03:20:43 +01:00
|
|
|
string.match(ws, "Temperature:[%s]([%-]?[%d%.]+).*[%c]") or weather["{tempf}"]
|
2009-10-04 00:54:27 +02:00
|
|
|
weather["{humid}"] = -- Relative humidity in percent
|
2010-03-06 03:20:43 +01:00
|
|
|
string.match(ws, "Relative[%s]Humidity:[%s]([%d]+)%%") or weather["{humid}"]
|
2009-10-04 00:54:27 +02:00
|
|
|
weather["{press}"] = -- Pressure in hPa
|
2010-03-06 03:20:43 +01:00
|
|
|
string.match(ws, "Pressure[%s].+%((.+)[%s]hPa%)") or weather["{press}"]
|
2009-10-04 00:54:27 +02:00
|
|
|
|
2010-03-06 03:20:43 +01:00
|
|
|
-- Wind speed in km/h if MPH was available
|
2009-10-04 00:54:27 +02:00
|
|
|
if weather["{windmph}"] ~= "N/A" then
|
2010-03-06 03:20:43 +01:00
|
|
|
weather["{windmph}"] = tonumber(weather["{windmph}"])
|
|
|
|
weather["{windkmh}"] = math.ceil(weather["{windmph}"] * 1.6)
|
|
|
|
end -- Temperature in degree °C if °F was available
|
|
|
|
if weather["{tempf}"] ~= "N/A" then
|
|
|
|
weather["{tempf}"] = tonumber(weather["{tempf}"])
|
|
|
|
weather["{tempc}"] = math.ceil((weather["{tempf}"] - 32) * 5/9)
|
2009-07-29 18:46:43 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
return weather
|
|
|
|
end
|
|
|
|
-- }}}
|
2009-08-01 23:11:41 +02:00
|
|
|
|
|
|
|
setmetatable(_M, { __call = function(_, ...) return worker(...) end })
|