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.
4 Yahoo's Awesome (WM) Weather Notification
6 Licensed under WTFPL v2
7 * (c) 2013, Luke Bonham
11 local markup = require("lain.util.markup")
13 local beautiful = require("beautiful")
14 local naughty = require("naughty")
15 local wibox = require("wibox")
17 local debug = { getinfo = debug.getinfo }
19 local os = { date = os.date,
21 local string = { find = string.find,
25 local tonumber = tonumber
27 local setmetatable = setmetatable
30 -- https://github.com/copycat-killer/yawn
36 icon = wibox.widget.imagebox(),
37 widget = wibox.widget.textbox()
40 local project_path = debug.getinfo(1, 'S').source:match[[^@(.*/).*$]]
41 local localizations_path = project_path .. 'localizations/'
42 local icon_path = project_path .. 'icons/'
43 local api_url = 'http://weather.yahooapis.com/forecastrss'
44 local units_set = '?u=c&w=' -- Default is Celsius
45 local language = string.match(os.getenv("LANG"), "(%S*$*)[.]")
46 local weather_data = nil
47 local notification = nil
51 local update_timer = nil
53 local function fetch_weather(args)
54 local toshow = args.toshow or "forecast"
55 local spr = args.spr or " "
56 local footer = args.footer or ""
58 local url = api_url .. units_set .. city_id
59 local f = io.popen("curl --connect-timeout 1 -fsm 2 '"
61 local text = f:read("*all")
64 -- In case of no connection or invalid city ID
65 -- widgets won't display
66 if text == "" or text:match("City not found")
68 sky = icon_path .. "na.png"
70 weather_data = "Service not available at the moment."
73 weather_data = "City not found!\n" ..
74 "Are you sure " .. city_id ..
75 " is your Yahoo city ID?"
80 -- Processing raw data
81 weather_data = text:gsub("<.->", "")
82 weather_data = weather_data:match("Current Conditions:.-Full")
83 weather_data = weather_data:gsub("Current Conditions:.-\n", "Now: ")
84 weather_data = weather_data:gsub("Forecast:.-\n", "")
85 weather_data = weather_data:gsub("\nFull", "")
86 weather_data = weather_data:gsub("[\n]$", "")
87 weather_data = weather_data:gsub(" [-] " , ": ")
88 weather_data = weather_data:gsub("[.]", ",")
89 weather_data = weather_data:gsub("High: ", "")
90 weather_data = weather_data:gsub(" Low: ", " - ")
92 -- Getting info for text widget
93 local now = weather_data:sub(weather_data:find("Now:")+5,
94 weather_data:find("\n")-1)
95 local forecast = now:sub(1, now:find(",")-1)
96 local units = now:sub(now:find(",")+2, -2)
98 -- Day/Night icon change
99 local hour = tonumber(os.date("%H"))
102 if forecast == "Clear" or
103 forecast == "Fair" or
104 forecast == "Partly Cloudy" or
105 forecast == "Mostly Cloudy"
107 if hour >= 6 and hour <= 18
115 sky = sky .. forecast:gsub(" ", ""):gsub("/", "") .. ".png"
117 -- In case there's no defined icon for current forecast
120 sky = icon_path .. "na.png"
126 local f = io.open(localizations_path .. language, "r")
127 if language:find("en_") == nil and f ~= nil
130 for line in io.lines(localizations_path .. language)
132 word = string.sub(line, 1, line:find("|")-1)
133 translation = string.sub(line, line:find("|")+1)
134 weather_data = string.gsub(weather_data, word, translation)
138 -- Finally setting infos
139 forecast = weather_data:match(": %S+"):gsub(": ", ""):gsub(",", "")
140 yawn.forecast = markup(yawn.forecast_color, markup.font(beautiful.font, forecast))
141 yawn.units = markup(yawn.units_color, markup.font(beautiful.font, units))
142 yawn.icon:set_image(sky)
144 if toshow == "forecast" then
146 elseif toshow == "units" then
149 return yawn.forecast .. spr
150 .. yawn.units .. footer
155 if notification ~= nil then
156 naughty.destroy(notification)
161 function yawn.show(t_out)
162 if yawn.widget._layout.text == "?"
164 if update_timer ~= nil
166 update_timer:emit_signal("timeout")
168 fetch_weather(settings)
174 notification = naughty.notify({
178 fg = yawn.notification_color
182 function yawn.register(id, args)
183 local args = args or {}
185 settings = { args.toshow, args.spr, args.footer }
187 yawn.units_color = args.units_color or
188 beautiful.fg_normal or "#FFFFFF"
189 yawn.forecast_color = args.forecast_color or
191 yawn.notification_color = args.notification_color or
192 beautiful.fg_focus or "#FFFFFF"
194 if args.u == "f" then units_set = '?u=f&w=' end
198 update_timer = timer({ timeout = 600 }) -- 10 mins
199 update_timer:connect_signal("timeout", function()
200 yawn.widget:set_markup(fetch_weather(settings))
203 update_timer:emit_signal("timeout")
205 yawn.icon:connect_signal("mouse::enter", function()
208 yawn.icon:connect_signal("mouse::leave", function()
213 function yawn.attach(widget, id, args)
214 yawn.register(id, args)
216 widget:connect_signal("mouse::enter", function()
220 widget:connect_signal("mouse::leave", function()
227 return setmetatable(yawn, { __call = function(_, ...) return yawn.register(...) end })