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 os = { time = os.time }
22 local string = { format = string.format,
25 local tonumber = tonumber
26 local setmetatable = setmetatable
29 -- current weather and X-days forecast
30 -- lain.widgets.weather
32 local function worker(args)
34 local args = args or {}
35 local APPID = args.APPID or "3e321f9414eaedbfab34983bda77a66e" -- lain default
36 local timeout = args.timeout or 900 -- 15 min
37 local timeout_forecast = args.timeout or 86400 -- 24 hrs
38 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'"
39 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'"
40 local city_id = args.city_id or 0 -- placeholder
41 local utc = args.utc or 0
42 local units = args.units or "metric"
43 local lang = args.lang or "en"
44 local cnt = args.cnt or 5
45 local date_cmd = args.date_cmd or "date -u -d @%d +'%%a %%d'"
46 local icons_path = args.icons_path or lain_icons .. "openweathermap/"
47 local notification_preset = args.notification_preset or {}
48 local notification_text_fun = args.notification_text_fun or
50 local day = string.gsub(read_pipe(string.format(date_cmd, wn["dt"])), "\n", "")
51 local tmin = math.floor(wn["temp"]["min"])
52 local tmax = math.floor(wn["temp"]["max"])
53 local desc = wn["weather"][1]["description"]
55 return string.format("<b>%s</b>: %s, %d - %d ", day, desc, tmin, tmax)
57 local weather_na_markup = args.weather_na_markup or " N/A "
58 local followmouse = args.followmouse or false
59 local settings = args.settings or function() end
61 weather.widget = wibox.widget.textbox(weather_na_markup)
62 weather.icon_path = icons_path .. "na.png"
63 weather.icon = wibox.widget.imagebox(weather.icon_path)
65 function weather.show(t_out)
69 notification_preset.screen = mouse.screen
72 if not weather.notification_text then
73 weather.forecast_update()
76 weather.notification = naughty.notify({
77 text = weather.current_text .. weather.notification_text,
78 icon = weather.icon_path,
80 preset = notification_preset
84 function weather.hide()
85 if weather.notification then
86 naughty.destroy(weather.notification)
87 weather.notification = nil
91 function weather.attach(obj)
92 obj:connect_signal("mouse::enter", function()
95 obj:connect_signal("mouse::leave", function()
100 function weather.forecast_update()
101 local cmd = string.format(forecast_call, city_id, units, lang, cnt, APPID)
102 async.request(cmd, function(f)
104 weather_now, pos, err = json.decode(f, 1, nil)
106 if not err and weather_now and tonumber(weather_now["cod"]) == 200 then
107 weather.notification_text = ''
108 for i = 1, weather_now["cnt"] do
109 weather.notification_text = weather.notification_text ..
110 notification_text_fun(weather_now["list"][i])
112 if i < weather_now["cnt"] then
113 weather.notification_text = weather.notification_text .. "\n"
120 function weather.update()
121 local cmd = string.format(current_call, city_id, units, lang, APPID)
122 async.request(cmd, function(f)
124 weather_now, pos, err = json.decode(f, 1, nil)
125 weather.current_text=''
126 if not err and weather_now and tonumber(weather_now["cod"]) == 200 then
128 -- weather icon based on localtime
129 now = os.time() - (utc * 3600)
130 sunrise = tonumber(weather_now["sys"]["sunrise"])
131 sunset = tonumber(weather_now["sys"]["sunset"])
132 icon = weather_now["weather"][1]["icon"]
134 if sunrise <= now and now <= sunset then
135 icon = string.gsub(icon, "n", "d")
137 current_dt = os.time()
138 sunrise = weather_now["sys"]["sunrise"]
139 sunset = weather_now["sys"]["sunset"]
140 if current_dt> sunrise and current_dt> sunset then current_dt = current_dt - 86400 end
141 if current_dt > sunrise and current_dt < sunset then
143 >>>>>>> e3a5dd623700b2cad423c8179141124e6e9b9027
145 icon = string.gsub(icon, "d", "n")
149 weather.icon_path = icons_path .. icon .. ".png"
152 -- error("dt sr:" .. sunrise .. "ss: " .. sunset .. "dt: " .. current_dt .. "d/n: " .. datetime .. "hehe")
153 icon = weather_now["weather"][1]["icon"]
154 weather.icon_path = icons_path .. icon:sub(1,2) .. datetime .. ".png"
155 >>>>>>> e3a5dd623700b2cad423c8179141124e6e9b9027
156 widget = weather.widget
157 weather.current_text = "Now:" .. weather_now["weather"][1]["description"] .. "\n"
160 weather.icon_path = icons_path .. "na.png"
161 weather.widget:set_markup(weather_na_markup)
164 weather.icon:set_image(weather.icon_path)
168 weather.attach(weather.widget)
170 newtimer("weather-" .. city_id, timeout, weather.update)
171 newtimer("weather_forecast-" .. city_id, timeout, weather.forecast_update)
173 return setmetatable(weather, { __index = weather.widget })
176 return setmetatable({}, { __call = function(_, ...) return worker(...) end })