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

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