changeset 4:06f22f0d9cab

add some lua files
author Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
date Sat, 28 Sep 2013 05:47:24 +0900
parents 30b09b383f4e
children 7e9ce3e3d84a
files http/bullet.lua http/slaxdom.lua http/slaxml.lua http/win_rate.lua
diffstat 4 files changed, 301 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/http/bullet.lua	Wed May 29 00:15:31 2013 +0900
+++ b/http/bullet.lua	Sat Sep 28 05:47:24 2013 +0900
@@ -2,19 +2,12 @@
 local http = require("socket.http")
 local ltn12 = require("ltn12")
 
-local bulletURL = "http://localhost:8080/createBoard?bname=hello%20world&author=oshiro&key=0&msg=hogehoge"
-
+local bulletURL = "http://bldsv12.cr.ie.u-ryukyu.ac.jp:8080/createBoard?bname=hello%20world&author=oshiro&key=0&msg=hogehoge"
+--local bulletURL = "http://mass00.cs.ie.u-ryukyu.ac.jp:8080/createBoard?bname=hello%20world&author=oshiro&key=0&msg=hogehoge"
 b, c, h = http.request
 {
---    url = bulletURL,
-    url = "http://www.google.com",
-    method = "GET",
---    method = "POST",
+    url = bulletURL,
+    method = "POST",
     sink = ltn12.sink.file(io.stdout)
 }
 
