]> git.madduck.net Git - etc/awesome.git/blob - widget/weather.lua

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

17fc4d7b2e72a861b115a13adaf39ae4b65cbe05
[etc/awesome.git] / widget / weather.lua
1
2 --[[
3                                                   
4      Licensed under GNU General Public License v2 
5       * (c) 2015, Luke Bonham                     
6                                                   
7 --]]
8
9 local helpers  = require("lain.helpers")
10 local json     = require("lain.util").dkjson
11 local focused  = require("awful.screen").focused
12 local naughty  = require("naughty")
13 local wibox    = require("wibox")
14 local math     = { floor  = math.floor }
15 local os       = { time   = os.time,
16                    date   = os.date }
17 local string   = { format = string.format,
18                    gsub   = string.gsub }
19 local tonumber = tonumber
20
21 -- OpenWeatherMap
22 -- current weather and X-days forecast
23 -- lain.widget.weather
24
25 local function factory(args)
26     local weather               = { widget = wibox.widget.textbox() }
27     local args                  = args or {}
28     local APPID                 = args.APPID or "3e321f9414eaedbfab34983bda77a66e" -- lain default
29     local timeout               = args.timeout or 60 * 15 -- 15 min
30     local timeout_forecast      = args.timeout or 60 * 60 * 24 -- 24 hrs
31     local current_call          = args.current_call  or "curl -s 'http://api.openweathermap.org/data/2.5/weather?id=%s&units=%s&lang=%s&APPID=%s'"
32     local forecast_call         = args.forecast_call or "curl -s 'http://api.openweathermap.org/data/2.5/forecast/daily?id=%s&units=%s&lang=%s&cnt=%s&APPID=%s'"
33     local city_id               = args.city_id or 0 -- placeholder
34     local units                 = args.units or "metric"
35     local lang                  = args.lang or "en"
36     local cnt                   = args.cnt or 5
37     local date_cmd              = args.date_cmd or "date -u -d @%d +'%%a %%d'"
38     local icons_path            = args.icons_path or helpers.icons_dir .. "openweathermap/"
39     local notification_preset   = args.notification_preset or {}
40     local notification_text_fun = args.notification_text_fun or
41                                   function (wn)
42                                       local day = os.date("%a %d", wn["dt"])
43                                       local tmin = math.floor(wn["temp"]["min"])
44                                       local tmax = math.floor(wn["temp"]["max"])
45                                       local desc = wn["weather"][1]["description"]
46                                       return string.format("<b>%s</b>: %s, %d - %d ", day, desc, tmin, tmax)
47                                   end
48     local weather_na_markup     = args.weather_na_markup or " N/A "
49     local followtag             = args.followtag or false
50     local showpopup             = args.showpopup or "on"
51     local settings              = args.settings or function() end
52
53     weather.widget:set_markup(weather_na_markup)
54     weather.icon_path = icons_path .. "na.png"
55     weather.icon = wibox.widget.imagebox(weather.icon_path)
56
57     function weather.show(t_out)
58         weather.hide()
59
60         if followtag then
61             notification_preset.screen = focused()
62         end
63
64         if not weather.notification_text then
65             weather.update()
66             weather.forecast_update()
67         end
68
69         weather.notification = naughty.notify({
70             text    = weather.notification_text,
71             icon    = weather.icon_path,
72             timeout = t_out,
73             preset  = notification_preset
74         })
75     end
76
77     function weather.hide()
78         if weather.notification then
79             naughty.destroy(weather.notification)
80             weather.notification = nil
81         end
82     end
83
84     function weather.attach(obj)
85         obj:connect_signal("mouse::enter", function()
86             weather.show(0)
87         end)
88         obj:connect_signal("mouse::leave", function()
89             weather.hide()
90         end)
91     end
92
93     function weather.forecast_update()
94         local cmd = string.format(forecast_call, city_id, units, lang, cnt, APPID)
95         helpers.async(cmd, function(f)
96             local pos, err
97             weather_now, pos, err = json.decode(f, 1, nil)
98
99             if not err and type(weather_now) == "table" and tonumber(weather_now["cod"]) == 200 then
100                 weather.notification_text = ""
101                 for i = 1, weather_now["cnt"] do
102                     weather.notification_text = weather.notification_text ..
103                                                 notification_text_fun(weather_now["list"][i])
104                     if i < weather_now["cnt"] then
105                         weather.notification_text = weather.notification_text .. "\n"
106                     end
107                 end
108             end
109         end)
110     end
111
112     function weather.update()
113         local cmd = string.format(current_call, city_id, units, lang, APPID)
114         helpers.async(cmd, function(f)
115             local pos, err, icon
116             weather_now, pos, err = json.decode(f, 1, nil)
117
118             if not err and type(weather_now) == "table" and tonumber(weather_now["cod"]) == 200 then
119                 local now     = os.time(os.date("!*t"))
120                 local sunrise = tonumber(weather_now["sys"]["sunrise"])
121                 local sunset  = tonumber(weather_now["sys"]["sunset"])
122                 local icon    = weather_now["weather"][1]["icon"]
123
124                 if sunrise <= now and now <= sunset then
125                     icon = string.gsub(icon, "n", "d")
126                 else
127                     icon = string.gsub(icon, "d", "n")
128                 end
129
130                 weather.icon_path = icons_path .. icon .. ".png"
131                 widget = weather.widget
132                 settings()
133             else
134                 weather.icon_path = icons_path .. "na.png"
135                 weather.widget:set_markup(weather_na_markup)
136             end
137
138             weather.icon:set_image(weather.icon_path)
139         end)
140     end
141
142     if showpopup == "on" then weather.attach(weather.widget) end
143
144     weather.timer = helpers.newtimer("weather-" .. city_id, timeout, weather.update, false, true)
145     weather.timer_forecast = helpers.newtimer("weather_forecast-" .. city_id, timeout, weather.forecast_update, false, true)
146
147     return weather
148 end
149
150 return factory