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

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