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 focused = require("awful.screen").focused
17 local naughty = require("naughty")
18 local wibox = require("wibox")
20 local math = { floor = math.floor }
21 local os = { time = os.time,
23 difftime = os.difftime }
24 local string = { format = string.format,
28 local tonumber = tonumber
29 local setmetatable = setmetatable
32 -- current weather and X-days forecast
33 -- lain.widgets.weather
35 local function worker(args)
37 local args = args or {}
38 local APPID = args.APPID or "3e321f9414eaedbfab34983bda77a66e" -- lain default
39 local timeout = args.timeout or 900 -- 15 min
40 local timeout_forecast = args.timeout or 86400 -- 24 hrs
41 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'"
42 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'"
43 local city_id = args.city_id or 0 -- placeholder
44 local utc_offset = args.utc_offset or
47 return os.difftime(now, os.time(os.date("!*t", now))) + ((os.date("*t").isdst and 1 or 0) * 3600)
49 local units = args.units or "metric"
50 local lang = args.lang or "en"
51 local cnt = args.cnt or 5
52 local date_cmd = args.date_cmd or "date -u -d @%d +'%%a %%d'"
53 local icons_path = args.icons_path or lain_icons .. "openweathermap/"
54 local notification_preset = args.notification_preset or {}
55 local notification_text_fun = args.notification_text_fun or
57 local day = string.gsub(read_pipe(string.format(date_cmd, wn["dt"])), "\n", "")
58 local tmin = math.floor(wn["temp"]["min"])
59 local tmax = math.floor(wn["temp"]["max"])
60 local desc = wn["weather"][1]["description"]
61 return string.format("<b>%s</b>: %s, %d - %d ", day, desc, tmin, tmax)
63 local weather_na_markup = args.weather_na_markup or " N/A "
64 local followtag = args.followtag or false
65 local settings = args.settings or function() end
67 weather.widget = wibox.widget.textbox(weather_na_markup)
68 weather.icon_path = icons_path .. "na.png"
69 weather.icon = wibox.widget.imagebox(weather.icon_path)
71 function weather.show(t_out)
75 notification_preset.screen = focused()
78 if not weather.notification_text then
79 weather.forecast_update()
82 weather.notification = naughty.notify({
83 text = weather.notification_text,
84 icon = weather.icon_path,
86 preset = notification_preset
90 function weather.hide()
91 if weather.notification then
92 naughty.destroy(weather.notification)
93 weather.notification = nil
97 function weather.attach(obj)
98 obj:connect_signal("mouse::enter", function()
101 obj:connect_signal("mouse::leave", function()
106 function weather.forecast_update()
107 local cmd = string.format(forecast_call, city_id, units, lang, cnt, APPID)
108 async.request(cmd, function(f)
110 weather_now, pos, err = json.decode(f, 1, nil)
112 if not err and type(weather_now) == "table" and tonumber(weather_now["cod"]) == 200 then
113 weather.notification_text = ''
114 for i = 1, weather_now["cnt"] do
115 weather.notification_text = weather.notification_text ..
116 notification_text_fun(weather_now["list"][i])
118 if i < weather_now["cnt"] then
119 weather.notification_text = weather.notification_text .. "\n"
126 function weather.update()
127 local cmd = string.format(current_call, city_id, units, lang, APPID)
128 async.request(cmd, function(f)
130 weather_now, pos, err = json.decode(f, 1, nil)
132 if not err and type(weather_now) == "table" and tonumber(weather_now["cod"]) == 200 then
133 -- weather icon based on localtime
134 local now = os.time()
135 local sunrise = tonumber(weather_now["sys"]["sunrise"])
136 local sunset = tonumber(weather_now["sys"]["sunset"])
137 local icon = weather_now["weather"][1]["icon"]
138 local utc_m = string.gsub(read_pipe(string.format("date -u -d 'today 00:00:00' +'%%s'")), "\n", "")
139 local loc_m = string.gsub(read_pipe(string.format("date -d 'today 00:00:00' +'%%s'")), "\n", "")
141 loc_m = tonumber(loc_m)
142 utc_m = tonumber(utc_m)
143 offset = utc_offset()
145 -- if we are 1 day after the GMT, return 1 day back, and viceversa
146 if offset > 0 and loc_m >= utc_m then
148 elseif offset < 0 and loc_m <= utc_m then
152 if sunrise <= now and now <= sunset then
153 icon = string.gsub(icon, "n", "d")
155 icon = string.gsub(icon, "d", "n")
158 weather.icon_path = icons_path .. icon .. ".png"
159 widget = weather.widget
162 weather.icon_path = icons_path .. "na.png"
163 weather.widget:set_markup(weather_na_markup)
166 weather.icon:set_image(weather.icon_path)
170 weather.attach(weather.widget)
172 newtimer("weather-" .. city_id, timeout, weather.update)
173 newtimer("weather_forecast-" .. city_id, timeout, weather.forecast_update)
175 return setmetatable(weather, { __index = weather.widget })
178 return setmetatable({}, { __call = function(_, ...) return worker(...) end })