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 Licensed under GNU General Public License v2
5 * (c) 2013, Luke Bonham
9 local newtimer = require("lain.helpers").newtimer
10 local async = require("lain.asyncshell")
12 local naughty = require("naughty")
13 local wibox = require("wibox")
15 local debug = { getinfo = debug.getinfo }
17 local os = { date = os.date,
19 local string = { find = string.find,
23 local tonumber = tonumber
25 local setmetatable = setmetatable
27 -- YAhoo! Weather Notification
31 icon = wibox.widget.imagebox(),
32 widget = wibox.widget.textbox('')
35 local project_path = debug.getinfo(1, 'S').source:match[[^@(.*/).*$]]
36 local localizations_path = project_path .. 'localizations/'
37 local icon_path = project_path .. 'icons/'
38 local api_url = 'http://weather.yahooapis.com/forecastrss'
39 local units_set = '?u=c&w=' -- Default is Celsius
40 local language = string.match(os.getenv("LANG"), "(%S*$*)[.]") or "en_US" -- if LANG is not set
41 local weather_data = nil
42 local notification = nil
45 local settings = function() end
47 yawn_notification_preset = {}
49 local function fetch_weather()
50 local url = api_url .. units_set .. city_id
51 local cmd = "curl --connect-timeout 1 -fsm 3 '" .. url .. "'"
53 async.request(cmd, function(f)
54 local text = f:read("*a")
57 -- In case of no connection or invalid city ID
58 -- widgets won't display
59 if text == "" or text:match("City not found")
61 yawn.icon:set_image(icon_path .. "na.png")
63 weather_data = "Service not available at the moment."
64 yawn.widget:set_text(" N/A ")
66 weather_data = "City not found!\n" ..
67 "Are you sure " .. city_id ..
68 " is your Yahoo city ID?"
69 yawn.widget:set_text(" ? ")
74 -- Processing raw data
75 weather_data = text:gsub("<.->", "")
76 weather_data = weather_data:match("Current Conditions:.-Full") or ""
78 -- may still happens in case of bad connectivity
79 if weather_data == "" then
80 yawn.icon:set_image(icon_path .. "na.png")
81 yawn.widget:set_text(" ? ")
85 weather_data = weather_data:gsub("Current Conditions:.-\n", "Now: ")
86 weather_data = weather_data:gsub("Forecast:.-\n", "")
87 weather_data = weather_data:gsub("\nFull", "")
88 weather_data = weather_data:gsub("[\n]$", "")
89 weather_data = weather_data:gsub(" [-] " , ": ")
90 weather_data = weather_data:gsub("[.]", ",")
91 weather_data = weather_data:gsub("High: ", "")
92 weather_data = weather_data:gsub(" Low: ", " - ")
94 -- Getting info for text widget
95 local now = weather_data:sub(weather_data:find("Now:")+5,
96 weather_data:find("\n")-1)
97 forecast = now:sub(1, now:find(",")-1)
98 units = now:sub(now:find(",")+2, -2)
100 -- Day/Night icon change
101 local hour = tonumber(os.date("%H"))
104 if forecast == "Clear" or
105 forecast == "Fair" or
106 forecast == "Partly Cloudy" or
107 forecast == "Mostly Cloudy"
109 if hour >= 6 and hour <= 18
117 sky = sky .. forecast:gsub(" ", ""):gsub("/", "") .. ".png"
119 -- In case there's no defined icon for current forecast
120 if io.open(sky) == nil then
121 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 _data = weather_data:match(": %S.-,") or weather_data
142 forecast = _data:gsub(": ", ""):gsub(",", "")
143 units = units:gsub(" ", "")
150 if notification ~= nil then
151 naughty.destroy(notification)
156 function yawn.show(t_out)
157 if yawn.widget._layout.text:match("?")
159 fetch_weather(settings)
164 notification = naughty.notify({
165 preset = yawn_notification_preset,
169 screen = client.focus and client.focus.screen or 1
173 function yawn.register(id, args)
174 local args = args or {}
175 local timeout = args.timeout or 600
176 settings = args.settings or function() end
178 if args.u == "f" then units_set = '?u=f&w=' end
182 newtimer("yawn", timeout, fetch_weather)
184 yawn.icon:connect_signal("mouse::enter", function()
187 yawn.icon:connect_signal("mouse::leave", function()
194 function yawn.attach(widget, id, args)
195 yawn.register(id, args)
197 widget:connect_signal("mouse::enter", function()
201 widget:connect_signal("mouse::leave", function()
206 return setmetatable(yawn, { __call = function(_, ...) return yawn.register(...) end })