From 0b2542d1a61fc5425ff65ab3dbf7ba7de174763f Mon Sep 17 00:00:00 2001 From: Diego Nehab Date: Wed, 4 Feb 2004 14:29:11 +0000 Subject: Worked on the manual. Implemented stuffing (needs test) Added cddb and qp examples. --- samples/cddb.lua | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 samples/cddb.lua (limited to 'samples') diff --git a/samples/cddb.lua b/samples/cddb.lua new file mode 100644 index 0000000..6ade3c0 --- /dev/null +++ b/samples/cddb.lua @@ -0,0 +1,43 @@ +if not arg or not arg[1] or not arg[2] then + print("luasocket cddb.lua []") + os.exit(1) +end + +local server = arg[3] or "http://freedb.freedb.org/~cddb/cddb.cgi" + +function parse(body) + local lines = string.gfind(body, "(.-)\r\n") + local status = lines() + local _, _, code, message = string.find(status, "(%d%d%d) (.*)") + if tonumber(code) ~= 210 then + return nil, code, message + end + local data = {} + for l in lines do + local c = string.sub(l, 1, 1) + if c ~= '#' and c ~= '.' then + local _, _, key, value = string.find(l, "(.-)=(.*)") + value = string.gsub(value, "\\n", "\n") + value = string.gsub(value, "\\\\", "\\") + value = string.gsub(value, "\\t", "\t") + data[key] = value + end + end + return data, code, message +end + +local host = socket.dns.gethostname() +local query = "%s?cmd=cddb+read+%s+%s&hello=LuaSocket+%s+LuaSocket+2.0&proto=6" +local url = string.format(query, server, arg[1], arg[2], host) +local body, headers, code, error = socket.http.get(url) + +if code == 200 then + local data, code, error = parse(body) + if not data then + print(error or code) + else + for i,v in data do + io.write(i, ': ', v, '\n') + end + end +else print(error) end -- cgit v1.2.3-55-g6feb