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

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