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,
24 local naughty = require("naughty")
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()
77 weather.notification = naughty.notify({
78 text = weather.notification_text,
79 icon = weather.icon_path,
81 preset = notification_preset
85 function weather.hide()
86 if weather.notification then
87 naughty.destroy(weather.notification)
88 weather.notification = nil
92 function weather.attach(obj)
93 obj:connect_signal("mouse::enter", function()
96 obj:connect_signal("mouse::leave", function()
101 function weather.forecast_update()
102 local cmd = string.format(forecast_call, city_id, units, lang, cnt, APPID)
103 async.request(cmd, function(f)
105 weather_now, pos, err = json.decode(f, 1, nil)
107 if not err and weather_now and tonumber(weather_now["cod"]) == 200 then
108 weather.notification_text = ''
109 for i = 1, weather_now["cnt"] do
110 weather.notification_text = weather.notification_text ..
111 notification_text_fun(weather_now["list"][i])
113 if i < weather_now["cnt"] then
114 weather.notification_text = weather.notification_text .. "\n"
121 function weather.update()
122 local cmd = string.format(current_call, city_id, units, lang, APPID)
123 async.request(cmd, function(f)
125 weather_now, pos, err = json.decode(f, 1, nil)
127 if not err and weather_now and tonumber(weather_now["cod"]) == 200 then
128 -- weather icon based on localtime
129 local now = os.time()
130 local sunrise = tonumber(weather_now["sys"]["sunrise"])
131 local sunset = tonumber(weather_now["sys"]["sunset"])
132 local icon = weather_now["weather"][1]["icon"]
133 local utc_m = string.gsub(read_pipe(string.format("date -u -d 'today 00:00:00' +'%%s'")), "\n", "")
135 if now > tonumber(utc_m) then
136 now = now - (utc * 3600)
139 if sunrise <= now and now <= sunset then
140 icon = string.gsub(icon, "n", "d")
142 icon = string.gsub(icon, "d", "n")
145 weather.icon_path = icons_path .. icon .. ".png"
147 widget = weather.widget
150 weather.icon_path = icons_path .. "na.png"
151 weather.widget:set_markup(weather_na_markup)
154 weather.icon:set_image(weather.icon_path)
158 weather.attach(weather.widget)
160 newtimer("weather-" .. city_id, timeout, weather.update)
161 newtimer("weather_forecast-" .. city_id, timeout, weather.forecast_update)
163 return setmetatable(weather, { __index = weather.widget })
166 return setmetatable({}, { __call = function(_, ...) return worker(...) end })