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 async = require("lain.helpers").async
10 local newtimer = require("lain.helpers").newtimer
11 local lain_icons = require("lain.helpers").icons_dir
12 local json = require("lain.util").dkjson
13 local focused = require("awful.screen").focused
14 local naughty = require("naughty")
15 local wibox = require("wibox")
16 local math = { floor = math.floor }
17 local os = { time = os.time,
19 difftime = os.difftime }
20 local string = { format = string.format,
22 local tonumber = tonumber
23 local setmetatable = setmetatable
26 -- current weather and X-days forecast
27 -- lain.widgets.weather
29 local function worker(args)
31 local args = args or {}
32 local APPID = args.APPID or "3e321f9414eaedbfab34983bda77a66e" -- lain default
33 local timeout = args.timeout or 900 -- 15 min
34 local timeout_forecast = args.timeout or 86400 -- 24 hrs
35 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'"
36 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'"
37 local city_id = args.city_id or 0 -- placeholder
38 local utc_offset = args.utc_offset or
41 return os.difftime(now, os.time(os.date("!*t", now))) + ((os.date("*t").isdst and 1 or 0) * 3600)
43 local units = args.units or "metric"
44 local lang = args.lang or "en"
45 local cnt = args.cnt or 5
46 local date_cmd = args.date_cmd or "date -u -d @%d +'%%a %%d'"
47 local icons_path = args.icons_path or lain_icons .. "openweathermap/"
48 local notification_preset = args.notification_preset or {}
49 local notification_text_fun = args.notification_text_fun or
51 local day = os.date("%a %d", wn["dt"])
52 local tmin = math.floor(wn["temp"]["min"])
53 local tmax = math.floor(wn["temp"]["max"])
54 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 followtag = args.followtag 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 = focused()
72 if not weather.notification_text then
73 weather.forecast_update()
76 weather.notification = naughty.notify({
77 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(cmd, function(f)
104 weather_now, pos, err = json.decode(f, 1, nil)
106 if not err and type(weather_now) == "table" 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(cmd, function(f)
124 weather_now, pos, err = json.decode(f, 1, nil)
126 if not err and type(weather_now) == "table" and tonumber(weather_now["cod"]) == 200 then
127 -- weather icon based on localtime
128 local now = os.time()
129 local sunrise = tonumber(weather_now["sys"]["sunrise"])
130 local sunset = tonumber(weather_now["sys"]["sunset"])
131 local icon = weather_now["weather"][1]["icon"]
132 local loc_m = os.time { year = os.date("%Y"), month = os.date("%m"), day = os.date("%d"), hour = 0 }
133 local offset = utc_offset()
134 local utc_m = loc_m + offset
136 -- if we are 1 day after the GMT, return 1 day back, and viceversa
137 if offset > 0 and loc_m >= utc_m then
139 elseif offset < 0 and loc_m <= utc_m then
143 if sunrise <= now and now <= sunset then
144 icon = string.gsub(icon, "n", "d")
146 icon = string.gsub(icon, "d", "n")
149 weather.icon_path = icons_path .. icon .. ".png"
150 widget = weather.widget
153 weather.icon_path = icons_path .. "na.png"
154 weather.widget:set_markup(weather_na_markup)
157 weather.icon:set_image(weather.icon_path)
161 weather.attach(weather.widget)
163 newtimer("weather-" .. city_id, timeout, weather.update)
164 newtimer("weather_forecast-" .. city_id, timeout, weather.forecast_update)
166 return setmetatable(weather, { __index = weather.widget })
169 return setmetatable({}, { __call = function(_, ...) return worker(...) end })