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
14 local focused = require("awful.screen").focused
15 local naughty = require("naughty")
16 local wibox = require("wibox")
18 local math = { floor = math.floor }
19 local os = { time = os.time,
21 difftime = os.difftime }
22 local string = { format = string.format,
26 local tonumber = tonumber
27 local setmetatable = setmetatable
30 -- current weather and X-days forecast
31 -- lain.widgets.weather
33 local function worker(args)
35 local args = args or {}
36 local APPID = args.APPID or "3e321f9414eaedbfab34983bda77a66e" -- lain default
37 local timeout = args.timeout or 900 -- 15 min
38 local timeout_forecast = args.timeout or 86400 -- 24 hrs
39 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'"
40 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'"
41 local city_id = args.city_id or 0 -- placeholder
42 local utc_offset = args.utc_offset or
45 return os.difftime(now, os.time(os.date("!*t", now))) + ((os.date("*t").isdst and 1 or 0) * 3600)
47 local units = args.units or "metric"
48 local lang = args.lang or "en"
49 local cnt = args.cnt or 5
50 local date_cmd = args.date_cmd or "date -u -d @%d +'%%a %%d'"
51 local icons_path = args.icons_path or lain_icons .. "openweathermap/"
52 local notification_preset = args.notification_preset or {}
53 local notification_text_fun = args.notification_text_fun or
55 local day = os.date("%a %d", wn["dt"])
56 local tmin = math.floor(wn["temp"]["min"])
57 local tmax = math.floor(wn["temp"]["max"])
58 local desc = wn["weather"][1]["description"]
59 return string.format("<b>%s</b>: %s, %d - %d ", day, desc, tmin, tmax)
61 local weather_na_markup = args.weather_na_markup or " N/A "
62 local followtag = args.followtag or false
63 local settings = args.settings or function() end
65 weather.widget = wibox.widget.textbox(weather_na_markup)
66 weather.icon_path = icons_path .. "na.png"
67 weather.icon = wibox.widget.imagebox(weather.icon_path)
69 function weather.show(t_out)
73 notification_preset.screen = focused()
76 if not weather.notification_text then
77 weather.forecast_update()
80 weather.notification = naughty.notify({
81 text = weather.notification_text,
82 icon = weather.icon_path,
84 preset = notification_preset
88 function weather.hide()
89 if weather.notification then
90 naughty.destroy(weather.notification)
91 weather.notification = nil
95 function weather.attach(obj)
96 obj:connect_signal("mouse::enter", function()
99 obj:connect_signal("mouse::leave", function()
104 function weather.forecast_update()
105 local cmd = string.format(forecast_call, city_id, units, lang, cnt, APPID)
106 async(cmd, function(f)
108 weather_now, pos, err = json.decode(f, 1, nil)
110 if not err and type(weather_now) == "table" and tonumber(weather_now["cod"]) == 200 then
111 weather.notification_text = ''
112 for i = 1, weather_now["cnt"] do
113 weather.notification_text = weather.notification_text ..
114 notification_text_fun(weather_now["list"][i])
116 if i < weather_now["cnt"] then
117 weather.notification_text = weather.notification_text .. "\n"
124 function weather.update()
125 local cmd = string.format(current_call, city_id, units, lang, APPID)
126 async(cmd, function(f)
128 weather_now, pos, err = json.decode(f, 1, nil)
130 if not err and type(weather_now) == "table" and tonumber(weather_now["cod"]) == 200 then
131 -- weather icon based on localtime
132 local now = os.time()
133 local sunrise = tonumber(weather_now["sys"]["sunrise"])
134 local sunset = tonumber(weather_now["sys"]["sunset"])
135 local icon = weather_now["weather"][1]["icon"]
136 local loc_m = os.time { year = os.date("%Y"), month = os.date("%m"), day = os.date("%d"), hour = 0 }
137 local offset = utc_offset()
138 local utc_m = loc_m + offset
140 -- if we are 1 day after the GMT, return 1 day back, and viceversa
141 if offset > 0 and loc_m >= utc_m then
143 elseif offset < 0 and loc_m <= utc_m then
147 if sunrise <= now and now <= sunset then
148 icon = string.gsub(icon, "n", "d")
150 icon = string.gsub(icon, "d", "n")
153 weather.icon_path = icons_path .. icon .. ".png"
154 widget = weather.widget
157 weather.icon_path = icons_path .. "na.png"
158 weather.widget:set_markup(weather_na_markup)
161 weather.icon:set_image(weather.icon_path)
165 weather.attach(weather.widget)
167 newtimer("weather-" .. city_id, timeout, weather.update)
168 newtimer("weather_forecast-" .. city_id, timeout, weather.forecast_update)
170 return setmetatable(weather, { __index = weather.widget })
173 return setmetatable({}, { __call = function(_, ...) return worker(...) end })