Advertisement
1lann

olympics-pc

Aug 8th, 2012
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.50 KB | None | 0 0
  1. -- A program written by 1lann --
  2. -- For computers --
  3. -- Made using CC 1.41 --
  4.  
  5. url = "http://www.london2012.com/paralympics/medals/medal-count/"
  6. side = "back"
  7.  
  8. local function getResults()
  9. local country = {}
  10. local rank = {}
  11. local gold = {}
  12. local silver = {}
  13. local bronze = {}
  14. local total = {}
  15. local bStart = nil
  16. local bEnd = nil
  17. local bInsert = nil
  18. local derp = nil
  19. local rLine = nil
  20. f = io.open("/.tmpresults")
  21. for i = 1, 30 do
  22.     f:read("*l")
  23. end
  24. local rLine = f:read("*l")
  25. while string.find(rLine, "<span class=\"countryName\">")  do
  26.     derp, bStart = string.find(rLine, "<td class=\"position c\">")
  27.     bEnd = string.find(rLine, "</td>", bStart+1)
  28.     bStart = bStart+1
  29.     bEnd = bEnd-1
  30.     bInsert = string.sub(rLine, bStart, bEnd)
  31.     table.insert(rank, bInsert)
  32.     derp, bStart = string.find(rLine, "<span class=\"countryName\">")
  33.     bEnd = string.find(rLine, "</span>", bStart+1)
  34.     bStart = bStart+1
  35.     bEnd = bEnd-1
  36.     bInsert = string.sub(rLine, bStart, bEnd)
  37.     if string.len(bInsert) > 11 then
  38.         derp, bStart = string.find(rLine, "src=\"/imgml/flags/s/")
  39.         bEnd = string.find(rLine, ".png\"", bStart+1)
  40.         bStart = bStart+1
  41.         bEnd = bEnd-1
  42.         bInsert = string.sub(rLine, bStart, bEnd)
  43.     end
  44.     table.insert(country, bInsert)
  45.     derp, bStart = string.find(rLine, "<td class=\"gold c\">")
  46.     bEnd = string.find(rLine, "</td>", bStart+1)
  47.     bStart = bStart+1
  48.     bEnd = bEnd-1
  49.     bInsert = string.sub(rLine, bStart, bEnd)
  50.     table.insert(gold, bInsert)
  51.     derp, bStart = string.find(rLine, "<td class=\"silver c\">")
  52.     bEnd = string.find(rLine, "</td>", bStart+1)
  53.     bStart = bStart+1
  54.     bEnd = bEnd-1
  55.     bInsert = string.sub(rLine, bStart, bEnd)
  56.     table.insert(silver, bInsert)
  57.     derp, bStart = string.find(rLine, "<td class=\"bronze c\">")
  58.     bEnd = string.find(rLine, "</td>", bStart+1)
  59.     bStart = bStart+1
  60.     bEnd = bEnd-1
  61.     bInsert = string.sub(rLine, bStart, bEnd)
  62.     table.insert(bronze, bInsert)
  63.     f:read("*l")
  64.     f:read("*l")
  65.     rLine = f:read("*l")
  66.     derp, bStart = string.find(rLine, "<td class=\"total c\">")
  67.     bEnd = string.find(rLine, "</td>", bStart+1)
  68.     bStart = bStart+1
  69.     bEnd = bEnd-1
  70.     bInsert = string.sub(rLine, bStart, bEnd)
  71.     table.insert(total, bInsert)
  72. end
  73. f:close()
  74. return rank, country, gold, silver, bronze, total
  75. end
  76.  
  77. local function drawGUI(rank, country, gold, silver, bronze, total, scroll)
  78. while true do
  79.     term.clear()
  80.     term.setCursorPos(1,1)
  81.     write(" +----------------------------------------------+")
  82.     term.setCursorPos(1,2)
  83.     write(" | London 2012 Paralympics Medals    (By 1lann) |")
  84.     term.setCursorPos(1,3)
  85.     write(" | Rank | Country     |Gold |Silver|Bronze|Total|")
  86.     term.setCursorPos(1,4)
  87.     write(" +------+-------------+-----+------+------+-----+")
  88.     i = 0
  89.     for b = scroll, scroll+14 do
  90.         i = i + 1
  91.         term.setCursorPos(2,i+4)
  92.         write("| " .. rank[b])
  93.         term.setCursorPos(9,i+4)
  94.         write("| " .. country[b])
  95.         term.setCursorPos(23,i+4)
  96.         write("| " .. gold[b])
  97.         term.setCursorPos(29,i+4)
  98.         write("| " .. silver[b])
  99.         term.setCursorPos(36,i+4)
  100.         write("| " .. bronze[b])
  101.         term.setCursorPos(43,i+4)
  102.         write("| " .. total[b])
  103.         term.setCursorPos(49, i+4)
  104.         write("|")
  105.     end
  106.     term.setCursorPos(1, 19)
  107.     write(" +------+-------------+-----+------+------+-----+")
  108.     while true do
  109.     event, key = os.pullEvent()
  110.     if event == "key" then
  111.     if key == 200 and scroll ~= 1 then
  112.         scroll = scroll - 1
  113.         break
  114.     elseif key == 208 and scroll ~= #country - 14 then
  115.         scroll = scroll + 1
  116.         break
  117.     elseif key == 18 then
  118.         term.clear()
  119.         return
  120.     end
  121.     end
  122. end
  123. end
  124. end
  125.  
  126.  
  127.  
  128. local function load(fail, code)
  129.     if not fail then
  130.     f = io.open("/.tmpresults", "w")
  131.     f:write(code:readAll())
  132.     f:close()
  133.     code:close()
  134.     end
  135.     local country = {}
  136.     local rank = {}
  137.     local gold = {}
  138.     local silver = {}
  139.     local bronze = {}
  140.     local total = {}
  141.     rank, country, gold, silver, bronze, total = getResults()
  142.     scroll = 1
  143.     drawGUI(rank, country, gold, silver, bronze, total, scroll)
  144. end
  145.  
  146. local code = nil
  147. term.setCursorBlink(false)
  148. print("Press E to Exit")
  149. textutils.slowPrint("Downloading results...")
  150. sTimer = os.startTimer(45)
  151. http.request(url)
  152. while true do
  153.     event, a, code = os.pullEvent()
  154.     if event == "http_failure" then
  155.         term.clearLine()
  156.         print("Connection failed!")
  157.         fail = true
  158.         break
  159.     elseif event == "http_success" then
  160.             fail = false
  161.             break
  162.     elseif event == "timer" and a == sTimer then
  163.             print("Connection timed out!")
  164.             fail = true
  165.             break
  166.     end
  167. end
  168.     if code == nil and fail == false then
  169.         print("Page not found!")
  170.         fail =  true
  171.     end
  172.     if not fail then
  173.         load(false, code)
  174.     else
  175.         error()
  176.     end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement