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
11 local async = require("lain.asyncshell")
12 local json = require("lain.util").dkjson
13 local lain_icons = require("lain.helpers").icons_dir
15 local read_pipe = require("lain.helpers").read_pipe
17 local async = require("lain.asyncshell")
18 local json = require("lain.util").dkjson
19 local lain_icons = require("lain.helpers").icons_dir
21 >>>>>>> upstream/master
22 local naughty = require("naughty")
23 local wibox = require("wibox")
25 local math = { floor = math.floor }
29 >>>>>>> upstream/master
30 local string = { format = string.format,
33 local setmetatable = setmetatable
36 -- current weather and X-days forecast
37 -- lain.widgets.weather
39 local function worker(args)
42 local args = args or {}
43 local timeout = args.timeout or 900 -- 15 min
44 local timeout_forecast = args.timeout or 86400 -- 24 hrs
45 local current_call = "curl -s 'http://api.openweathermap.org/data/2.5/weather?id=%s&units=%s&lang=%s'"
46 local forecast_call = "curl -s 'http://api.openweathermap.org/data/2.5/forecast/daily?id=%s&units=%s&lang=%s&cnt=%s'"
47 local city_id = args.city_id or 0 -- placeholder
48 local units = args.units or "metric"
49 local lang = args.lang or "en"
50 local cnt = args.cnt or 7
51 local date_cmd = args.date_cmd or "date -u -d @%d +'%%a %%d'"
52 local icons_path = args.icons_path or lain_icons .. "openweathermap/"
53 local w_notification_preset = args.w_notification_preset or {}
54 local settings = args.settings or function() end
57 local args = args or {}
58 local timeout = args.timeout or 900 -- 15 min
59 local timeout_forecast = args.timeout or 86400 -- 24 hrs
60 local current_call = "curl -s 'http://api.openweathermap.org/data/2.5/weather?id=%s&units=%s&lang=%s'"
61 local forecast_call = "curl -s 'http://api.openweathermap.org/data/2.5/forecast/daily?id=%s&units=%s&lang=%s&cnt=%s'"
62 local city_id = args.city_id or 0 -- placeholder
63 local units = args.units or "metric"
64 local lang = args.lang or "en"
65 local cnt = args.cnt or 7
66 local date_cmd = args.date_cmd or "date -u -d @%d +'%%a %%d'"
67 local icons_path = args.icons_path or lain_icons .. "openweathermap/"
68 local notification_preset = args.notification_preset or {}
69 local followmouse = args.followmouse or false
70 local settings = args.settings or function() end
71 >>>>>>> upstream/master
73 weather.widget = wibox.widget.textbox('')
74 weather.icon = wibox.widget.imagebox()
76 function weather.show(t_out)
82 notification_preset.screen = mouse.screen
85 >>>>>>> upstream/master
86 weather.notification = naughty.notify({
87 text = weather.notification_text,
88 icon = weather.icon_path,
91 preset = w_notification_preset
93 preset = notification_preset
94 >>>>>>> upstream/master
98 function weather.hide()
99 if weather.notification ~= nil then
100 naughty.destroy(weather.notification)
101 weather.notification = nil
105 function weather.attach(obj)
106 obj:connect_signal("mouse::enter", function()
109 obj:connect_signal("mouse::leave", function()
114 function weather.forecast_update()
115 local cmd = string.format(forecast_call, city_id, units, lang, cnt)
116 async.request(cmd, function(f)
120 weather_now, pos, err = json.decode(j, 1, nil)
122 weather_now, pos, err = json.decode(f, 1, nil)
123 >>>>>>> upstream/master
125 if not err and weather_now ~= nil and tonumber(weather_now["cod"]) == 200 then
126 weather.notification_text = ''
127 for i = 1, weather_now["cnt"] do
129 local f = assert(io.popen(string.format(date_cmd, weather_now["list"][i]["dt"])))
130 day = string.gsub(f:read("a"), "\n", "")
133 day = string.gsub(read_pipe(string.format(date_cmd, weather_now["list"][i]["dt"])), "\n", "")
134 >>>>>>> upstream/master
136 tmin = math.floor(weather_now["list"][i]["temp"]["min"])
137 tmax = math.floor(weather_now["list"][i]["temp"]["max"])
138 desc = weather_now["list"][i]["weather"][1]["description"]
140 weather.notification_text = weather.notification_text ..
141 string.format("<b>%s</b>: %s, %d - %d ", day, desc, tmin, tmax)
143 if i < weather_now["cnt"] then
144 weather.notification_text = weather.notification_text .. "\n"
148 weather.icon_path = icons_path .. "na.png"
149 weather.notification_text = "API/connection error or bad/not set city ID"
154 function weather.update()
155 local cmd = string.format(current_call, city_id, units, lang)
156 async.request(cmd, function(f)
160 weather_now, pos, err = json.decode(j, 1, nil)
162 weather_now, pos, err = json.decode(f, 1, nil)
163 >>>>>>> upstream/master
165 if not err and weather_now ~= nil and tonumber(weather_now["cod"]) == 200 then
166 weather.icon_path = icons_path .. weather_now["weather"][1]["icon"] .. ".png"
167 weather.icon:set_image(weather.icon_path)
168 widget = weather.widget
171 weather.widget._layout.text = " N/A " -- tries to avoid textbox bugs
172 weather.icon:set_image(icons_path .. "na.png")
177 weather.attach(weather.widget)
179 newtimer("weather-" .. city_id, timeout, weather.update)
180 newtimer("weather_forecast" .. city_id, timeout, weather.forecast_update)
182 return setmetatable(weather, { __index = weather.widget })
185 return setmetatable({}, { __call = function(_, ...) return worker(...) end })