]> git.madduck.net Git - etc/awesome.git/blob - widgets/weather.lua.orig

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:

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