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.
3 Licensed under GNU General Public License v2
4 * (c) 2013, Luke Bonham
8 local newtimer = require("lain.helpers").newtimer
10 local naughty = require("naughty")
11 local wibox = require("wibox")
13 local debug = { getinfo = debug.getinfo }
15 local os = { date = os.date,
17 local string = { find = string.find,
21 local tonumber = tonumber
23 local setmetatable = setmetatable
25 -- YAhoo! Weather Notification
29 icon = wibox.widget.imagebox(),
30 widget = wibox.widget.textbox('')
33 local project_path = debug.getinfo(1, 'S').source:match[[^@(.*/).*$]]
34 local localizations_path = project_path .. 'localizations/'
35 local icon_path = project_path .. 'icons/'
36 local api_url = 'http://weather.yahooapis.com/forecastrss'
37 local units_set = '?u=c&w=' -- Default is Celsius
38 local language = string.match(os.getenv("LANG"), "(%S*$*)[.]")
39 local weather_data = nil
40 local notification = nil
43 local settings = function() end
45 yawn_notification_preset = {}
47 local function fetch_weather()
48 local url = api_url .. units_set .. city_id
49 local f = io.popen("curl --connect-timeout 1 -fsm 1 '"
51 local text = f:read("*all")
54 -- In case of no connection or invalid city ID
55 -- widgets won't display
56 if text == "" or text:match("City not found")
58 yawn.icon:set_image(icon_path .. "na.png")
60 weather_data = "Service not available at the moment."
61 yawn.widget:set_text("N/A")
63 weather_data = "City not found!\n" ..
64 "Are you sure " .. city_id ..
65 " is your Yahoo city ID?"
66 yawn.widget:set_text("?")
71 -- Processing raw data
72 weather_data = text:gsub("<.->", "")
73 weather_data = weather_data:match("Current Conditions:.-Full") or ""
75 -- may still happens in case of bad connectivity
76 if weather_data == "" then
77 yawn.icon:set_image(icon_path .. "na.png")
78 yawn.widget:set_text("?")
82 weather_data = weather_data:gsub("Current Conditions:.-\n", "Now: ")
83 weather_data = weather_data:gsub("Forecast:.-\n", "")
84 weather_data = weather_data:gsub("\nFull", "")
85 weather_data = weather_data:gsub("[\n]$", "")
86 weather_data = weather_data:gsub(" [-] " , ": ")
87 weather_data = weather_data:gsub("[.]", ",")
88 weather_data = weather_data:gsub("High: ", "")
89 weather_data = weather_data:gsub(" Low: ", " - ")
91 -- Getting info for text widget
92 local now = weather_data:sub(weather_data:find("Now:")+5,
93 weather_data:find("\n")-1)
94 forecast = now:sub(1, now:find(",")-1)
95 units = now:sub(now:find(",")+2, -2)
97 -- Day/Night icon change
98 local hour = tonumber(os.date("%H"))
101 if forecast == "Clear" or
102 forecast == "Fair" or
103 forecast == "Partly Cloudy" or
104 forecast == "Mostly Cloudy"
106 if hour >= 6 and hour <= 18
114 sky = sky .. forecast:gsub(" ", ""):gsub("/", "") .. ".png"
116 -- In case there's no defined icon for current forecast
119 sky = icon_path .. "na.png"
125 local f = io.open(localizations_path .. language, "r")
126 if language:find("en_") == nil and f ~= nil
129 for line in io.lines(localizations_path .. language)
131 word = string.sub(line, 1, line:find("|")-1)
132 translation = string.sub(line, line:find("|")+1)
133 weather_data = string.gsub(weather_data, word, translation)
137 -- Finally setting infos
138 yawn.icon:set_image(sky)
141 forecast = weather_data:match(": %S.-,"):gsub(": ", ""):gsub(",", "")
142 units = units:gsub(" ", "")
148 if notification ~= nil then
149 naughty.destroy(notification)
154 function yawn.show(t_out)
155 if yawn.widget._layout.text == "?"
157 fetch_weather(settings)
162 notification = naughty.notify({
163 preset = yawn_notification_preset,
170 function yawn.register(id, args)
171 local args = args or {}
172 local timeout = args.timeout or 600
173 settings = args.settings or function() end
175 if args.u == "f" then units_set = '?u=f&w=' end
179 newtimer("yawn", timeout, fetch_weather)
181 yawn.icon:connect_signal("mouse::enter", function()
184 yawn.icon:connect_signal("mouse::leave", function()
191 function yawn.attach(widget, id, args)
192 yawn.register(id, args)
194 widget:connect_signal("mouse::enter", function()
198 widget:connect_signal("mouse::leave", function()
203 return setmetatable(yawn, { __call = function(_, ...) return yawn.register(...) end })