]> git.madduck.net Git - etc/awesome.git/blob - widgets/weather.lua

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

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.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

another attempt to fix weather widget behaviour
[etc/awesome.git] / widgets / weather.lua
1
2 --[[
3                                                   
4      Licensed under GNU General Public License v2 
5       * (c) 2015, Luke Bonham                     
6                                                   
7 --]]
8
9 local newtimer     = require("lain.helpers").newtimer
10 local read_pipe    = require("lain.helpers").read_pipe
11
12 local async        = require("lain.asyncshell")
13 local json         = require("lain.util").dkjson
14 local lain_icons   = require("lain.helpers").icons_dir
15
16 local naughty      = require("naughty")
17 local wibox        = require("wibox")
18
19 local math         = { floor  = math.floor }
20 local mouse        = mouse
21 local os           = { time   = os.time }
22 local string       = { format = string.format,
23                        gsub   = string.gsub }
24 local naughty = require("naughty")
25 local tonumber     = tonumber
26 local setmetatable = setmetatable
27
28 -- OpenWeatherMap
29 -- current weather and X-days forecast
30 -- lain.widgets.weather
31
32 local function worker(args)
33     local weather               = {}
34     local args                  = args or {}
35     local APPID                 = args.APPID or "3e321f9414eaedbfab34983bda77a66e" -- lain default
36     local timeout               = args.timeout or 900   -- 15 min
37     local timeout_forecast      = args.timeout or 86400 -- 24 hrs
38     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'"
39     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'"
40     local city_id               = args.city_id or 0 -- placeholder
41     local utc                   = args.utc or 0
42     local units                 = args.units or "metric"
43     local lang                  = args.lang or "en"
44     local cnt                   = args.cnt or 5
45     local date_cmd              = args.date_cmd or "date -u -d @%d +'%%a %%d'"
46     local icons_path            = args.icons_path or lain_icons .. "openweathermap/"
47     local notification_preset   = args.notification_preset or {}
48     local notification_text_fun = args.notification_text_fun or
49                                   function (wn)
50                                       local day = string.gsub(read_pipe(string.format(date_cmd, wn["dt"])), "\n", "")
51                                       local tmin = math.floor(wn["temp"]["min"])
52                                       local tmax = math.floor(wn["temp"]["max"])
53                                       local desc = wn["weather"][1]["description"]
54
55                                       return string.format("<b>%s</b>: %s, %d - %d ", day, desc, tmin, tmax)
56                                   end
57     local weather_na_markup     = args.weather_na_markup or " N/A "
58     local followmouse           = args.followmouse or false
59     local settings              = args.settings or function() end
60
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)
64
65     function weather.show(t_out)
66         weather.hide()
67
68         if followmouse then
69             notification_preset.screen = mouse.screen
70         end
71
72         if not weather.notification_text then
73             weather.forecast_update()
74         end
75
76
77         weather.notification = naughty.notify({
78             text    = weather.notification_text,
79             icon    = weather.icon_path,
80             timeout = t_out,
81             preset  = notification_preset
82         })
83     end
84
85     function weather.hide()
86         if weather.notification then
87             naughty.destroy(weather.notification)
88             weather.notification = nil
89         end
90     end
91
92     function weather.attach(obj)
93         obj:connect_signal("mouse::enter", function()
94             weather.show(0)
95         end)
96         obj:connect_signal("mouse::leave", function()
97             weather.hide()
98         end)
99     end
100
101     function weather.forecast_update()
102         local cmd = string.format(forecast_call, city_id, units, lang, cnt, APPID)
103         async.request(cmd, function(f)
104             local pos, err
105             weather_now, pos, err = json.decode(f, 1, nil)
106
107             if not err and weather_now and tonumber(weather_now["cod"]) == 200 then
108                 weather.notification_text = ''
109                 for i = 1, weather_now["cnt"] do
110                     weather.notification_text = weather.notification_text ..
111                                                 notification_text_fun(weather_now["list"][i])
112
113                     if i < weather_now["cnt"] then
114                         weather.notification_text = weather.notification_text .. "\n"
115                     end
116                 end
117             end
118         end)
119     end
120
121     function weather.update()
122         local cmd = string.format(current_call, city_id, units, lang, APPID)
123         local utc_midnight_cmd   = string.format("date -u -d 'today 00:00:00' +'%%s'")
124         local local_midnight_cmd = string.format("date -d 'today 00:00:00' +'%%s'")
125
126         async.request(cmd, function(f)
127             local pos, err, icon
128             weather_now, pos, err = json.decode(f, 1, nil)
129
130             if not err and weather_now and tonumber(weather_now["cod"]) == 200 then
131                 -- weather icon based on localtime
132                 now                  = os.time()
133                 local utc_midnight   = string.gsub(read_pipe(utc_midnight_cmd), "\n", "")
134                 local local_midnight = string.gsub(read_pipe(local_midnight_cmd), "\n", "")
135              
136                 if utc > 0 then -- we are to the East from GMT
137                     if tonumber(local_midnight) >= tonumber(utc_midnight) then -- we are 1 day after the GMT, so have to return 1 day back
138                        now = now - 86400
139                     end
140                 end
141
142                 if utc < 0 then -- we are to the West from GMT
143                     if tonumber(local_midnight) <= tonumber(utc_midnight) then -- we are 1 day before the GMT
144                        now = now + 86400
145                     end
146                 end
147                 -- if utc==0 leave it as is
148                 local sunrise = tonumber(weather_now["sys"]["sunrise"])
149                 local sunset  = tonumber(weather_now["sys"]["sunset"])
150                 local icon    = weather_now["weather"][1]["icon"]
151
152                 if sunrise <= now and now <= sunset then
153                     icon = string.gsub(icon, "n", "d")
154                 else
155                     icon = string.gsub(icon, "d", "n")
156                 end
157
158                 weather.icon_path = icons_path .. icon .. ".png"
159
160                 widget = weather.widget
161                 settings()
162             else
163                 weather.icon_path = icons_path .. "na.png"
164                 weather.widget:set_markup(weather_na_markup)
165             end
166
167             weather.icon:set_image(weather.icon_path)
168         end)
169     end
170
171     weather.attach(weather.widget)
172
173     newtimer("weather-" .. city_id, timeout, weather.update)
174     newtimer("weather_forecast-" .. city_id, timeout, weather.forecast_update)
175
176     return setmetatable(weather, { __index = weather.widget })
177 end
178
179 return setmetatable({}, { __call = function(_, ...) return worker(...) end })