]> git.madduck.net Git - etc/awesome.git/blob - widget/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:

b2ccc192d6df52ddd9c387b7d233ee3b09f226d7
[etc/awesome.git] / widget / weather.lua
1
2 --[[
3                                                   
4      Licensed under GNU General Public License v2 
5       * (c) 2015, Luke Bonham                     
6                                                   
7 --]]
8
9 local helpers  = require("lain.helpers")
10 local json     = require("lain.util").dkjson
11 local focused  = require("awful.screen").focused
12 local naughty  = require("naughty")
13 local wibox    = require("wibox")
14
15 local math, os, string, tonumber = math, os, string, tonumber
16
17 -- OpenWeatherMap
18 -- current weather and X-days forecast
19 -- lain.widget.weather
20
21 local function factory(args)
22     local weather               = { widget = wibox.widget.textbox() }
23     local args                  = args or {}
24     local APPID                 = args.APPID or "3e321f9414eaedbfab34983bda77a66e" -- lain default
25     local timeout               = args.timeout or 60 * 15 -- 15 min
26     local timeout_forecast      = args.timeout or 60 * 60 * 24 -- 24 hrs
27     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'"
28     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'"
29     local city_id               = args.city_id or 0 -- placeholder
30     local utc_offset            = args.utc_offset or
31                                   function ()
32                                       local now = os.time()
33                                       return os.difftime(now, os.time(os.date("!*t", now))) + ((os.date("*t").isdst and 1 or 0) * 3600)
34                                   end
35     local units                 = args.units or "metric"
36     local lang                  = args.lang or "en"
37     local cnt                   = args.cnt or 5
38     local date_cmd              = args.date_cmd or "date -u -d @%d +'%%a %%d'"
39     local icons_path            = args.icons_path or helpers.icons_dir .. "openweathermap/"
40     local notification_preset   = args.notification_preset or {}
41     local notification_text_fun = args.notification_text_fun or
42                                   function (wn)
43                                       local day = os.date("%a %d", wn["dt"])
44                                       local tmin = math.floor(wn["temp"]["min"])
45                                       local tmax = math.floor(wn["temp"]["max"])
46                                       local desc = wn["weather"][1]["description"]
47                                       return string.format("<b>%s</b>: %s, %d - %d ", day, desc, tmin, tmax)
48                                   end
49     local weather_na_markup     = args.weather_na_markup or " N/A "
50     local followtag             = args.followtag or false
51     local showpopup             = args.showpopup or "on"
52     local settings              = args.settings or function() end
53
54     weather.widget:set_markup(weather_na_markup)
55     weather.icon_path = icons_path .. "na.png"
56     weather.icon = wibox.widget.imagebox(weather.icon_path)
57
58     function weather.show(t_out)
59         weather.hide()
60
61         if followtag then
62             notification_preset.screen = focused()
63         end
64
65         if not weather.notification_text then
66             weather.update()
67             weather.forecast_update()
68         end
69
70         weather.notification = naughty.notify({
71             text    = weather.notification_text,
72             icon    = weather.icon_path,
73             timeout = t_out,
74             preset  = notification_preset
75         })
76     end
77
78     function weather.hide()
79         if weather.notification then
80             naughty.destroy(weather.notification)
81             weather.notification = nil
82         end
83     end
84
85     function weather.attach(obj)
86         obj:connect_signal("mouse::enter", function()
87             weather.show(0)
88         end)
89         obj:connect_signal("mouse::leave", function()
90             weather.hide()
91         end)
92     end
93
94     function weather.forecast_update()
95         local cmd = string.format(forecast_call, city_id, units, lang, cnt, APPID)
96         helpers.async(cmd, function(f)
97             local pos, err
98             weather_now, pos, err = json.decode(f, 1, nil)
99
100             if not err and type(weather_now) == "table" and tonumber(weather_now["cod"]) == 200 then
101                 weather.notification_text = ""
102                 for i = 1, weather_now["cnt"] do
103                     weather.notification_text = weather.notification_text ..
104                                                 notification_text_fun(weather_now["list"][i])
105                     if i < weather_now["cnt"] then
106                         weather.notification_text = weather.notification_text .. "\n"
107                     end
108                 end
109             end
110         end)
111     end
112
113     function weather.update()
114         local cmd = string.format(current_call, city_id, units, lang, APPID)
115         helpers.async(cmd, function(f)
116             local pos, err, icon
117             weather_now, pos, err = json.decode(f, 1, nil)
118
119             if not err and type(weather_now) == "table" and tonumber(weather_now["cod"]) == 200 then
120                 local sunrise = tonumber(weather_now["sys"]["sunrise"])
121                 local sunset  = tonumber(weather_now["sys"]["sunset"])
122                 local icon    = weather_now["weather"][1]["icon"]
123                 local now     = os.time()
124                 local loc_m   = os.time { year = os.date("%Y"), month = os.date("%m"), day = os.date("%d"), hour = 0 }
125                 local offset  = utc_offset()
126                 local utc_m   = loc_m - offset
127
128                 -- if we are 1 day after the GMT, return 1 day back, and viceversa
129                 if offset > 0 and (now - utc_m) >= 86400 then
130                     utc_m = utc_m + 86400
131                 elseif offset < 0 and (utc_m - now) >= 86400 then
132                     utc_m = utc_m - 86400
133                 end
134
135                 if sunrise <= now and now <= sunset then
136                     icon = string.gsub(icon, "n", "d")
137                 else
138                     icon = string.gsub(icon, "d", "n")
139                 end
140
141                 weather.icon_path = icons_path .. icon .. ".png"
142                 widget = weather.widget
143                 settings()
144             else
145                 weather.icon_path = icons_path .. "na.png"
146                 weather.widget:set_markup(weather_na_markup)
147             end
148
149             weather.icon:set_image(weather.icon_path)
150         end)
151     end
152
153     if showpopup == "on" then weather.attach(weather.widget) end
154
155     weather.timer = helpers.newtimer("weather-" .. city_id, timeout, weather.update, false, true)
156     weather.timer_forecast = helpers.newtimer("weather_forecast-" .. city_id, timeout, weather.forecast_update, false, true)
157
158     return weather
159 end
160
161 return factory