--
-- 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_RSS_Initialize(skin, window)
Rainlendar_CreateTimer(60 * 60 * 1000, Shadow4_RSS_OnTimer, skin .. "|" .. window)
Rainlendar_SetEventHandler(Shadow4_RSS_OnPowerResume, 1000, "", skin .. "|" .. window)
Shadow4_RSS_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_RSS_OnTimer(userData)
_, _, skin, window = userData:find("(.*)|(.*)")
url = Rainlendar_GetVariable(skin, window, "RSS_location")
Rainlendar_Log("Downloading: " .. url)
Rainlendar_Download(url, Shadow4_RSS_OnDownload, userData)
return True
end
-- A callback function which gets called when computer wakes up from sleep.
-- This reloads the RSS feed.
function Shadow4_RSS_OnPowerResume(eventData, userData)
Shadow4_RSS_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 rss feed is parsed and the list is filled with the data.
--
function Shadow4_RSS_OnDownload(result, data, userData)
_, _, skin, window = userData:find("(.*)|(.*)")
-- Parse the data and fill in the list item data
if (result == 200) or (result == 0) then
-- Download succeeded
local strTitle
-- Erase the existing items
Rainlendar_SetItemValue(window, "RSS.list", "list", "")
-- Get type of Feed
_, _, feed_type = data:find(".-<(%a+)")
Rainlendar_Log("Feed Type: " .. feed_type)
local tag_entry = ""
local tag_title = ""
local tag_link = ""
local tag_desc = ""
local tag_author = ""
local tag_date = ""
if (feed_type == "rss") then
tag_entry = "(.-) "
tag_title = "
(.-) "
tag_link = " (.-)"
tag_desc = "(.-) "
tag_author = "(.-) "
tag_date = "(.-) "
elseif(feed_type == "feed") then
tag_entry = "(.-) "
tag_title = "(.-)"
tag_link = " "
tag_link2 = " "
tag_desc = "(.-)"
tag_author = ".-(.-) .- "
tag_date = "(.-) "
elseif(feed_type == "rdf") then
tag_entry = "(.-) "
tag_title = "(.-) "
tag_link = " (.-)"
tag_desc = "(.-) "
tag_author = "(.-) "
tag_date = "(.-) "
end
-- Get the first title and set it to the window
_, _, title = data:find(tag_title)
if (title) then
-- Clean the CDATA from the title
_, _, newTitle = title:find("")
if (newTitle) then
title = newTitle
end
title = Shadow4_RSS_DecodeString(title)
if (title) then
Rainlendar_SetItemValue(window, "RSS.title", "text", title)
else
Rainlendar_SetItemValue(window, "RSS.title", "text", "RSS")
end
end
-- Find the items and their components
pos = 1
for item in string.gfind(data, tag_entry) do
_, _, title = item:find(tag_title)
_, _, link = item:find(tag_link)
_, _, desc = item:find(tag_desc)
_, _, author = item:find(tag_author)
_, _, date = item:find(tag_date)
if (link == nil and tag_link2) then
_, _, link = item:find(tag_link2)
end
if (desc == nil) then
_, _, desc = item:find("(.-) ")
end
if (desc) then
_, _, newDesc = desc:find("")
end
if (newDesc) then
desc = newDesc
end
if (title) then
-- Clean the CDATA from the title
_, _, newTitle = title:find("")
if (newTitle) then
title = newTitle
end
text = ""
if (date) then
text = text .. "Date: " .. date .. "\n"
end
if (author) then
text = text .. "Author: " .. author .. "\n"
end
if (desc) then
text = text .. desc
end
title = Shadow4_RSS_DecodeString(title)
text = Shadow4_RSS_DecodeString(text)
-- Remove html from the text
text = string.gsub(text, "<.->", "")
-- Put the parsed data to the list and its tooltip
Rainlendar_SetItemValue(window, "RSS.list", "list." .. pos .. ".type", "1")
if (link) then
link = Shadow4_RSS_DecodeString(link)
Rainlendar_SetItemValue(window, "RSS.list", "list." .. pos .. ".text", "[[" .. link .. "][" .. title .. "]]")
Rainlendar_SetItemValue(window, "RSS.list", "list." .. pos .. ".tooltipHeader", "[[" .. link .. "][" .. title .. "]]")
else
Rainlendar_SetItemValue(window, "RSS.list", "list." .. pos .. ".text", title)
Rainlendar_SetItemValue(window, "RSS.list", "list." .. pos .. ".tooltipHeader", title)
end
Rainlendar_SetItemValue(window, "RSS.list", "list." .. pos .. ".tooltipText", text)
pos = pos + 1
end
end
-- Show an error if we were not able to parse anything
if (pos == 1) then
Rainlendar_SetItemValue(window, "RSS.title", "text", Rainlendar_GetString("Parsing failed!"))
end
else
-- Download failed
Rainlendar_SetItemValue(window, "RSS.title", "text", Rainlendar_GetString("Download failed!"))
end
Rainlendar_Redraw(1, window)
end
-- Converts the NNN; values to utf-8
function Shadow4_RSS_DecodeString(s)
s = string.gsub(s, "(x%x+);",
function (x)
x = x:sub(2)
val = tonumber(x, 16)
if (val < 128) then
return string.char(val)
elseif (val < 2048) then
return string.char(192 + val / 64) .. string.char(128 + (val % 64))
elseif (val < 65536) then
return string.char(224 + val / 4096) .. string.char(128 + ((val / 64) % 64)) .. string.char(128 + (val % 64))
end
end)
s = string.gsub(s, "(%d+);",
function (d)
val = tonumber(d, 10)
if (val < 128) then
return string.char(val)
elseif (val < 2048) then
return string.char(192 + val / 64) .. string.char(128 + (val % 64))
elseif (val < 65536) then
return string.char(224 + val / 4096) .. string.char(128 + ((val / 64) % 64)) .. string.char(128 + (val % 64))
end
end)
-- Clean the data
s = string.gsub(s, "&(.-);",
function (str)
str = str:lower()
if (str == "\r") then return ""
elseif (str == "quot") then return "\""
elseif (str == "amp") then return "&"
elseif (str == "lt") then return "<"
elseif (str == "gt") then return ">"
elseif (str == "nbsp") then return " "
elseif (str == "iexcl") then return "¡"
elseif (str == "cent") then return "¢"
elseif (str == "pound") then return "£"
elseif (str == "curren") then return "¤"
elseif (str == "yen") then return "¥"
elseif (str == "brvbar") then return "¦"
elseif (str == "sect") then return "§"
elseif (str == "uml") then return "¨"
elseif (str == "copy") then return "©"
elseif (str == "ordf") then return "ª"
elseif (str == "laquo") then return "«"
elseif (str == "not") then return "¬"
elseif (str == "shy") then return ""
elseif (str == "reg") then return "®"
elseif (str == "macr") then return "¯"
elseif (str == "deg") then return "°"
elseif (str == "plusmn") then return "±"
elseif (str == "sup2") then return "²"
elseif (str == "sup3") then return "³"
elseif (str == "acute") then return "´"
elseif (str == "micro") then return "µ"
elseif (str == "para") then return "¶"
elseif (str == "middot") then return "·"
elseif (str == "cedil") then return "¸"
elseif (str == "sup1") then return "¹"
elseif (str == "ordm") then return "º"
elseif (str == "raquo") then return "»"
elseif (str == "frac14") then return "¼"
elseif (str == "frac12") then return "½"
elseif (str == "frac34") then return "¾"
elseif (str == "iquest") then return "¿"
elseif (str == "times") then return "×"
elseif (str == "divide") then return "÷"
elseif (str == "Agrave") then return "À"
elseif (str == "Aacute") then return "Á"
elseif (str == "Acirc") then return "Â"
elseif (str == "Atilde") then return "Ã"
elseif (str == "Auml") then return "Ä"
elseif (str == "Aring") then return "Å"
elseif (str == "AElig") then return "Æ"
elseif (str == "Ccedil") then return "Ç"
elseif (str == "Egrave") then return "È"
elseif (str == "Eacute") then return "É"
elseif (str == "Ecirc") then return "Ê"
elseif (str == "Euml") then return "Ë"
elseif (str == "Igrave") then return "Ì"
elseif (str == "Iacute") then return "Í"
elseif (str == "Icirc") then return "Î"
elseif (str == "Iuml") then return "Ï"
elseif (str == "ETH") then return "Ð"
elseif (str == "Ntilde") then return "Ñ"
elseif (str == "Ograve") then return "Ò"
elseif (str == "Oacute") then return "Ó"
elseif (str == "Ocirc") then return "Ô"
elseif (str == "Otilde") then return "Õ"
elseif (str == "Ouml") then return "Ö"
elseif (str == "Oslash") then return "Ø"
elseif (str == "Ugrave") then return "Ù"
elseif (str == "Uacute") then return "Ú"
elseif (str == "Ucirc") then return "Û"
elseif (str == "Uuml") then return "Ü"
elseif (str == "Yacute") then return "Ý"
elseif (str == "THORN") then return "Þ"
elseif (str == "szlig") then return "ß"
elseif (str == "agrave") then return "à"
elseif (str == "aacute") then return "á"
elseif (str == "acirc") then return "â"
elseif (str == "atilde") then return "ã"
elseif (str == "auml") then return "ä"
elseif (str == "aring") then return "å"
elseif (str == "aelig") then return "æ"
elseif (str == "ccedil") then return "ç"
elseif (str == "egrave") then return "è"
elseif (str == "eacute") then return "é"
elseif (str == "ecirc") then return "ê"
elseif (str == "euml") then return "ë"
elseif (str == "igrave") then return "ì"
elseif (str == "iacute") then return "í"
elseif (str == "icirc") then return "î"
elseif (str == "iuml") then return "ï"
elseif (str == "eth") then return "ð"
elseif (str == "ntilde") then return "ñ"
elseif (str == "ograve") then return "ò"
elseif (str == "oacute") then return "ó"
elseif (str == "ocirc") then return "ô"
elseif (str == "otilde") then return "õ"
elseif (str == "ouml") then return "ö"
elseif (str == "oslash") then return "ø"
elseif (str == "ugrave") then return "ù"
elseif (str == "uacute") then return "ú"
elseif (str == "ucirc") then return "û"
elseif (str == "uuml") then return "ü"
elseif (str == "yacute") then return "ý"
elseif (str == "thorn") then return "þ"
elseif (str == "yuml") then return "ÿ"
else return str
end
end)
return s
end