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

Merge pull request #176 from thinusbaun/any_year_in_calendar
[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          = "curl -s 'http://api.openweathermap.org/data/2.5/weather?id=%s&units=%s&lang=%s&APPID=%s'"
37     local forecast_call         = "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 7
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 (day, desc, tmin, tmax)
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 followmouse           = args.followmouse or false
51     local settings              = args.settings or function() end
52
53     weather.widget    = wibox.widget.textbox(weather_na_markup)
54     weather.icon_path = icons_path .. "na.png"
55     weather.icon      = wibox.widget.imagebox(weather.icon_path)
56
57     function weather.show(t_out)
58         weather.hide()
59
60         if followmouse then
61             notification_preset.screen = mouse.screen
62         end
63
64         weather.notification = naughty.notify({
65             text    = weather.notification_text
66                       or "Waiting for the server to respond...",
67             icon    = weather.icon_path,
68             timeout = t_out,
69             preset  = notification_preset
70         })
71     end
72
73     function weather.hide()
74         if weather.notification ~= nil then
75             naughty.destroy(weather.notification)
76             weather.notification = nil
77         end
78     end
79
80     function weather.attach(obj)
81         obj:connect_signal("mouse::enter", function()
82             weather.show(0)
83         end)
84         obj:connect_signal("mouse::leave", function()
85             weather.hide()
86         end)
87     end
88
89     function weather.forecast_update()
90         local cmd = string.format(forecast_call, city_id, units, lang, cnt, APPID)
91         async.request(cmd, function(f)
92             local pos, err
93             weather_now, pos, err = json.decode(f, 1, nil)
94
95             if not err and weather_now ~= nil and tonumber(weather_now["cod"]) == 200 then
96                 weather.notification_text = ''
97                 for i = 1, weather_now["cnt"] do
98                     local day = string.gsub(read_pipe(string.format(date_cmd, weather_now["list"][i]["dt"])), "\n", "")
99
100                     local tmin = math.floor(weather_now["list"][i]["temp"]["min"])
101                     local tmax = math.floor(weather_now["list"][i]["temp"]["max"])
102                     local desc = weather_now["list"][i]["weather"][1]["description"]
103
104                     weather.notification_text = weather.notification_text ..
105                                                 notification_text_fun(day, desc, tmin, tmax)
106
107                     if i < weather_now["cnt"] then
108                         weather.notification_text = weather.notification_text .. "\n"
109                     end
110                 end
111             else
112                 weather.notification_text = "API/connection error or bad/not set city ID"
113             end
114         end)
115     end
116
117     function weather.update()
118         local cmd = string.format(current_call, city_id, units, lang, APPID)
119         async.request(cmd, function(f)
120             local pos, err
121             weather_now, pos, err = json.decode(f, 1, nil)
122
123             if not err and weather_now ~= nil and tonumber(weather_now["cod"]) == 200 then
124                 weather.icon_path = icons_path .. weather_now["weather"][1]["icon"] .. ".png"
125                 widget = weather.widget
126                 settings()
127             else
128                 weather.icon_path = icons_path .. "na.png"
129                 weather.widget:set_markup(weather_na_markup)
130             end
131
132             weather.icon:set_image(weather.icon_path)
133         end)
134     end
135
136     weather.attach(weather.widget)
137
138     newtimer("weather-" .. city_id, timeout, weather.update)
139     newtimer("weather_forecast-" .. city_id, timeout, weather.forecast_update)
140
141     return setmetatable(weather, { __index = weather.widget })
142 end
143
144 return setmetatable({}, { __call = function(_, ...) return worker(...) end })