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 helpers = require("lain.helpers")
10 local json = require("lain.util").dkjson
11 local focused = require("awful.screen").focused
12 local naughty = require("naughty")
13 local wibox = require("wibox")
15 local math, os, string, tonumber = math, os, string, tonumber
18 -- current weather and X-days forecast
19 -- lain.widget.weather
21 local function factory(args)
22 local weather = { widget = wibox.widget.textbox() }
23 local args = args or {}
24 local APPID = args.APPID or "3e321f9414eaedbfab34983bda77a66e" -- lain default
25 local timeout = args.timeout or 60 * 15 -- 15 min
26 local timeout_forecast = args.timeout or 60 * 60 * 24 -- 24 hrs
27 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'"
28 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'"
29 local city_id = args.city_id or 0 -- placeholder
30 local utc_offset = args.utc_offset or
33 return os.difftime(now, os.time(os.date("!*t", now))) + ((os.date("*t").isdst and 1 or 0) * 3600)
35 local units = args.units or "metric"
36 local lang = args.lang or "en"
37 local cnt = args.cnt or 5
38 local date_cmd = args.date_cmd or "date -u -d @%d +'%%a %%d'"
39 local icons_path = args.icons_path or helpers.icons_dir .. "openweathermap/"
40 local notification_preset = args.notification_preset or {}
41 local notification_text_fun = args.notification_text_fun or
43 local day = os.date("%a %d", wn["dt"])
44 local tmin = math.floor(wn["temp"]["min"])
45 local tmax = math.floor(wn["temp"]["max"])
46 local desc = wn["weather"][1]["description"]
47 return string.format("<b>%s</b>: %s, %d - %d ", day, desc, tmin, tmax)
49 local weather_na_markup = args.weather_na_markup or " N/A "
50 local followtag = args.followtag or false
51 local showpopup = args.showpopup or "on"
52 local settings = args.settings or function() end
54 weather.widget:set_markup(weather_na_markup)
55 weather.icon_path = icons_path .. "na.png"
56 weather.icon = wibox.widget.imagebox(weather.icon_path)
58 function weather.show(t_out)
62 notification_preset.screen = focused()
65 if not weather.notification_text then
67 weather.forecast_update()
70 weather.notification = naughty.notify({
71 text = weather.notification_text,
72 icon = weather.icon_path,
74 preset = notification_preset
78 function weather.hide()
79 if weather.notification then
80 naughty.destroy(weather.notification)
81 weather.notification = nil
85 function weather.attach(obj)
86 obj:connect_signal("mouse::enter", function()
89 obj:connect_signal("mouse::leave", function()
94 function weather.forecast_update()
95 local cmd = string.format(forecast_call, city_id, units, lang, cnt, APPID)
96 helpers.async(cmd, function(f)
98 weather_now, pos, err = json.decode(f, 1, nil)
100 if not err and type(weather_now) == "table" and tonumber(weather_now["cod"]) == 200 then
101 weather.notification_text = ""
102 for i = 1, weather_now["cnt"] do
103 weather.notification_text = weather.notification_text ..
104 notification_text_fun(weather_now["list"][i])
105 if i < weather_now["cnt"] then
106 weather.notification_text = weather.notification_text .. "\n"
113 function weather.update()
114 local cmd = string.format(current_call, city_id, units, lang, APPID)
115 helpers.async(cmd, function(f)
117 weather_now, pos, err = json.decode(f, 1, nil)
119 if not err and type(weather_now) == "table" and tonumber(weather_now["cod"]) == 200 then
120 local sunrise = tonumber(weather_now["sys"]["sunrise"])
121 local sunset = tonumber(weather_now["sys"]["sunset"])
122 local icon = weather_now["weather"][1]["icon"]
123 local now = os.time()
124 local loc_m = os.time { year = os.date("%Y"), month = os.date("%m"), day = os.date("%d"), hour = 0 }
125 local offset = utc_offset()
126 local utc_m = loc_m - offset
128 -- if we are 1 day after the GMT, return 1 day back, and viceversa
129 if offset > 0 and (now - utc_m) >= 86400 then
130 utc_m = utc_m + 86400
131 elseif offset < 0 and (utc_m - now) >= 86400 then
132 utc_m = utc_m - 86400
135 if sunrise <= now and now <= sunset then
136 icon = string.gsub(icon, "n", "d")
138 icon = string.gsub(icon, "d", "n")
141 weather.icon_path = icons_path .. icon .. ".png"
142 widget = weather.widget
145 weather.icon_path = icons_path .. "na.png"
146 weather.widget:set_markup(weather_na_markup)
149 weather.icon:set_image(weather.icon_path)
153 if showpopup == "on" then weather.attach(weather.widget) end
155 weather.timer = helpers.newtimer("weather-" .. city_id, timeout, weather.update, false, true)
156 weather.timer_forecast = helpers.newtimer("weather_forecast-" .. city_id, timeout, weather.forecast_update, false, true)