Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local http = require "socket.http"
- -- full of bugs
- local MT = {}
- MT.__index = MT
- function Node(name, attributes)
- return setmetatable({name = name, children = { }, attributes = attributes or { }}, MT)
- end
- function MT:getChild(name)
- for _, node in ipairs(self.children) do
- if node.name == name then
- return node
- end
- end
- return nil
- end
- function MT:getChildren(name)
- if not name then
- local idx = 1
- return function(lol)
- local child = self.children[idx]
- if child then
- idx = idx + 1
- return child
- end
- end
- end
- local idx = 1
- return function()
- for i = idx, #self.children do
- local child = self.children[i]
- if child and child.name == name then
- idx = i + 1
- return child
- end
- end
- end
- end
- function MT:getAttribute(name, func)
- local attr = self.attributes[name]
- if attr then
- return func and func(attr) or attr
- end
- end
- local XML_PARENTNODE = 0
- local XML_CLOSENODE = 1
- local XML_SIMPLENODE = 2
- local function parseNode(s)
- local c1, name, attrString, c2 = s:match('^<%s*(/?)%s*([%w_]+)%s-(.-)(/?)%s*>$')
- assert(name, 'XML Error in:\n' .. s .. '\nNode name not found.')
- if c1 == '/' then
- return XML_CLOSENODE, name
- end
- local attributes = {}
- if attrString then
- for name, value in attrString:gmatch([[([%w_]-)%s*=%s*(%b"")%s*]]) do
- assert(name and value, 'XML Error in:\n' .. s .. '\nCould not parse attribute name and value.')
- attributes[name] = value:sub(2, -2)
- end
- for name, value in attrString:gmatch([[([%w_]-)%s*=%s*(%b'')%s*]]) do
- assert(name and value, 'XML Error in:\n' .. s .. '\nCould not parse attribute name and value.')
- attributes[name] = value:sub(2, -2)
- end
- end
- if c2 == '/' then
- return XML_SIMPLENODE, name, attributes
- else
- return XML_PARENTNODE, name, attributes
- end
- end
- local function parseString(s)
- local doc = Node()
- local stack = {doc}
- for n in s:gmatch('%b<>') do
- local current = stack[#stack]
- local ret, name, attributes = parseNode(n)
- if ret == XML_SIMPLENODE then
- table.insert(current.children, Node(name, attributes))
- elseif ret == XML_PARENTNODE then
- local node = Node(name, attributes)
- table.insert(current.children, node)
- table.insert(stack, node)
- elseif ret == XML_CLOSENODE then
- assert(current.name == name, 'XML Error in:\n' .. n .. '\nAttempt to close unknown node.')
- table.remove(stack, #stack)
- end
- end
- return doc
- end
- local function parseFile(filename)
- local handle = assert(io.open(filename, 'r'))
- local data = assert(handle:read('*all'))
- handle:close()
- data = data:gsub("<%?.-%?>", "")
- data = data:gsub("<!%-%-.-%-%->", "")
- return parseString(data)
- end
- local wiki = {
- ["Giant Spider Wyda"] = "Giant_Spider_(Wyda)",
- ["Avalanche"] = "Avalanche_(Creature)",
- ["Kraknaknork Demon"] = "Askarak_Demon",
- ["Man In The Cave"] = "Man_in_the_Cave",
- ["Pythius The Rotten"] = "Pythius_the_Rotten_(Creature)",
- ["Yaga The Crone"] = "Yaga_the_Crone",
- ["War wolf"] = "War_Wolf",
- ["Winter wolf"] = "Winter_Wolf",
- ["Blood Crab Underwater"] = "Blood_Crab_(Underwater)",
- ["Deepling worker"] = "Deepling_Worker",
- ["Fish"] = "Fish_(Creature)",
- ["Northern Pike"] = "Northern_Pike_(Creature)",
- ["Phantasm Summon"] = "Phantasm",
- ["Blue Butterfly"] = "Butterfly_(Blue)",
- ["Butterfly"] = "Butterfly_(Blue)",
- ["Pink Butterfly"] = "Butterfly_(Blue)",
- ["Purple Butterfly"] = "Butterfly_(Purple)",
- ["Red Butterfly"] = "Butterfly_(Red)",
- ["Yellow Butterfly"] = "Butterfly_(Yellow)",
- ["Yalahari"] = "Yalahari_(Creature)",
- ["Armenius"] = "Armenius_(Creature)",
- ["Horse1"] = "Horse",
- ["Horse2"] = "Horse",
- ["Horse3"] = "Horse",
- }
- function wikify(text)
- return wiki[text] or text:gsub(" ", "_")
- end
- local path = "./monster/"
- local data = parseFile(path .. "monsters.xml")
- local outfound = ""
- local outnotfound = ""
- local outfailed = ""
- for node in data:getChild("monsters"):getChildren("monster") do
- local name = node:getAttribute("name")
- local html = http.request("http://tibia.wikia.com/wiki/" .. wikify(name))
- local around = html:match('<td class="property">Walks around</td>.-<td class="value">(.-)</td>')
- local through = html:match('<td class="property">Walks through</td>.-<td class="value">(.-)</td>')
- if not around or not through then
- outfailed = outfailed .. "Failed to fetch information about " .. name .. "\n"
- else
- around = around:gsub("<br />", ", ")
- through = through:gsub("<br />", ", ")
- if #around == 0 and #through == 0 then
- outnotfound = outnotfound .. ("%s: No information found.\n"):format(name)
- else
- outfound = outfound .. ("%s: Around: %s // Through: %s\n"):format(name, around, through)
- end
- end
- end
- print("Found:")
- print(outfound)
- print("Not found:")
- print(outnotfound)
- print("Failed:")
- print(outfailed)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement