Advertisement
1lann

olympics

Aug 4th, 2012
594
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.42 KB | None | 0 0
  1. -- A program written by 1lann --
  2. -- FOR MONITORS ONLY --
  3. -- MONITOR MUST BE 8 WIDE --
  4.  
  5. url = "http://www.london2012.com/paralympics/medals/medal-count/"
  6. side = "back"
  7. noObjects = 25
  8.  
  9. local function getResults()
  10. local country = {}
  11. local rank = {}
  12. local gold = {}
  13. local silver = {}
  14. local bronze = {}
  15. local total = {}
  16. local bStart = nil
  17. local bEnd = nil
  18. local bInsert = nil
  19. local derp = nil
  20. local rLine = nil
  21. f = io.open("/.tmpresults")
  22. for i = 1, 30 do
  23.     f:read("*l")
  24. end
  25. local rLine = f:read("*l")
  26. for i = 1, noObjects do
  27.     derp, bStart = string.find(rLine, "<td class=\"position c\">")
  28.     bEnd = string.find(rLine, "</td>", bStart+1)
  29.     bStart = bStart+1
  30.     bEnd = bEnd-1
  31.     bInsert = string.sub(rLine, bStart, bEnd)
  32.     table.insert(rank, bInsert)
  33.     derp, bStart = string.find(rLine, "<span class=\"countryName\">")
  34.     bEnd = string.find(rLine, "</span>", bStart+1)
  35.     bStart = bStart+1
  36.     bEnd = bEnd-1
  37.     bInsert = string.sub(rLine, bStart, bEnd)
  38.     table.insert(country, bInsert)
  39.     derp, bStart = string.find(rLine, "<td class=\"gold c\">")
  40.     bEnd = string.find(rLine, "</td>", bStart+1)
  41.     bStart = bStart+1
  42.     bEnd = bEnd-1
  43.     bInsert = string.sub(rLine, bStart, bEnd)
  44.     table.insert(gold, bInsert)
  45.     derp, bStart = string.find(rLine, "<td class=\"silver 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(silver, bInsert)
  51.     derp, bStart = string.find(rLine, "<td class=\"bronze 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(bronze, bInsert)
  57.     f:read("*l")
  58.     f:read("*l")
  59.     rLine = f:read("*l")
  60.     derp, bStart = string.find(rLine, "<td class=\"total c\">")
  61.     bEnd = string.find(rLine, "</td>", bStart+1)
  62.     bStart = bStart+1
  63.     bEnd = bEnd-1
  64.     bInsert = string.sub(rLine, bStart, bEnd)
  65.     table.insert(total, bInsert)
  66. end
  67. f:close()
  68. return rank, country, gold, silver, bronze, total
  69. end
  70.  
  71. local function drawGUI(rank, country, gold, silver, bronze, total)
  72.     mon.clear()
  73.     mon.setCursorPos(1,2)
  74.     mon.write(" +-------------------------------------------------------------------------+")
  75.     mon.setCursorPos(1,3)
  76.     mon.write(" | London 2012 Olympics: Top " .. noObjects .. " Countries (Updated every 5 minutes)        |")
  77.     mon.setCursorPos(1,4)
  78.     mon.write(" | Rank | Country                                |Gold |Silver|Bronze|Total|")
  79.     mon.setCursorPos(1,5)
  80.     mon.write(" +------+----------------------------------------+-----+------+------+-----+")
  81.     for i = 1, #country do
  82.         mon.setCursorPos(2,i+5)
  83.         mon.write("| " .. rank[i])
  84.         mon.setCursorPos(9,i+5)
  85.         mon.write("| " .. country[i])
  86.         mon.setCursorPos(50,i+5)
  87.         mon.write("| " .. gold[i])
  88.         mon.setCursorPos(56,i+5)
  89.         mon.write("| " .. silver[i])
  90.         mon.setCursorPos(63,i+5)
  91.         mon.write("| " .. bronze[i])
  92.         mon.setCursorPos(70,i+5)
  93.         mon.write("| " .. total[i])
  94.         mon.setCursorPos(76, i+5)
  95.         mon.write("|")
  96.     end
  97.     mon.setCursorPos(1, #country+6)
  98.     mon.write(" +------+----------------------------------------+-----+------+------+-----+")
  99. end
  100.  
  101.  
  102.  
  103. local function load(fail, code)
  104.     if not fail then
  105.     f = io.open("/.tmpresults", "w")
  106.     f:write(code:readAll())
  107.     f:close()
  108.     code:close()
  109.     end
  110.     local country = {}
  111.     local rank = {}
  112.     local gold = {}
  113.     local silver = {}
  114.     local bronze = {}
  115.     local total = {}
  116.     rank, country, gold, silver, bronze, total = getResults()
  117.     drawGUI(rank, country, gold, silver, bronze, total)
  118.     print("Waiting...")
  119.     if fail then
  120.         sleep(30)
  121.     else
  122.     sleep(300)
  123. end
  124. end
  125.  
  126. local code = nil
  127. mon = peripheral.wrap(side)
  128. print("2012 London Olympic Games Scoreboard")
  129. print("For 8 wide monitors only!")
  130. print("Made by 1lann")
  131. term.setCursorBlink(false)
  132. mon.setCursorBlink(false)
  133. while true do
  134. textutils.slowPrint("Downloading results...")
  135. mon.setTextScale(1)
  136. mon.clear()
  137. mon.setCursorPos(1,1)
  138. os.startTimer(45)
  139. http.request(url)
  140. while true do
  141.     event, a, code = os.pullEvent()
  142.     if event == "http_failure" or event == "timer" then
  143.         term.clearLine()
  144.         print("Connection failed!")
  145.         fail = true
  146.         break
  147.     elseif event == "http_success" then
  148.             fail = false
  149.             break
  150.     end
  151. end
  152.     if code == nil and fail == false then
  153.         term.clearLine()
  154.         print("Page not found!")
  155.         fail = true
  156.     end
  157.     if not fail then
  158.         load(false, code)
  159.     elseif fs.exists("/.tmpresults") then
  160.         print("Failure warning")
  161.         load(fail, code)
  162.     else
  163.         print("Retrying in 30 seconds...")
  164.         sleep(30)
  165.     end
  166. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement