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

make notification text customizable
[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 mouse        = mouse
21 local string       = { format = string.format,
22                        gsub   = string.gsub }
23
24 local setmetatable = setmetatable
25
26 -- OpenWeatherMap
27 -- current weather and X-days forecast
28 -- lain.widgets.weather
29
30 local function worker(args)
31     local weather               = {}
32     local args                  = args or {}
33     local timeout               = args.timeout or 900   -- 15 min
34     local timeout_forecast      = args.timeout or 86400 -- 24 hrs
35     local current_call          = "curl -s 'http://api.openweathermap.org/data/2.5/weather?id=%s&units=%s&lang=%s'"
36     local forecast_call         = "curl -s 'http://api.openweathermap.org/data/2.5/forecast/daily?id=%s&units=%s&lang=%s&cnt=%s'"
37     local city_id               = args.city_id or 0 -- placeholder
38     local units                 = args.units or "metric"
39     local lang                  = args.lang or "en"
40     local cnt                   = args.cnt or 7
41     local date_cmd              = args.date_cmd or "date -u -d @%d +'%%a %%d'"
42     local icons_path            = args.icons_path or lain_icons .. "openweathermap/"
43     local notification_preset   = args.notification_preset or {}
44     local notification_text_cmd = args.notification_text_cmd or
45                                   function (day, desc, tmin, tmax)
46                                       return string.format("<b>%s</b>: %s, %d - %d  ", day, desc, tmin, tmax)
47                                   end
48     local followmouse           = args.followmouse or false
49     local settings              = args.settings or function() end
50
51     weather.widget = wibox.widget.textbox('')
52     weather.icon   = wibox.widget.imagebox()
53
54     function weather.show(t_out)
55         weather.hide()
56
57         if followmouse then
58             notification_preset.screen = mouse.screen
59         end
60
61         weather.notification = naughty.notify({
62             text    = weather.notification_text,
63             icon    = weather.icon_path,
64             timeout = t_out,
65             preset  = notification_preset
66         })
67     end
68
69     function weather.hide()
70         if weather.notification ~= nil then
71             naughty.destroy(weather.notification)
72             weather.notification = nil
73         end
74     end
75
76     function weather.attach(obj)
77         obj:connect_signal("mouse::enter", function()
78             weather.show(0)
79         end)
80         obj:connect_signal("mouse::leave", function()
81             weather.hide()
82         end)
83     end
84
85     function weather.forecast_update()
86         local cmd = string.format(forecast_call, city_id, units, lang, cnt)
87         async.request(cmd, function(f)
88             weather_now, pos, err = json.decode(f, 1, nil)
89
90             if not err and weather_now ~= nil and tonumber(weather_now["cod"]) == 200 then
91                 weather.notification_text = ''
92                 for i = 1, weather_now["cnt"] do
93                     day = string.gsub(read_pipe(string.format(date_cmd, weather_now["list"][i]["dt"])), "\n", "")
94
95                     tmin = math.floor(weather_now["list"][i]["temp"]["min"])
96                     tmax = math.floor(weather_now["list"][i]["temp"]["max"])
97                     desc = weather_now["list"][i]["weather"][1]["description"]
98
99                     weather.notification_text = weather.notification_text ..
100                                                 notification_text_cmd(day, desc, tmin, tmax)
101
102                     if i < weather_now["cnt"] then
103                         weather.notification_text = weather.notification_text .. "\n"
104                     end
105                 end
106             else
107                 weather.icon_path = icons_path .. "na.png"
108                 weather.notification_text = "API/connection error or bad/not set city ID"
109             end
110         end)
111     end
112
113     function weather.update()
114         local cmd = string.format(current_call, city_id, units, lang)
115         async.request(cmd, function(f)
116             weather_now, pos, err = json.decode(f, 1, nil)
117
118             if not err and weather_now ~= nil and tonumber(weather_now["cod"]) == 200 then
119                 weather.icon_path = icons_path .. weather_now["weather"][1]["icon"] .. ".png"
120                 weather.icon:set_image(weather.icon_path)
121                 widget = weather.widget
122                 settings()
123             else
124                 weather.widget._layout.text = " N/A " -- tries to avoid textbox bugs
125                 weather.icon:set_image(icons_path .. "na.png")
126             end
127         end)
128     end
129
130     weather.attach(weather.widget)
131
132     newtimer("weather-" .. city_id, timeout, weather.update)
133     newtimer("weather_forecast" .. city_id, timeout, weather.forecast_update)
134
135     return setmetatable(weather, { __index = weather.widget })
136 end
137
138 return setmetatable({}, { __call = function(_, ...) return worker(...) end })