--
-- 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(skin, window)
-- Create a timer which updates the data once per hour
Rainlendar_CreateTimer(60 * 60 * 1000, Shadow4_Weather_OnTimer, skin .. "|" .. window)
-- Set global event for power resume
Rainlendar_SetEventHandler(Shadow4_Weather_OnPowerResume, 1000, "", skin .. "|" .. window)
-- Run the timer callback so the data is updated immediately
Shadow4_Weather_OnTimer(skin .. "|" .. 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)
-- Split the user data
_, _, skin, window = userData:find("(.*)|(.*)")
url = Rainlendar_GetVariable(skin, window, "Weather_location")
Rainlendar_Log("Downloading: " .. url)
Rainlendar_Download(url, Shadow4_Weather_OnDownload, userData)
return True
end
-- A callback function which gets called when computer wakes up from sleep.
-- This reloads the weather data.
function Shadow4_Weather_OnPowerResume(eventData, userData)
Shadow4_Weather_OnTimer(userData)
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)
-- Split the user data
_, _, skin, window = userData:find("(.*)|(.*)")
if (result == 200) then
url = Rainlendar_GetVariable(skin, window, "Weather_location")
-- MODIF
-- Check for German localized page
if url:find("wetter.msn.com") then
strReCurrent = "Aktuelle Wetterlage: (.*) in"
strReAsOf = "am (.*)."
strReHumidity = "/> .-%p (.-)%p Luftfeuchtigkeit"
strReHumidity2 = "Luftfeuchtigkeit: (%d*%%)"
strReWinds = "Windstärke: (.-). "
strReLoHi = "Niedrig: (-*%d+).-Höchst: (-*%d+)"
-- Check for French localized page
elseif url:find("meteo.msn.com") then
strReCurrent = "Conditions actuelles : (.-) à"
strReAsOf = "%(à (.*)."
strReHumidity = "/> .-%p (.-)%p Humidité"
strReHumidity2 = "Humidité : (%d.-%%)"
strReWinds = "Vents : (.-). "
strReLoHi = "Bas: (-*%d+).-Haut: (-*%d+)"
-- Check for English localized page
else
strReCurrent = "Current Conditions: (.*) in"
strReAsOf = "as of (.*)."
strReHumidity = "/> .-%p (.-)%p Humidity"
strReHumidity2 = "Humidity: (%d*%%)"
strReWinds = "Winds: (.-). "
strReLoHi = "Lo: (-*%d+).-Hi: (-*%d+)"
end
-- Get the first link from the data
_, _, link = data:find("(.-)")
-- Get the first title from the data
_, _, title = data:find("
(.-)")
if (title) then
title = Shadow4_Weather_DecodeString(title)
-- 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(window, "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(window, "Weather.icon.large", "element", "bitmap.icon.weather." .. icon)
end
-- Get the degrees
_, _, degrees = item:find("(-*%d+°[CF])")
if (degrees) then
degrees = Shadow4_Weather_DecodeString(degrees)
Rainlendar_SetItemValue(window, "Weather.degrees", "text", degrees)
end
-- Create a tooltip for the weather icon
_, _, conditions = item:find(strReCurrent)
_, _, date = item:find(strReAsOf)
_, _, degrees = item:find(strReHumidity)
_, _, humidity = item:find(strReHumidity2)
_, _, winds = item:find(strReWinds)
text = ""
if (conditions) then
-- MODIF (to resolve special characters problem in French or other languages)
text = Rainlendar_GetString("Conditions: ") .. Shadow4_Weather_DecodeString(conditions) .. " " .. "\n"
end
if (degrees) then
-- MODIF (to resolve special characters problem in French or other languages)
text = text .. Rainlendar_GetString("Temperature: ") .. Shadow4_Weather_DecodeString(degrees) .. " " .. "\n"
end
if (humidity) then
-- MODIF
text = text .. Rainlendar_GetString("Humidity: ") .. humidity .. " " .. "\n"
end
if (winds) then
-- MODIF
text = text .. Rainlendar_GetString("Winds: ") .. winds .. " " .. "\n"
end
if (date) then
-- MODIF
text = text .. Rainlendar_GetString("Time: ") .. date .. " "
end
Rainlendar_SetItemValue(window, "Weather.icon.large", "tooltipHeader", title)
Rainlendar_SetItemValue(window, "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
-- MODIF (because in french weather RSS, days begin with a lowercase)
day = Shadow4_Weather_Uppercase(day)
if (day == "Today" or day == "Tomorrow") then
Rainlendar_SetItemValue(window, "Weather.day." .. count, "text", day)
else
-- Long names do not fit so cut them
Rainlendar_SetItemValue(window, "Weather.day." .. count, "text", day:sub(0, 3))
end
end
-- Get the icon number
_, _, icon = forecast:find("(%d+).gif")
if (icon) then
Rainlendar_SetItemValue(window, "Weather.icon.small." .. count, "element", "bitmap.icon.weather." .. icon)
end
-- Get the high and low temp
_, _, lo, hi = forecast:find(strReLoHi)
if (lo and hi) then
Rainlendar_SetItemValue(window, "Weather.degrees." .. count, "text", lo .. "/" .. hi)
end
end
count = count + 1
end
end
loop = loop + 1
end
else
-- Download failed
Rainlendar_SetItemValue(window, "Weather.title", "text", Rainlendar_GetString("Download failed!"))
end
Rainlendar_Redraw(0, window)
end
-- Converts the NNN; values to utf-8
function Shadow4_Weather_DecodeString(s)
s = string.gsub(s, "(%d%d%d);",
function (d)
val = tonumber(d, 10)
if (val < 128) then
return string.char(val)
elseif (val < 192) then
return "\194" .. string.char(val)
else
return "\195" .. string.char(val - 64)
end
end)
return s
end
-- MODIF
-- Converts the first character of a string to uppercase
function Shadow4_Weather_Uppercase(s)
first = string.sub(s, 1, 1)
s = string.sub(s, 2)
first = string.upper(first)
return first .. s
end