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

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