Add. examples for Lua-cURL
parent
40e0b2668e
commit
5974af17ea
|
@ -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")
|
|
@ -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
|
|
@ -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
|
Loading…
Reference in New Issue