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 read_pipe = require("lain.helpers").read_pipe
12 local async = require("lain.asyncshell")
13 local json = require("lain.util").dkjson
14 local lain_icons = require("lain.helpers").icons_dir
16 local naughty = require("naughty")
17 local wibox = require("wibox")
19 local math = { floor = math.floor }
21 local string = { format = string.format,
24 local setmetatable = setmetatable
27 -- current weather and X-days forecast
28 -- lain.widgets.weather
30 local function worker(args)
32 local args = args or {}
33 local APPID = args.APPID or 1 -- mandatory
34 local timeout = args.timeout or 900 -- 15 min
35 local timeout_forecast = args.timeout or 86400 -- 24 hrs
36 local current_call = "curl -s 'http://api.openweathermap.org/data/2.5/weather?id=%s&units=%s&lang=%s&APPID=%s'"
37 local forecast_call = "curl -s 'http://api.openweathermap.org/data/2.5/forecast/daily?id=%s&units=%s&lang=%s&cnt=%s&APPID=%s'"
38 local city_id = args.city_id or 0 -- placeholder
39 local units = args.units or "metric"
40 local lang = args.lang or "en"
41 local cnt = args.cnt or 7
42 local date_cmd = args.date_cmd or "date -u -d @%d +'%%a %%d'"
43 local icons_path = args.icons_path or lain_icons .. "openweathermap/"
44 local notification_preset = args.notification_preset or {}
45 local notification_text_fun = args.notification_text_fun or
46 function (day, desc, tmin, tmax)
47 return string.format("<b>%s</b>: %s, %d - %d ", day, desc, tmin, tmax)
49 local followmouse = args.followmouse or false
50 local settings = args.settings or function() end
52 weather.widget = wibox.widget.textbox('')
53 weather.icon = wibox.widget.imagebox()
55 function weather.show(t_out)
59 notification_preset.screen = mouse.screen
62 weather.notification = naughty.notify({
63 text = weather.notification_text,
64 icon = weather.icon_path,
66 preset = notification_preset
70 function weather.hide()
71 if weather.notification ~= nil then
72 naughty.destroy(weather.notification)
73 weather.notification = nil
77 function weather.attach(obj)
78 obj:connect_signal("mouse::enter", function()
81 obj:connect_signal("mouse::leave", function()
86 function weather.forecast_update()
87 local cmd = string.format(forecast_call, city_id, units, lang, cnt, APPID)
88 async.request(cmd, function(f)
90 weather_now, pos, err = json.decode(f, 1, nil)
92 if not err and weather_now ~= nil and tonumber(weather_now["cod"]) == 200 then
93 weather.notification_text = ''
94 for i = 1, weather_now["cnt"] do
95 local day = string.gsub(read_pipe(string.format(date_cmd, weather_now["list"][i]["dt"])), "\n", "")
97 local tmin = math.floor(weather_now["list"][i]["temp"]["min"])
98 local tmax = math.floor(weather_now["list"][i]["temp"]["max"])
99 local desc = weather_now["list"][i]["weather"][1]["description"]
101 weather.notification_text = weather.notification_text ..
102 notification_text_fun(day, desc, tmin, tmax)
104 if i < weather_now["cnt"] then
105 weather.notification_text = weather.notification_text .. "\n"
109 weather.icon_path = icons_path .. "na.png"
110 weather.notification_text = "API/connection error or bad/not set city ID"
115 function weather.update()
116 local cmd = string.format(current_call, city_id, units, lang, APPID)
117 async.request(cmd, function(f)
119 weather_now, pos, err = json.decode(f, 1, nil)
121 if not err and weather_now ~= nil and tonumber(weather_now["cod"]) == 200 then
122 weather.icon_path = icons_path .. weather_now["weather"][1]["icon"] .. ".png"
123 weather.icon:set_image(weather.icon_path)
124 widget = weather.widget
127 weather.widget._layout.text = " N/A " -- tries to avoid textbox bugs
128 weather.icon:set_image(icons_path .. "na.png")
133 weather.attach(weather.widget)
135 newtimer("weather-" .. city_id, timeout, weather.update)
136 newtimer("weather_forecast-" .. city_id, timeout, weather.forecast_update)
138 return setmetatable(weather, { __index = weather.widget })
141 return setmetatable({}, { __call = function(_, ...) return worker(...) end })