Advertisement
CaptainSpaceCat

CodeCounter

Jul 31st, 2015
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.91 KB | None | 0 0
  1. local w, h = term.getSize()
  2. local oTemp = fs.open("console", "w")
  3. function log(string)
  4.         oTemp.writeLine(string)
  5.         oTemp.flush()
  6. end
  7.  
  8. function beginWrite(str)
  9.     local str = str or ""
  10.     term.setTextColor(colors.white)
  11.     term.setBackgroundColor(colors.black)
  12.     term.clear()
  13.     term.setCursorPos(1, 1)
  14.     write(str)
  15. end
  16.  
  17. function getPastes(html)
  18.     local pastes = {}
  19.     while #html > 0 do
  20.         local _, st = html:find("<paste_key>")
  21.         local en = html:find("</paste_key>")
  22.         pastes[#pastes + 1] = html:sub(st + 1, en - 1)
  23.         _, en = html:find("</paste>")
  24.         html = html:sub(en + 1)
  25.     end
  26.     return pastes
  27. end
  28.  
  29. function drawProgressBar(remaining, total)
  30.     term.setBackgroundColor(colors.lightBlue)
  31.     term.setCursorPos(1, h)
  32.     write(string.rep(" ", ((total - remaining) / total) * w))
  33. end
  34.  
  35. function getNum(pastes)
  36.     local num = 0
  37.     local len = #pastes
  38.     local count = 1
  39.     while #pastes > 0 do
  40.         local current = table.remove(pastes)
  41.         term.setBackgroundColor(colors.black)
  42.         term.setCursorPos(1, count < h and count or h)
  43.         write("Connecting to " .. current .. "...")
  44.         print(string.rep(" ", w - 25))
  45.         drawProgressBar(#pastes, len)
  46.         local response = http.get(
  47. "http://pastebin.com/raw.php?i=" .. textutils.urlEncode(current)
  48. )
  49.         local response = response.readAll()
  50.         local newLine = 0
  51.         for i in string.gmatch(response, "\n") do
  52.             newLine = newLine + 1
  53.         end
  54.         num = num + newLine + 1
  55.         count = count + 1
  56.     end
  57.     return num
  58. end
  59.  
  60. term.setBackgroundColor(colors.black)
  61. term.setTextColor(colors.white)
  62. term.clear()
  63. term.setCursorPos(1, 1)
  64. term.setTextColor(colors.blue)
  65. print("Pastebin CodeCounter")
  66. term.setTextColor(colors.cyan)
  67. write("Enter Username: ")
  68. term.setTextColor(colors.white)
  69. local username = read()
  70. term.setCursorPos(1, 3)
  71. term.setTextColor(colors.cyan)
  72. write("Enter Password :) : ")
  73. term.setTextColor(colors.white)
  74. local password = read("*")
  75. term.setCursorPos(1, 4)
  76. term.setTextColor(colors.cyan)
  77. write("Paste dev key here: ")
  78. term.setTextColor(colors.yellow)
  79. local devkey = read()
  80.  
  81. beginWrite("Logging in...")
  82. local postdata = http.post(
  83.         "http://pastebin.com/api/api_login.php",
  84.         "api_option=login&"..
  85.         "api_dev_key="..devkey.."&"..
  86.         "api_user_name="..textutils.urlEncode(username).."&"..
  87.         "api_user_password="..textutils.urlEncode(password)
  88. )  
  89. local userkey = postdata.readAll()
  90. if #userkey ~= 32 then
  91.     error("Invalid login")
  92. end
  93.  
  94. beginWrite("Retrieving paste list...")
  95. local list = http.post("http://pastebin.com/api/api_post.php",
  96. "api_dev_key=" .. devkey ..
  97. "&api_user_key=" .. userkey ..
  98. "&api_results_limit=1000" ..
  99. "&api_option=list")
  100. list = list.readAll()
  101. beginWrite()
  102. local pastes = getPastes(list)
  103. local num = getNum(pastes)
  104. log(num)
  105. log("=====")
  106. log(list)
  107. beginWrite()
  108. write("You have written approximately ")
  109. term.setTextColor(colors.lightBlue)
  110. write(tostring(num))
  111. term.setTextColor(colors.white)
  112. write(" lines of code so far!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement