function getChildTagsByType(topTag, tagType) local tags = {} for key, value in topTag do if type(value)=="table" then if valOrNil(value.label) == tagType then tinsert(tags, value) end end end return tags end function parseCorrectType(ParsedXML) if ParsedXML[2] == nil then return {title="N/A", link="N/A", description="N/A"}, {n=0, nEnc=0, feedType="Invalid feed"} end feedType = ParsedXML[2].label if feedType == "rss" then rssVersion = ParsedXML[2].args["version"] return parseRSS(ParsedXML, rssVersion) elseif feedType == "rdf" or string.find(feedType, "[Rr][Dd][Ff][:]?[Rr][Dd][Ff]") then return parseRDF(ParsedXML) else return {title="N/A", link="N/A", description="N/A"}, {n=0, nEnc=0, feedType="Unknown feed type"} end end function parseRSS(ParsedXML, rssVersion) local channel = ParsedXML[2][1] local channelInfo = {} local feeditems = {} channelInfo["title"] = getChildTagsByType(channel, "title")[1][1] channelInfo["link"] = getChildTagsByType(channel, "link")[1][1] channelInfo["description"] = getChildTagsByType(channel, "description")[1][1] local items = getChildTagsByType(channel, "item") local parsedFeed = {} local n, nEnc = 0, 0 for key, value in items do local title, link, description = getChildTagsByType(value, "title")[1], getChildTagsByType(value, "link")[1], getChildTagsByType(value, "description")[1] local enc = getChildTagsByType(value, "enclosure") local newItem = {} newItem["title"] = title[1] newItem["link"] = link[1] newItem["description"] = description[1] if table.getn(enc) > 0 then local enclosures = {} for key, val in enc do nEnc = nEnc + 1 enclosures[key] = val.args.url end newItem.enclosures = enclosures end tinsert(feeditems, newItem) n = n + 1 end parsedFeed["items"] = feeditems parsedFeed["n"] = n parsedFeed["nEnc"] = nEnc parsedFeed["feedType"] = "RSS "..rssVersion return channelInfo, parsedFeed end function parseRDF(ParsedXML) local channel = ParsedXML[2][1] local channelInfo = {} local feeditems = {} channelInfo["title"] = getChildTagsByType(channel, "title")[1][1] channelInfo["link"] = getChildTagsByType(channel, "link")[1][1] channelInfo["description"] = getChildTagsByType(channel, "description")[1][1] local itemsTag = getChildTagsByType(channel, "items")[1] local items = getChildTagsByType(ParsedXML[2], "item") local parsedFeed = {} local n, nEnc = 0, 0 for key, value in items do local title, link, description = getChildTagsByType(value, "title")[1], getChildTagsByType(value, "link")[1], getChildTagsByType(value, "description")[1] local enc = getChildTagsByType(value, "enclosure") local newItem = {} newItem["title"] = title[1] newItem["link"] = link[1] newItem["description"] = description[1] if table.getn(enc) > 0 then local enclosures = {} for key, val in enc do nEnc = nEnc + 1 enclosures[key] = val.args.url end newItem.enclosures = enclosures end tinsert(feeditems, newItem) n = n + 1 end parsedFeed["items"] = feeditems parsedFeed["n"] = n parsedFeed["nEnc"] = nEnc parsedFeed["feedType"] = "RDF" return channelInfo, parsedFeed end function writePlainText(title, ParsedFeed) local outfile = assert(io.open("feeds/"..fixFileName(title)..".txt", "w")) for key, item in ParsedFeed.items do outfile:write("Title: "..item.title.."\r\n") outfile:write("Link: "..item.link.."\r\n") desc = string.gsub(item.description, '[<][!][[]CDATA[[]',"") desc = string.gsub(desc, '[]][]][>]',"") outfile:write("Description: "..string.gsub(desc, "<(%/?)([%w:]+)(.-)(%/?)>","").."\r\n") if type(item.enclosures) ~= "nil" then outfile:write("Enclosures:".."\r\n") for key, enclosure in item.enclosures do outfile:write(enclosure.."\r\n") end end outfile:write("\r\n") end outfile:close() end