Advertisement
Mkalo

Untitled

Sep 7th, 2016
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.19 KB | None | 0 0
  1. local http = require "socket.http"
  2. -- full of bugs
  3.  
  4. local MT = {}
  5. MT.__index = MT
  6.  
  7. function Node(name, attributes)
  8.     return setmetatable({name = name, children = { }, attributes = attributes or { }}, MT)
  9. end
  10.  
  11. function MT:getChild(name)
  12.     for _, node in ipairs(self.children) do
  13.         if node.name == name then
  14.             return node
  15.         end
  16.     end
  17.     return nil
  18. end
  19.  
  20. function MT:getChildren(name)
  21.     if not name then
  22.         local idx = 1
  23.         return function(lol)
  24.             local child = self.children[idx]
  25.             if child then
  26.                 idx = idx + 1
  27.                 return child
  28.             end
  29.         end
  30.     end
  31.  
  32.     local idx = 1
  33.     return function()
  34.         for i = idx, #self.children do
  35.             local child = self.children[i]
  36.             if child and child.name == name then
  37.                 idx = i + 1
  38.                 return child
  39.             end
  40.         end
  41.     end
  42. end
  43.  
  44. function MT:getAttribute(name, func)
  45.     local attr = self.attributes[name]
  46.     if attr then
  47.         return func and func(attr) or attr
  48.     end
  49. end
  50.  
  51. local XML_PARENTNODE = 0
  52. local XML_CLOSENODE  = 1
  53. local XML_SIMPLENODE = 2
  54.  
  55. local function parseNode(s)
  56.     local c1, name, attrString, c2 = s:match('^<%s*(/?)%s*([%w_]+)%s-(.-)(/?)%s*>$')
  57.     assert(name, 'XML Error in:\n' .. s .. '\nNode name not found.')
  58.  
  59.     if c1 == '/' then
  60.         return XML_CLOSENODE, name
  61.     end
  62.  
  63.     local attributes = {}
  64.     if attrString then
  65.         for name, value in attrString:gmatch([[([%w_]-)%s*=%s*(%b"")%s*]]) do
  66.             assert(name and value, 'XML Error in:\n' .. s .. '\nCould not parse attribute name and value.')
  67.             attributes[name] = value:sub(2, -2)
  68.         end
  69.  
  70.         for name, value in attrString:gmatch([[([%w_]-)%s*=%s*(%b'')%s*]]) do
  71.             assert(name and value, 'XML Error in:\n' .. s .. '\nCould not parse attribute name and value.')
  72.             attributes[name] = value:sub(2, -2)
  73.         end
  74.     end
  75.  
  76.     if c2 == '/' then
  77.         return XML_SIMPLENODE, name, attributes
  78.     else
  79.         return XML_PARENTNODE, name, attributes
  80.     end
  81. end
  82.  
  83.  
  84. local function parseString(s)
  85.     local doc = Node()
  86.     local stack = {doc}
  87.     for n in s:gmatch('%b<>') do
  88.         local current = stack[#stack]
  89.         local ret, name, attributes = parseNode(n)
  90.         if ret == XML_SIMPLENODE then
  91.             table.insert(current.children, Node(name, attributes))
  92.         elseif ret == XML_PARENTNODE then
  93.             local node = Node(name, attributes)
  94.             table.insert(current.children, node)
  95.             table.insert(stack, node)
  96.         elseif ret == XML_CLOSENODE then
  97.             assert(current.name == name, 'XML Error in:\n' .. n .. '\nAttempt to close unknown node.')
  98.             table.remove(stack, #stack)
  99.         end
  100.     end
  101.     return doc
  102. end
  103.  
  104. local function parseFile(filename)
  105.     local handle = assert(io.open(filename, 'r'))
  106.     local data = assert(handle:read('*all'))
  107.     handle:close()
  108.     data = data:gsub("<%?.-%?>", "")
  109.     data = data:gsub("<!%-%-.-%-%->", "")
  110.     return parseString(data)
  111. end
  112.  
  113. local wiki = {
  114.     ["Giant Spider Wyda"] = "Giant_Spider_(Wyda)",
  115.     ["Avalanche"] = "Avalanche_(Creature)",
  116.     ["Kraknaknork Demon"] = "Askarak_Demon",
  117.     ["Man In The Cave"] = "Man_in_the_Cave",
  118.     ["Pythius The Rotten"] = "Pythius_the_Rotten_(Creature)",
  119.     ["Yaga The Crone"] = "Yaga_the_Crone",
  120.     ["War wolf"] = "War_Wolf",
  121.     ["Winter wolf"] = "Winter_Wolf",
  122.     ["Blood Crab Underwater"] = "Blood_Crab_(Underwater)",
  123.     ["Deepling worker"] = "Deepling_Worker",
  124.     ["Fish"] = "Fish_(Creature)",
  125.     ["Northern Pike"] = "Northern_Pike_(Creature)",
  126.     ["Phantasm Summon"] = "Phantasm",
  127.     ["Blue Butterfly"] = "Butterfly_(Blue)",
  128.     ["Butterfly"] = "Butterfly_(Blue)",
  129.     ["Pink Butterfly"] = "Butterfly_(Blue)",
  130.     ["Purple Butterfly"] = "Butterfly_(Purple)",
  131.     ["Red Butterfly"] = "Butterfly_(Red)",
  132.     ["Yellow Butterfly"] = "Butterfly_(Yellow)",
  133.     ["Yalahari"] = "Yalahari_(Creature)",
  134.     ["Armenius"] = "Armenius_(Creature)",
  135.     ["Horse1"] = "Horse",
  136.     ["Horse2"] = "Horse",
  137.     ["Horse3"] = "Horse",
  138. }
  139.  
  140.  
  141. function wikify(text)
  142.     return wiki[text] or text:gsub(" ", "_")
  143. end
  144.  
  145. local path = "./monster/"
  146. local data = parseFile(path .. "monsters.xml")
  147. local outfound = ""
  148. local outnotfound = ""
  149. local outfailed = ""
  150. for node in data:getChild("monsters"):getChildren("monster") do
  151.     local name = node:getAttribute("name")
  152.     local html = http.request("http://tibia.wikia.com/wiki/" .. wikify(name))
  153.     local around = html:match('<td class="property">Walks around</td>.-<td class="value">(.-)</td>')
  154.     local through = html:match('<td class="property">Walks through</td>.-<td class="value">(.-)</td>')
  155.     if not around or not through then
  156.         outfailed = outfailed .. "Failed to fetch information about " .. name .. "\n"
  157.     else
  158.         around = around:gsub("<br />", ", ")
  159.         through = through:gsub("<br />", ", ")
  160.         if #around == 0 and #through == 0 then
  161.             outnotfound = outnotfound .. ("%s: No information found.\n"):format(name)
  162.         else
  163.             outfound = outfound .. ("%s: Around: %s // Through: %s\n"):format(name, around, through)
  164.         end
  165.     end
  166. end
  167.  
  168. print("Found:")
  169. print(outfound)
  170. print("Not found:")
  171. print(outnotfound)
  172. print("Failed:")
  173. print(outfailed)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement