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

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