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 }
20 local os = { time = os.time,
22 difftime = os.difftime }
23 local string = { format = string.format,
27 local tonumber = tonumber
28 local setmetatable = setmetatable
31 -- current weather and X-days forecast
32 -- lain.widgets.weather
34 local function worker(args)
36 local args = args or {}
37 local APPID = args.APPID or "3e321f9414eaedbfab34983bda77a66e" -- lain default
38 local timeout = args.timeout or 900 -- 15 min
39 local timeout_forecast = args.timeout or 86400 -- 24 hrs
40 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'"
41 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'"
42 local city_id = args.city_id or 0 -- placeholder
43 local utc_offset = args.utc_offset or
46 return os.difftime(now, os.time(os.date("!*t", now))) + ((os.date("*t").isdst and 1 or 0) * 3600)
48 local units = args.units or "metric"
49 local lang = args.lang or "en"
50 local cnt = args.cnt or 5
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 notification_preset = args.notification_preset or {}
54 local notification_text_fun = args.notification_text_fun or
56 local day = string.gsub(read_pipe(string.format(date_cmd, wn["dt"])), "\n", "")
57 local tmin = math.floor(wn["temp"]["min"])
58 local tmax = math.floor(wn["temp"]["max"])
59 local desc = wn["weather"][1]["description"]
60 return string.format("<b>%s</b>: %s, %d - %d ", day, desc, tmin, tmax)
62 local weather_na_markup = args.weather_na_markup or " N/A "
63 local followmouse = args.followmouse or false
64 local settings = args.settings or function() end
66 weather.widget = wibox.widget.textbox(weather_na_markup)
67 weather.icon_path = icons_path .. "na.png"
68 weather.icon = wibox.widget.imagebox(weather.icon_path)
70 function weather.show(t_out)
74 notification_preset.screen = mouse.screen
77 if not weather.notification_text then
78 weather.forecast_update()
81 weather.notification = naughty.notify({
82 text = weather.notification_text,
83 icon = weather.icon_path,
85 preset = notification_preset
89 function weather.hide()
90 if weather.notification then
91 naughty.destroy(weather.notification)
92 weather.notification = nil
96 function weather.attach(obj)
97 obj:connect_signal("mouse::enter", function()
100 obj:connect_signal("mouse::leave", function()
105 function weather.forecast_update()
106 local cmd = string.format(forecast_call, city_id, units, lang, cnt, APPID)
107 async.request(cmd, function(f)
109 weather_now, pos, err = json.decode(f, 1, nil)
111 if not err and type(weather_now) == "table" and tonumber(weather_now["cod"]) == 200 then
112 weather.notification_text = ''
113 for i = 1, weather_now["cnt"] do
114 weather.notification_text = weather.notification_text ..
115 notification_text_fun(weather_now["list"][i])
117 if i < weather_now["cnt"] then
118 weather.notification_text = weather.notification_text .. "\n"
125 function weather.update()
126 local cmd = string.format(current_call, city_id, units, lang, APPID)
127 async.request(cmd, function(f)
129 weather_now, pos, err = json.decode(f, 1, nil)
131 if not err and type(weather_now) == "table" and tonumber(weather_now["cod"]) == 200 then
132 -- weather icon based on localtime
133 local now = os.time()
134 local sunrise = tonumber(weather_now["sys"]["sunrise"])
135 local sunset = tonumber(weather_now["sys"]["sunset"])
136 local icon = weather_now["weather"][1]["icon"]
137 local utc_m = string.gsub(read_pipe(string.format("date -u -d 'today 00:00:00' +'%%s'")), "\n", "")
138 local loc_m = string.gsub(read_pipe(string.format("date -d 'today 00:00:00' +'%%s'")), "\n", "")
140 loc_m = tonumber(loc_m)
141 utc_m = tonumber(utc_m)
142 offset = utc_offset()
144 -- if we are 1 day after the GMT, return 1 day back, and viceversa
145 if offset > 0 and loc_m >= utc_m then
147 elseif offset < 0 and loc_m <= utc_m then
151 if sunrise <= now and now <= sunset then
152 icon = string.gsub(icon, "n", "d")
154 icon = string.gsub(icon, "d", "n")
157 weather.icon_path = icons_path .. icon .. ".png"
158 widget = weather.widget
161 weather.icon_path = icons_path .. "na.png"
162 weather.widget:set_markup(weather_na_markup)
165 weather.icon:set_image(weather.icon_path)
169 weather.attach(weather.widget)
171 newtimer("weather-" .. city_id, timeout, weather.update)
172 newtimer("weather_forecast-" .. city_id, timeout, weather.forecast_update)
174 return setmetatable(weather, { __index = weather.widget })
177 return setmetatable({}, { __call = function(_, ...) return worker(...) end })