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
11 local naughty = require("naughty")
12 local wibox = require("wibox")
14 local debug = { getinfo = debug.getinfo }
16 local os = { date = os.date,
18 local string = { find = string.find,
22 local tonumber = tonumber
24 local setmetatable = setmetatable
26 -- YAhoo! Weather Notification
30 icon = wibox.widget.imagebox(),
31 widget = wibox.widget.textbox('')
34 local project_path = debug.getinfo(1, 'S').source:match[[^@(.*/).*$]]
35 local localizations_path = project_path .. 'localizations/'
36 local icon_path = project_path .. 'icons/'
37 local api_url = 'http://weather.yahooapis.com/forecastrss'
38 local units_set = '?u=c&w=' -- Default is Celsius
39 local language = string.match(os.getenv("LANG"), "(%S*$*)[.]")
40 local weather_data = nil
41 local notification = nil
44 local settings = function() end
46 yawn_notification_preset = {}
48 local function fetch_weather()
49 local url = api_url .. units_set .. city_id
50 local f = io.popen("curl --connect-timeout 1 -fsm 1 '"
52 local text = f:read("*all")
55 -- In case of no connection or invalid city ID
56 -- widgets won't display
57 if text == "" or text:match("City not found")
59 yawn.icon:set_image(icon_path .. "na.png")
61 weather_data = "Service not available at the moment."
62 yawn.widget:set_text("N/A")
64 weather_data = "City not found!\n" ..
65 "Are you sure " .. city_id ..
66 " is your Yahoo city ID?"
67 yawn.widget:set_text("?")
72 -- Processing raw data
73 weather_data = text:gsub("<.->", "")
74 weather_data = weather_data:match("Current Conditions:.-Full")
75 weather_data = weather_data:gsub("Current Conditions:.-\n", "Now: ")
76 weather_data = weather_data:gsub("Forecast:.-\n", "")
77 weather_data = weather_data:gsub("\nFull", "")
78 weather_data = weather_data:gsub("[\n]$", "")
79 weather_data = weather_data:gsub(" [-] " , ": ")
80 weather_data = weather_data:gsub("[.]", ",")
81 weather_data = weather_data:gsub("High: ", "")
82 weather_data = weather_data:gsub(" Low: ", " - ")
84 -- Getting info for text widget
85 local now = weather_data:sub(weather_data:find("Now:")+5,
86 weather_data:find("\n")-1)
87 forecast = now:sub(1, now:find(",")-1)
88 units = now:sub(now:find(",")+2, -2)
90 -- Day/Night icon change
91 local hour = tonumber(os.date("%H"))
94 if forecast == "Clear" or
96 forecast == "Partly Cloudy" or
97 forecast == "Mostly Cloudy"
99 if hour >= 6 and hour <= 18
107 sky = sky .. forecast:gsub(" ", ""):gsub("/", "") .. ".png"
109 -- In case there's no defined icon for current forecast
112 sky = icon_path .. "na.png"
118 local f = io.open(localizations_path .. language, "r")
119 if language:find("en_") == nil and f ~= nil
122 for line in io.lines(localizations_path .. language)
124 word = string.sub(line, 1, line:find("|")-1)
125 translation = string.sub(line, line:find("|")+1)
126 weather_data = string.gsub(weather_data, word, translation)
130 -- Finally setting infos
131 yawn.icon:set_image(sky)
134 forecast = weather_data:match(": %S+.-,"):gsub(": ", ""):gsub(",", "\n")
135 units = units:gsub(" ", "")
141 if notification ~= nil then
142 naughty.destroy(notification)
147 function yawn.show(t_out)
148 if yawn.widget._layout.text == "?"
150 fetch_weather(settings)
155 notification = naughty.notify({
156 preset = yawn_notification_preset,
163 function yawn.register(id, args)
164 local args = args or {}
165 local timeout = args.timeout or 600
166 settings = args.settings or function() end
168 if args.u == "f" then units_set = '?u=f&w=' end
172 newtimer("yawn", timeout, fetch_weather)
174 yawn.icon:connect_signal("mouse::enter", function()
177 yawn.icon:connect_signal("mouse::leave", function()
184 function yawn.attach(widget, id, args)
185 yawn.register(id, args)
187 widget:connect_signal("mouse::enter", function()
191 widget:connect_signal("mouse::leave", function()
196 return setmetatable(yawn, { __call = function(_, ...) return yawn.register(...) end })