]> git.madduck.net Git - etc/awesome.git/blob - widgets/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:

whitespaces cleaning
[etc/awesome.git] / widgets / weather.lua
1
2 --[[
3                                                   
4      Licensed under GNU General Public License v2 
5       * (c) 2015, Luke Bonham                     
6                                                   
7 --]]
8
9 local newtimer     = require("lain.helpers").newtimer
10 local async        = require("lain.asyncshell")
11 local json         = require("lain.util").dkjson
12 local lain_icons   = require("lain.helpers").icons_dir
13 local naughty      = require("naughty")
14 local wibox        = require("wibox")
15
16 local math         = { floor  = math.floor }
17 local string       = { format = string.format,
18                        gsub   = string.gsub }
19
20 local setmetatable = setmetatable
21
22 -- OpenWeatherMap
23 -- current weather and X-days forecast
24 -- lain.widgets.weather
25
26 local function worker(args)
27     local weather               = {}
28     local args                  = args or {}
29     local timeout               = args.timeout or 900   -- 15 min
30     local timeout_forecast      = args.timeout or 86400 -- 24 hrs
31     local current_call          = "curl -s 'http://api.openweathermap.org/data/2.5/weather?id=%s&units=%s&lang=%s'"
32     local forecast_call         = "curl -s 'http://api.openweathermap.org/data/2.5/forecast/daily?id=%s&units=%s&lang=%s&cnt=%s'"
33     local city_id               = args.city_id
34     local units                 = args.units or "metric"
35     local lang                  = args.lang or "en"
36     local cnt                   = args.cnt or 7
37     local date_cmd              = args.date_cmd or "date -u -d @%d +'%%a %%d'"
38     local icons_path            = args.icons_path or lain_icons .. "openweathermap/"
39     local w_notification_preset = args.w_notification_preset or {}
40     local settings              = args.settings or function() end
41
42     weather.widget = wibox.widget.textbox('')
43     weather.icon   = wibox.widget.imagebox()
44
45     function weather.show(t_out)
46         weather.hide()
47         weather.notification = naughty.notify({
48             text    = weather.notification_text,
49             icon    = weather.icon_path,
50             timeout = t_out,
51             preset  = w_notification_preset
52         })
53     end
54
55     function weather.hide()
56         if weather.notification ~= nil then
57             naughty.destroy(weather.notification)
58             weather.notification = nil
59         end
60     end
61
62     function weather.attach(obj)
63         obj:connect_signal("mouse::enter", function()
64             weather.show(0)
65         end)
66         obj:connect_signal("mouse::leave", function()
67             weather.hide()
68         end)
69     end
70
71     function weather.forecast_update()
72         local cmd = string.format(forecast_call, city_id, units, lang, cnt)
73         async.request(cmd, function(f)
74             j = f:read("*a")
75             f:close()
76             weather_now, pos, err = json.decode(j, 1, nil)
77
78             if tonumber(weather_now["cod"]) == 200 then
79                 weather.notification_text = ''
80                 for i = 1, weather_now["cnt"] do
81                     local f = assert(io.popen(string.format(date_cmd, weather_now["list"][i]["dt"])))
82                     day = string.gsub(f:read("a"), "\n", "")
83                     f:close()
84
85                     tmin = math.floor(weather_now["list"][i]["temp"]["min"])
86                     tmax = math.floor(weather_now["list"][i]["temp"]["max"])
87                     desc = weather_now["list"][i]["weather"][1]["description"]
88
89                     weather.notification_text = weather.notification_text ..
90                                                 string.format("<b>%s</b>: %s, %d - %d  ", day, desc, tmin, tmax)
91
92                     if i < weather_now["cnt"] then
93                         weather.notification_text = weather.notification_text .. "\n"
94                     end
95                 end
96             end
97         end)
98     end
99
100     function weather.update()
101         local cmd = string.format(current_call, city_id, units, lang)
102         async.request(cmd, function(f)
103             j = f:read("*a")
104             f:close()
105             weather_now, pos, err = json.decode(j, 1, nil)
106
107             if err then
108                 weather.widget.set_text("N/A")
109                 weather.icon:set_image(icons_path .. "na.png")
110             elseif tonumber(weather_now["cod"]) == 200 then
111                 weather.icon_path = icons_path .. weather_now["weather"][1]["icon"] .. ".png"
112                 weather.icon:set_image(weather.icon_path)
113                 widget = weather.widget
114                 settings()
115             end
116         end)
117     end
118
119     newtimer("weather", timeout, weather.update)
120     newtimer("weather_forecast", timeout, weather.forecast_update)
121
122     return setmetatable(weather, { __index = weather.widget })
123 end
124
125 return setmetatable({}, { __call = function(_, ...) return worker(...) end })