]> 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:

17b3d3463737ef338b79ed8ceeece3b873a4cb73
[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 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
14 local focused      = require("awful.screen").focused
15 local naughty      = require("naughty")
16 local wibox        = require("wibox")
17
18 local math         = { floor    = math.floor }
19 local os           = { time     = os.time,
20                        date     = os.date,
21                        difftime = os.difftime }
22 local string       = { format   = string.format,
23                        gsub     = string.gsub }
24
25 local mouse        = mouse
26 local tonumber     = tonumber
27 local setmetatable = setmetatable
28
29 -- OpenWeatherMap
30 -- current weather and X-days forecast
31 -- lain.widgets.weather
32
33 local function worker(args)
34     local weather               = {}
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
43                                   function ()
44                                       local now = os.time()
45                                       return os.difftime(now, os.time(os.date("!*t", now))) + ((os.date("*t").isdst and 1 or 0) * 3600)
46                                   end
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
54                                   function (wn)
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)
60                                   end
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
64
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)
68
69     function weather.show(t_out)
70         weather.hide()
71
72         if followtag then
73             notification_preset.screen = focused()
74         end
75
76         if not weather.notification_text then
77             weather.forecast_update()
78         end
79
80         weather.notification = naughty.notify({
81             text    = weather.notification_text,
82             icon    = weather.icon_path,
83             timeout = t_out,
84             preset  = notification_preset
85         })
86     end
87
88     function weather.hide()
89         if weather.notification then
90             naughty.destroy(weather.notification)
91             weather.notification = nil
92         end
93     end
94
95     function weather.attach(obj)
96         obj:connect_signal("mouse::enter", function()
97             weather.show(0)
98         end)
99         obj:connect_signal("mouse::leave", function()
100             weather.hide()
101         end)
102     end
103
104     function weather.forecast_update()
105         local cmd = string.format(forecast_call, city_id, units, lang, cnt, APPID)
106         async(cmd, function(f)
107             local pos, err
108             weather_now, pos, err = json.decode(f, 1, nil)
109
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])
115
116                     if i < weather_now["cnt"] then
117                         weather.notification_text = weather.notification_text .. "\n"
118                     end
119                 end
120             end
121         end)
122     end
123
124     function weather.update()
125         local cmd = string.format(current_call, city_id, units, lang, APPID)
126         async(cmd, function(f)
127             local pos, err, icon
128             weather_now, pos, err = json.decode(f, 1, nil)
129
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
139
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
142                     now = now - 86400
143                 elseif offset < 0 and loc_m <= utc_m then
144                     now = now + 86400
145                 end
146
147                 if sunrise <= now and now <= sunset then
148                     icon = string.gsub(icon, "n", "d")
149                 else
150                     icon = string.gsub(icon, "d", "n")
151                 end
152
153                 weather.icon_path = icons_path .. icon .. ".png"
154                 widget = weather.widget
155                 settings()
156             else
157                 weather.icon_path = icons_path .. "na.png"
158                 weather.widget:set_markup(weather_na_markup)
159             end
160
161             weather.icon:set_image(weather.icon_path)
162         end)
163     end
164
165     weather.attach(weather.widget)
166
167     newtimer("weather-" .. city_id, timeout, weather.update)
168     newtimer("weather_forecast-" .. city_id, timeout, weather.forecast_update)
169
170     return setmetatable(weather, { __index = weather.widget })
171 end
172
173 return setmetatable({}, { __call = function(_, ...) return worker(...) end })