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.
4 Licensed under GNU General Public License v2
5 * (c) 2015, Luke Bonham
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")
16 local math = { floor = math.floor }
17 local string = { format = string.format,
20 local setmetatable = setmetatable
23 -- current weather and X-days forecast
24 -- lain.widgets.weather
26 local function worker(args)
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
42 weather.widget = wibox.widget.textbox('')
43 weather.icon = wibox.widget.imagebox()
45 function weather.show(t_out)
47 weather.notification = naughty.notify({
48 text = weather.notification_text,
49 icon = weather.icon_path,
51 preset = w_notification_preset
55 function weather.hide()
56 if weather.notification ~= nil then
57 naughty.destroy(weather.notification)
58 weather.notification = nil
62 function weather.attach(obj)
63 obj:connect_signal("mouse::enter", function()
66 obj:connect_signal("mouse::leave", function()
71 function weather.forecast_update()
72 local cmd = string.format(forecast_call, city_id, units, lang, cnt)
73 async.request(cmd, function(f)
76 weather_now, pos, err = json.decode(j, 1, nil)
78 if not err and weather_now ~= nil and 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", "")
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"]
89 weather.notification_text = weather.notification_text ..
90 string.format("<b>%s</b>: %s, %d - %d ", day, desc, tmin, tmax)
92 if i < weather_now["cnt"] then
93 weather.notification_text = weather.notification_text .. "\n"
100 function weather.update()
101 local cmd = string.format(current_call, city_id, units, lang)
102 async.request(cmd, function(f)
105 weather_now, pos, err = json.decode(j, 1, nil)
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
119 weather.attach(weather.widget)
121 newtimer("weather", timeout, weather.update)
122 newtimer("weather_forecast", timeout, weather.forecast_update)
124 return setmetatable(weather, { __index = weather.widget })
127 return setmetatable({}, { __call = function(_, ...) return worker(...) end })