view http/win_rate.lua @ 7:ec8638157982

modified
author Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
date Sat, 28 Sep 2013 07:30:17 +0900
parents 18ba4378501e
children
line wrap: on
line source

package.path = '../?.lua;./?.lua;' .. package.path

local io = require("io")
local http = require("socket.http")
local ltn12 = require("ltn12")

local baseUrl = "http://www.lolking.net"
local userUrl = "/summoner/na/41645712"
local request = {}
local b, c, h = http.request
{
    url = baseUrl..userUrl,
    method = "GET",
    sink = ltn12.sink.table(request)
}

local xmlString = table.concat(request)
local match = xmlString:gmatch('<table>(.-)</table>')
local count = 0
local userInfoTable = {}
for i in match do 
  local names = i:gmatch('<td style="color: #FFF;">(.-)</td>')
  userInfoTable[count] = {}
  local userInfo = userInfoTable[count]
  for j in names do
    local href, name = j:match('<a href="(.-)">(.-)</a>')
    if (href == nil) then 
      userInfo[j] = {
	name = j,
	href = userUrl
      }
    else 
      userInfo[name] = {
	name = name,
	href = href
      }
    end
  end
  count = count + 1
  if (count > 2) then break end
end

function getWinNum(url)
  local request = {}
  local b, c, h = http.request 
  {
    url = url,
    method = "GET",
    sink = ltn12.sink.table(request)
  }
  local xmlString = table.concat(request)
  local winNum = xmlString:match('<td class="lifetime_stats_val" style="">(.-)</td>')
  return winNum
end

for j,l in pairs(userInfoTable) do
  local userInfo = userInfoTable[j]
  for i,v in pairs(userInfo) do 
    local userName = i
    local userTable = v
    local userUrl = v["href"]
    userTable['win'] = getWinNum(baseUrl..userUrl)
  end
end

for j,l in pairs(userInfoTable) do
  for i,v in pairs(l) do
    print(i.." "..v['win'])
  end
  print("")
end