-print("")
-print("b = "..b)
-print("c = "..c)
-
-for i,v in pairs(h) do print(i,v) end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/http/slaxdom.lua	Sat Sep 28 05:47:24 2013 +0900
@@ -0,0 +1,49 @@
+-- Optional parser that creates a flat DOM from parsing
+local SLAXML = require 'slaxml'
+function SLAXML:dom(xml,opts)
+	if not opts then opts={} end
+	local rich = not opts.simple
+	local push, pop = table.insert, table.remove
+	local stack = {}
+	local doc = { type="document", name="#doc", kids={} }
+	local current = doc
+	local builder = SLAXML:parser{
+		startElement = function(name,nsURI)
+			local el = { type="element", name=name, kids={}, el=rich and {} or nil, attr={}, nsURI=nsURI, parent=rich and current or nil }
+			if current==doc then
+				if doc.root then error(("Encountered element '%s' when the document already has a root '%s' element"):format(name,doc.root.name)) end
+				doc.root = el
+			end
+			push(current.kids,el)
+			if current.el then push(current.el,el) end
+			current = el
+			push(stack,el)
+		end,
+		attribute = function(name,value,nsURI)
+			if not current or current.type~="element" then error(("Encountered an attribute %s=%s but I wasn't inside an element"):format(name,value)) end
+			local attr = {type='attribute',name=name,nsURI=nsURI,value=value,parent=rich and current or nil}
+			if rich then current.attr[name] = value end
+			push(current.attr,attr)
+		end,
+		closeElement = function(name)
+			if current.name~=name or current.type~="element" then error(("Received a close element notification for '%s' but was inside a '%s' %s"):format(name,current.name,current.type)) end
+			pop(stack)
+			current = stack[#stack]
+		end,
+		text = function(value)
+			if current.type~='document' then
+				if current.type~="element" then error(("Received a text notification '%s' but was inside a %s"):format(value,current.type)) end
+				push(current.kids,{type='text',name='#text',value=value,parent=rich and current or nil})
+			end
+		end,
+		comment = function(value)
+			push(current.kids,{type='comment',name='#comment',value=value,parent=rich and current or nil})
+		end,
+		pi = function(name,value)
+			push(current.kids,{type='pi',name=name,value=value,parent=rich and current or nil})
+		end
+	}
+	builder:parse(xml,opts)
+	return doc
+end
+return SLAXML
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/http/slaxml.lua	Sat Sep 28 05:47:24 2013 +0900
@@ -0,0 +1,219 @@
+--[=====================================================================[
+v0.5.1 Copyright © 2013 Gavin Kistner <!@phrogz.net>; MIT Licensed
+See http://github.com/Phrogz/SLAXML for details.
+--]=====================================================================]
+local SLAXML = {
+	VERSION = "0.5.1",
+	_call = {
+		pi = function(target,content)
+			print(string.format("<?%s %s?>",target,content))
+		end,
+		comment = function(content)
+			print(string.format("<!-- %s -->",content))
+		end,
+		startElement = function(name,nsURI)
+			print(string.format("<%s%s>",name,nsURI and (" ("..nsURI..")") or ""))
+		end,
+		attribute = function(name,value,nsURI)
+			print(string.format("  %s=%q%s",name,value,nsURI and (" ("..nsURI..")") or ""))
+		end,
+		text = function(text)
+			print(string.format("  text: %q",text))
+		end,
+		closeElement = function(name,nsURI)
+			print(string.format("</%s>",name))
+		end,
+	}
+}
+
+function SLAXML:parser(callbacks)
+	return { _call=callbacks or self._call, parse=SLAXML.parse }
+end
+
+function SLAXML:parse(xml,options)
+	if not options then options = { stripWhitespace=false } end
+
+	-- Cache references for maximum speed
+	local find, sub, gsub, char, push, pop = string.find, string.sub, string.gsub, string.char, table.insert, table.remove
+	local first, last, match1, match2, match3, pos2, nsURI
+	local pos = 1
+	local state = "text"
+	local textStart = 1
+	local currentElement={}
+	local currentAttributes={}
+	local currentAttributeCt
+	local nsStack = {}
+
+	local entityMap  = { ["lt"]="<", ["gt"]=">", ["amp"]="&", ["quot"]='"', ["apos"]="'" }
+	local entitySwap = function(orig,n,s) return entityMap[s] or n=="#" and char(s) or orig end
+	local function unescape(str) return gsub( str, '(&(#?)([%d%a]+);)', entitySwap ) end
+
+	local function finishText()
+		if first>textStart and self._call.text then
+			local text = sub(xml,textStart,first-1)
+			if options.stripWhitespace then
+				text = gsub(text,'^%s+','')
+				text = gsub(text,'%s+$','')
+				if #text==0 then text=nil end
+			end
+			if text then self._call.text(unescape(text)) end
+		end
+	end
+
+	local function findPI()
+		first, last, match1, match2 = find( xml, '^<%?([:%a_][:%w_.-]*) ?(.-)%?>', pos )
+		if first then
+			finishText()
+			if self._call.pi then self._call.pi(match1,match2) end
+			pos = last+1
+			textStart = pos
+			return true
+		end
+	end
+
+	local function findComment()
+		first, last, match1 = find( xml, '^<!%-%-(.-)%-%->', pos )
+		if first then
+			finishText()
+			if self._call.comment then self._call.comment(match1) end
+			pos = last+1
+			textStart = pos
+			return true
+		end
+	end
+
+	local function nsForPrefix(prefix)
+		for i=#nsStack,1,-1 do if nsStack[i][prefix] then return nsStack[i][prefix] end end
+		error(("Cannot find namespace for prefix %s"):format(prefix))
+	end
+
+	local function startElement()
+		first, last, match1 = find( xml, '^<([%a_][%w_.-]*)', pos )
+		if first then
+			currentElement[2] = nil
+			finishText()
+			pos = last+1
+			first,last,match2 = find(xml, '^:([%a_][%w_.-]*)', pos )
+			if first then
+				currentElement[1] = match2
+				currentElement[2] = nsForPrefix(match1)
+				match1 = match2
+				pos = last+1
+			else
+				currentElement[1] = match1
+				for i=#nsStack,1,-1 do if nsStack[i]['!'] then currentElement[2] = nsStack[i]['!']; break end end
+			end
+			currentAttributeCt = 0
+			push(nsStack,{})
+			return true
+		end
+	end
+
+	local function findAttribute()
+		first, last, match1 = find( xml, '^%s+([:%a_][:%w_.-]*)%s*=%s*', pos )
+		if first then
+			pos2 = last+1
+			first, last, match2 = find( xml, '^"([^<"]*)"', pos2 ) -- FIXME: disallow non-entity ampersands
+			if first then
+				pos = last+1
+				match2 = unescape(match2)
+			else
+				first, last, match2 = find( xml, "^'([^<']*)'", pos2 ) -- FIXME: disallow non-entity ampersands
+				if first then
+					pos = last+1
+					match2 = unescape(match2)
+				end
+			end
+		end
+		if match1 and match2 then
+			local currentAttribute = {match1,match2}
+			local prefix,name = string.match(match1,'^([^:]+):([^:]+)$')
+			if prefix then
+				if prefix=='xmlns' then
+					nsStack[#nsStack][name] = match2
+				else
+					currentAttribute[1] = name
+					currentAttribute[3] = nsForPrefix(prefix)
+				end
+			else
+				if match1=='xmlns' then
+					nsStack[#nsStack]['!'] = match2
+					currentElement[2] = match2
+				end
+			end
+			currentAttributeCt = currentAttributeCt + 1
+			currentAttributes[currentAttributeCt] = currentAttribute
+			return true
+		end
+	end
+
+	local function findCDATA()
+		first, last, match1 = find( xml, '^<!%[CDATA%[(.-)%]%]>', pos )
+		if first then
+			finishText()
+			if self._call.text then self._call.text(match1) end
+			pos = last+1
+			textStart = pos
+			return true
+		end
+	end
+
+	local function closeElement()
+		first, last, match1 = find( xml, '^%s*(/?)>', pos )
+		if first then
+			state = "text"
+			pos = last+1
+			textStart = pos
+
+			if self._call.startElement then self._call.startElement(unpack(currentElement)) end
+			if self._call.attribute then
+			for i=1,currentAttributeCt do self._call.attribute(unpack(currentAttributes[i])) end end
+
+			if match1=="/" then
+				pop(nsStack)
+				if self._call.closeElement then self._call.closeElement(unpack(currentElement)) end
+			end
+			return true
+		end
+	end
+
+	local function findElementClose()
+		first, last, match1, match2 = find( xml, '^</([%a_][%w_.-]*)%s*>', pos )
+		if first then
+			nsURI = nil
+			for i=#nsStack,1,-1 do if nsStack[i]['!'] then nsURI = nsStack[i]['!']; break end end
+		else
+			first, last, match2, match1 = find( xml, '^</([%a_][%w_.-]*):([%a_][%w_.-]*)%s*>', pos )
+			if first then nsURI = nsForPrefix(match2) end
+		end
+		if first then
+			finishText()
+			if self._call.closeElement then self._call.closeElement(match1,nsURI) end
+			pos = last+1
+			textStart = pos
+			pop(nsStack)
+			return true
+		end
+	end
+
+	while pos<#xml do
+		if state=="text" then
+			if not (findPI() or findComment() or findCDATA() or findElementClose()) then		
+				if startElement() then
+					state = "attributes"
+				else
+					first, last = find( xml, '^[^<]+', pos )
+					pos = (first and last or pos) + 1
+				end
+			end
+		elseif state=="attributes" then
+			if not findAttribute() then
+				if not closeElement() then
+					error("Was in an element and couldn't find attributes or the close.")
+				end
+			end
+		end
+	end
+end
+
+return SLAXML
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/http/win_rate.lua	Sat Sep 28 05:47:24 2013 +0900
@@ -0,0 +1,29 @@
+package.path = '../?.lua;' .. package.path
+
+local SLAXML = require("slaxdom")
+local io = require("io")
+local http = require("socket.http")
+local ltn12 = require("ltn12")
+
+local url = "http://www.lolking.net/summoner/na/41645712#history"
+local request = {}
+b, c, h = http.request
+{
+    url = url,
+    method = "GET",
+--    sink = ltn12.sink.file(io.stdout)
+    sink = ltn12.sink.table(request)
+}
+
+local xml = table.concat(request)
+local doc = SLAXML:dom(xml)
+
+for i,v in pairs(doc) do print(i,v) end
+
+local kids = doc["kids"]
+print("kids:",kids)
+for i,v in pairs(kids) do 
+  print("kids:for")
+  print(i,v) 
+end
+