2009-07-29 18:46:43 +02:00
|
|
|
----------------------------------------------------------
|
|
|
|
-- Licensed under the GNU General Public License version 2
|
|
|
|
-- * Copyright (C) 2009 Adrian C. <anrxc_sysphere_org>
|
|
|
|
----------------------------------------------------------
|
|
|
|
|
|
|
|
-- {{{ Grab environment
|
|
|
|
local io = { popen = io.popen }
|
2009-08-01 23:11:41 +02:00
|
|
|
local setmetatable = setmetatable
|
2009-08-01 06:20:31 +02:00
|
|
|
local math = { floor = math.floor }
|
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
|
|
|
|
function worker(format, station)
|
|
|
|
-- US National Oceanic and Atmospheric Administration
|
|
|
|
-- * Station codes: http://www.rap.ucar.edu/weather/surface/stations.txt
|
|
|
|
local noaa = "http://weather.noaa.gov/pub/data/observations/metar/decoded/"
|
|
|
|
|
|
|
|
-- Get info from a weather station
|
2009-08-06 01:50:50 +02:00
|
|
|
local f = io.popen("curl --max-time 3 -f -s "..noaa..station..".TXT")
|
2009-07-29 18:46:43 +02:00
|
|
|
local ws = f:read("*all")
|
|
|
|
f:close()
|
|
|
|
|
|
|
|
-- Setup tables
|
|
|
|
local weather = {
|
2009-07-31 21:53:14 +02:00
|
|
|
-- Some of the weather symbols would look nice if prepended to
|
|
|
|
-- the final output, but it is up to the user to do that
|
|
|
|
-- * http://www.alanwood.net/unicode/miscellaneous_symbols.html
|
2009-07-29 18:46:43 +02:00
|
|
|
["{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"
|
|
|
|
}
|
|
|
|
|
|
|
|
-- Check if there was a timeout or a problem with the station
|
|
|
|
if ws == nil then
|
|
|
|
return weather
|
|
|
|
else
|
|
|
|
weather["{city}"] = -- City and/or area
|
|
|
|
string.match(ws, "^(.+)%,.*%([%u]+%)") or weather["{city}"]
|
|
|
|
weather["{wind}"] = -- Wind direction and degrees if available
|
|
|
|
string.match(ws, "Wind:[%s][%a]+[%s][%a]+[%s](.+)[%s]at.+$") or weather["{wind}"]
|
|
|
|
weather["{windmph}"] = -- Wind speed in MPH if available
|
|
|
|
string.match(ws, "Wind:[%s].+[%s]at[%s]([%d]+)[%s]MPH") or weather["{windmph}"]
|
|
|
|
weather["{sky}"] = -- Sky conditions if available
|
|
|
|
string.match(ws, "Sky[%s]conditions:[%s](.-)[%c]") or weather["{sky}"]
|
|
|
|
weather["{weather}"] = -- Weather conditions if available
|
|
|
|
string.match(ws, "Weather:[%s](.-)[%c]") or weather["{weather}"]
|
|
|
|
weather["{tempf}"] = -- Temperature in fahrenheit
|
|
|
|
string.match(ws, "Temperature:[%s]([%d%.]+).*[%c]") or weather["{tempf}"]
|
|
|
|
weather["{tempc}"] = -- Temperature in celsius
|
|
|
|
string.match(ws, "Temperature:[%s][%d%.]+[%s]F[%s]%(([%d%.]+)[%s]C%)[%c]") or weather["{tempc}"]
|
|
|
|
weather["{humid}"] = -- Relative humidity in percent
|
|
|
|
string.match(ws, "Relative[%s]Humidity:[%s]([%d]+)%%") or weather["{humid}"]
|
|
|
|
weather["{press}"] = -- Pressure in hPa
|
|
|
|
string.match(ws, "Pressure[%s].+%((.+)[%s]hPa%)") or weather["{press}"]
|
2009-08-05 21:58:04 +02:00
|
|
|
|
|
|
|
-- Wind speed in KMH if MPH was available
|
|
|
|
if weather["{windmph}"] ~= "N/A" then
|
|
|
|
weather["{windkmh}"] = math.floor(weather["{windmph}"] * 1.6)
|
|
|
|
end
|
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 })
|