diff --git a/examples/cURL.lua b/examples/Lua-cURL/cURL.lua similarity index 100% rename from examples/cURL.lua rename to examples/Lua-cURL/cURL.lua diff --git a/examples/Lua-cURL/file.lua b/examples/Lua-cURL/file.lua new file mode 100644 index 0000000..d46239e --- /dev/null +++ b/examples/Lua-cURL/file.lua @@ -0,0 +1,16 @@ +local cURL = require("cURL") + +-- open output file +f = io.open("example_homepage", "w") + +c = cURL.easy_init() +-- setup url +c:setopt_url("http://www.example.com/") +-- perform, invokes callbacks +c:perform({writefunction = function(str) + f:write(str) + end}) + +-- close output file +f:close() +print("Done") \ No newline at end of file diff --git a/examples/Lua-cURL/multi.lua b/examples/Lua-cURL/multi.lua new file mode 100644 index 0000000..c094cea --- /dev/null +++ b/examples/Lua-cURL/multi.lua @@ -0,0 +1,22 @@ +local cURL = require("cURL") + +-- setup easy +c = cURL.easy_init() +c2 = cURL.easy_init() + +-- setup url +c:setopt_url("http://www.lua.org/") +c2:setopt_url("http://luajit.org/") + +m = cURL.multi_init() +m:add_handle(c) +m:add_handle(c2) + +local f1 = io.open("lua.html","a+") +local f2 = io.open("luajit.html","a+") + +for data, type, easy in m:perform() do +-- if (type == "header") then print(data) end + if (type == "data" and c == easy) then f1:write(data) end + if (type == "data" and c2 == easy) then f2:write(data) end +end \ No newline at end of file diff --git a/examples/Lua-cURL/rss.lua b/examples/Lua-cURL/rss.lua new file mode 100644 index 0000000..7c2ff9d --- /dev/null +++ b/examples/Lua-cURL/rss.lua @@ -0,0 +1,53 @@ +-- use LuaExpat and Lua-CuRL together for On-The-Fly XML parsing +local lxp = require("lxp") +local cURL = require("cURL") + +tags = {} +items = {} + +callback = {} + +function callback.StartElement(parser, tagname) + tags[#tags + 1] = tagname + if (tagname == "item") then + items[#items + 1] = {} + end +end + +function callback.CharacterData(parser, str) + if (tags[#tags -1] == "item") then + --we are parsing a item, get rid of trailing whitespace + items[#items][tags[#tags]] = string.gsub(str, "%s*$", "") + end +end +function callback.EndElement(parser, tagname) + --assuming well formed xml + tags[#tags] = nil +end + +p = lxp.new(callback) + +-- create and setup easy handle +c = cURL.easy_init() +c:setopt_url("http://www.lua.org/news.rss") + +m = cURL.multi_init() +m:add_handle(c) + +for data,type in m:perform() do + -- ign "header" + if (type == "data") then + assert(p:parse(data)) + end +end + +--finish document +assert(p:parse()) +p:close() + +for i, item in ipairs(items) do + for k, v in pairs(item) do + print(k,v) + end + print() +end \ No newline at end of file