--
-- This is the initialization function. It gets called every time Rainlendar
-- is started or refreshed. The initialization function must be defined in the
-- skin file.
--
function Shadow4_Weather_Initialize(window)
-- Create a timer which updates the data once per hour
Rainlendar_CreateTimer(60 * 60 * 1000, Shadow4_Weather_OnTimer, window)
-- Run the timer callback so the data is updated immediately
Shadow4_Weather_OnTimer(window)
end
--
-- A callback function for the timer. This gets called every time the timer triggers.
-- Returns true so the timer will go on forever (false would stop the timer).
--
function Shadow4_Weather_OnTimer(userData)
url = Rainlendar_GetVariable("Shadow4", userData, "Weather_location")
Rainlendar_Log("Downloading: " .. url)
Rainlendar_Download(url, Shadow4_Weather_OnDownload, userData)
return True
end
--
-- A callback function for the downloading. This gets called after the given file
-- has been downloaded from the net. The data is the downloaded file.
-- In this case the weather rss feed from msn is parsed and the view is filled
-- in with the data.
--
function Shadow4_Weather_OnDownload(result, data, userData)
if (result == 200) then
-- Get the first link from the data
_, _, link = data:find("(.-)")
-- Get the first title from the data
_, _, title = data:find("
(.-)")
if (title) then
-- Cut everything after comma away
pos = title:find(",")
if (pos) then
title = title:sub(0, pos - 1)
end
if (link) then
-- Create a link from the title
title = "[[" .. link .. "][" .. title .. "]]"
end
-- Put the title to the window
Rainlendar_SetItemValue(userData, "Weather.title", "text", title)
end
-- Loop throught all :s. The first one is for the current conditions and the second is the forecast.
loop = 1
for item in string.gfind(data, "(.-)") do
if (loop == 1) then
-- Get the icon number
_, _, icon = item:find("(%d+).gif")
if (icon) then
Rainlendar_SetItemValue(userData, "Weather.icon.large", "element", "bitmap.icon.weather." .. icon)
end
-- Get the degrees
_, _, degrees = item:find("(-*%d+°[CF])")
if (degrees) then
degrees = degrees:gsub("°", "°")
Rainlendar_SetItemValue(userData, "Weather.degrees", "text", degrees)
end
-- Create a tooltip for the weather icon
_, _, conditions = item:find("Aktuelle Wetterlage: (.*) in")
_, _, date = item:find("am (.*).")
_, _, degrees = item:find("/> .-%p (.-)%p Luftfeuchtigkeit")
_, _, humidity = item:find("Luftfeuchtigkeit: (%d*%%)")
_, _, winds = item:find("Windstärke: (.-). ")
text = ""
if (conditions) then
text = text .. "Aktuelle Wetterlage: " .. conditions .. "\n"
end
if (degrees) then
degrees = degrees:gsub("°", "°")
text = text .. "Temperatur: " .. degrees .. "\n"
end
if (humidity) then
text = text .. "Luftfeuchtigkeit: " .. humidity .. "\n"
end
if (winds) then
text = text .. "Windstärke: " .. winds .. "\n"
end
if (date) then
text = text .. "am " .. date .. "\n"
end
Rainlendar_SetItemValue(userData, "Weather.icon.large", "tooltipHeader", title)
Rainlendar_SetItemValue(userData, "Weather.icon.large", "tooltipText", text)
else
-- Loop through all the days in the forecast
count = 1
for forecast in string.gfind(item, "") do
if (count <= 5) then
-- Get the day name
_, _, day = forecast:find(">(.*)")
if (day) then
if (day == "Heute" or day == "Morgen") then
Rainlendar_SetItemValue(userData, "Weather.day." .. count, "text", day)
else
-- Long names do not fit so cut them
Rainlendar_SetItemValue(userData, "Weather.day." .. count, "text", day:sub(0, 3))
end
end
-- Get the icon number
_, _, icon = forecast:find("(%d+).gif")
if (icon) then
Rainlendar_SetItemValue(userData, "Weather.icon.small." .. count, "element", "bitmap.icon.weather." .. icon)
end
-- Get the high and low temp
_, _, lo, hi = forecast:find("Niedrig: (-*%d+).-Höchst: (-*%d+)")
if (lo and hi) then
Rainlendar_SetItemValue(userData, "Weather.degrees." .. count, "text", lo .. "/" .. hi)
end
end
count = count + 1
end
end
loop = loop + 1
end
else
-- Download failed
Rainlendar_SetItemValue(userData, "Weather.title", "text", "Download failed!")
end
Rainlendar_Redraw(0, userData)
end