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

modified: widgets/weather.lua - added current weather description to notification
[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 APPID                 = args.APPID or "3e321f9414eaedbfab34983bda77a66e" -- lain default
34     local timeout               = args.timeout or 900   -- 15 min
35     local timeout_forecast      = args.timeout or 86400 -- 24 hrs
36     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'"
37     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'"
38     local city_id               = args.city_id or 0 -- placeholder
39     local units                 = args.units or "metric"
40     local lang                  = args.lang or "en"
41     local cnt                   = args.cnt or 5
42     local date_cmd              = args.date_cmd or "date -u -d @%d +'%%a %%d'"
43     local icons_path            = args.icons_path or lain_icons .. "openweathermap/"
44     local notification_preset   = args.notification_preset or {}
45     local notification_text_fun = args.notification_text_fun or
46                                   function (wn)
47                                       local day = string.gsub(read_pipe(string.format(date_cmd, wn["dt"])), "\n", "")
48                                       local tmin = math.floor(wn["temp"]["min"])
49                                       local tmax = math.floor(wn["temp"]["max"])
50                                       local desc = wn["weather"][1]["description"]
51
52                                       return string.format("<b>%s</b>: %s, %d - %d ", day, desc, tmin, tmax)
53                                   end
54     local weather_na_markup     = args.weather_na_markup or " N/A "
55     local followmouse           = args.followmouse or false
56     local settings              = args.settings or function() end
57
58     weather.widget    = wibox.widget.textbox(weather_na_markup)
59     weather.icon_path = icons_path .. "na.png"
60     weather.icon      = wibox.widget.imagebox(weather.icon_path)
61
62     function weather.show(t_out)
63         weather.hide()
64
65         if followmouse then
66             notification_preset.screen = mouse.screen
67         end
68
69         if not weather.notification_text then
70             weather.forecast_update()
71         end
72
73         if not weather.current_text then
74             weather.update()
75         end
76
77         weather.notification = naughty.notify({
78             text    = weather.current_text .. weather.notification_text,
79             icon    = weather.icon_path,
80             timeout = t_out,
81             preset  = notification_preset
82         })
83     end
84
85     function weather.hide()
86         if weather.notification then
87             naughty.destroy(weather.notification)
88             weather.notification = nil
89         end
90     end
91
92     function weather.attach(obj)
93         obj:connect_signal("mouse::enter", function()
94             weather.show(0)
95         end)
96         obj:connect_signal("mouse::leave", function()
97             weather.hide()
98         end)
99     end
100
101     function weather.forecast_update()
102         local cmd = string.format(forecast_call, city_id, units, lang, cnt, APPID)
103         async.request(cmd, function(f)
104             local pos, err
105             weather_now, pos, err = json.decode(f, 1, nil)
106
107             if not err and weather_now and tonumber(weather_now["cod"]) == 200 then
108                 weather.notification_text = ''
109                 for i = 1, weather_now["cnt"] do
110                     weather.notification_text = weather.notification_text ..
111                                                 notification_text_fun(weather_now["list"][i])
112
113                     if i < weather_now["cnt"] then
114                         weather.notification_text = weather.notification_text .. "\n"
115                     end
116                 end
117             end
118         end)
119     end
120
121     function weather.update()
122         local cmd = string.format(current_call, city_id, units, lang, APPID)
123         async.request(cmd, function(f)
124             local pos, err, sunrise, sunset, current_dt, datetime, icon
125             weather_now, pos, err = json.decode(f, 1, nil)
126             weather.current_text=''
127             if not err and weather_now and tonumber(weather_now["cod"]) == 200 then
128                 current_dt = os.time()
129                 sunrise = weather_now["sys"]["sunrise"]
130                 sunset  = weather_now["sys"]["sunset"]
131                 if current_dt > sunrise and current_dt < sunset then 
132                     datetime="d"
133                 else
134                     datetime="n"
135                 end
136                 icon = weather_now["weather"][1]["icon"]
137                 weather.icon_path = icons_path .. icon:sub(1,2) .. datetime .. ".png"
138                 widget = weather.widget
139                 weather.current_text = "Now:" .. weather_now["weather"][1]["description"] .. "\n"
140                 settings()
141             else
142                 weather.icon_path = icons_path .. "na.png"
143                 weather.widget:set_markup(weather_na_markup)
144             end
145
146             weather.icon:set_image(weather.icon_path)
147         end)
148     end
149
150     weather.attach(weather.widget)
151
152     newtimer("weather-" .. city_id, timeout, weather.update)
153     newtimer("weather_forecast-" .. city_id, timeout, weather.forecast_update)
154
155     return setmetatable(weather, { __index = weather.widget })
156 end
157
158 return setmetatable({}, { __call = function(_, ...) return worker(...) end })