aboutsummaryrefslogtreecommitdiff
path: root/weather.lua
blob: cf43aa1d94ff1f159f6d1996651467ec20eb9868 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
----------------------------------------------------------
-- Licensed under the GNU General Public License version 2
--  * Copyright (C) 2009 Adrian C. <anrxc_sysphere_org>
----------------------------------------------------------

-- {{{ Grab environment
local io = { popen = io.popen }
local setmetatable = setmetatable
local math = { floor = math.floor }
local string = { match = string.match }
-- }}}


-- Weather: provides weather information for a requested station
module("vicious.weather")


-- {{{ Weather widget type
local function worker(format, station)
    -- US National Oceanic and Atmospheric Administration
    --   * ICAO 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
    local f = io.popen("curl --connect-timeout 1 -fsm 3 "..noaa..station..".TXT")
    local ws = f:read("*all")
    f:close()

    -- Default values
    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",
        ["{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}"]

        -- Wind speed in KMH if MPH was available
        if weather["{windmph}"] ~= "N/A" then
           weather["{windkmh}"] = math.floor(weather["{windmph}"] * 1.6)
        end
    end

    return weather
end
-- }}}

setmetatable(_M, { __call = function(_, ...) return worker(...) end })