Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function getInfo(label)
- local id
- while true do
- term.clear()
- term.setCursorPos(1, 1)
- print("Enter the " .. label .. " of the post to get code from:")
- id = tonumber(read())
- if id then break end
- print("Error: That is not a number. Please try again.")
- sleep(3)
- end
- term.clear()
- term.setCursorPos(1, 1)
- return id
- end
- local function hasError(page)
- return string.find(page, "<h1 class='ipsType_pagetitle'>An Error Occurred</h1>") and true
- end
- local function getPage(url)
- local page
- for attempts=1,3 do
- print("Downloading Page: Attempt " .. attempts)
- page = http.get(url)
- if page then print("Download Successful!") break
- else print("Download Failed!") end
- if attempts < 3 then print("Retrying...") end
- end
- if not page then return end
- local html = page.readAll()
- page.close()
- return html
- end
- local function contains(tabl, item)
- for _,value in ipairs(tabl) do
- if value == item then return true end
- end
- return false
- end
- local function extractCode(page)
- local codes = {}
- local matches = 0
- print("Extracting Codes.")
- for code in string.gmatch(page, "<pre class='prettyprint'>(.-)</pre>") do
- if not contains(codes, code) then
- table.insert(codes, code)
- end
- end
- print(#codes .. " Code Blocks Found")
- return codes
- end
- local function makeURL(id, pageNum)
- return "http://www.computercraft.info/forums2/index.php?/topic/"..id.."-/page__st_" .. 20*(pageNum-1)
- end
- function unescape(codes)
- replace = {["quot"] = [["]], ["apos"] = [[']], ["amp"] = "&", ["lt"] = "<", ["gt"] = ">"}
- newcodes = {}
- for i,code in ipairs(codes) do
- table.insert(newcodes, ({string.gsub(code, "&#?(%w-);", function(word) return replace[word] or string.char(tonumber(word)) end)})[1])
- newcodes[#newcodes] = string.gsub(newcodes[#newcodes], "<br />", "\n")
- end
- return newcodes
- end
- local function displayCodes(codes)
- local codeLines = {}
- local termHeight = ({term.getSize()})[2]
- local lineUp = 0
- local lineDown = termHeight+1
- local menuOpen = false
- local codeNumber = 1
- local option = 1
- local options = {"[Save] Delete Exit", " Save [Delete] Exit", " Save Delete [Exit]"}
- for _,codeString in ipairs(codes) do
- local code={}
- codeString = "\n"..codeString.."\n"
- while true do
- local line, subPos = string.match(codeString, "\n(.-)()\n")
- if not line then break end
- table.insert(code, line)
- codeString = string.sub(codeString, subPos)
- end
- table.insert(codeLines, code)
- end
- loadDisplay(codeLines[codeNumber])
- while true do
- e,p = os.pullEvent("key")
- if not menuOpen then
- if p == keys.up then
- if lineUp >= 0 then
- term.scroll(-1)
- term.setCursorPos(1, 1)
- term.write(codeLines[codeNumber][lineUp])
- lineDown = lineDown - 1
- lineUp = lineUp - 1
- end
- elseif p == keys.down then
- if lineDown <= #codeLines[codeNumber] then
- term.scroll(1)
- term.setCursorPos(1, termHeight)
- term.write(codeLines[codeNumber][lineDown])
- lineDown = lineDown + 1
- lineUp = lineUp + 1
- end
- elseif p == keys.left then
- if codeNumber > 1 then
- codeNumber = codeNumber - 1
- loadDisplay(codeLines[codeNumber])
- lineUp = 0
- lineDown = termHeight+1
- end
- elseif p == keys.right then
- if codeNumber < #codeLines then
- codeNumber = codeNumber + 1
- loadDisplay(codeLines[codeNumber])
- lineUp = 0
- lineDown = termHeight+1
- end
- elseif p == keys.rightCtrl or p == keys.leftCtrl then
- term.setCursorPos(1, termHeight)
- option = 1
- term.clearLine()
- term.write(options[option])
- menuOpen = true
- end
- else
- if p == keys.leftCtrl or p == keys.rightCtrl then
- term.setCursorPos(1, termHeight)
- term.clearLine()
- if codeLines[codeNumber][lineDown-1] then term.write(codeLines[codeNumber][lineDown-1]) end
- menuOpen = false
- elseif p == keys.left then
- option = ((option-2)%3)+1
- term.setCursorPos(1, termHeight)
- term.clearLine()
- term.write(options[option])
- elseif p == keys.right then
- option = ((option)%3)+1
- term.setCursorPos(1, termHeight)
- term.clearLine()
- term.write(options[option])
- elseif p == keys.enter then
- if option == 1 then
- save(codeLines[codeNumber])
- table.remove(codeLines, codeNumber)
- codeNumber = 1
- if #codeLines < 1 then
- return true
- end
- loadDisplay(codeLines[codeNumber])
- menuOpen = false
- elseif option == 2 then
- table.remove(codeLines, codeNumber)
- codeNumber = 1
- if #codeLines < 1 then
- return true
- end
- loadDisplay(codeLines[codeNumber])
- lineUp = 0
- lineDown = termHeight+1
- menuOpen = false
- elseif option == 3 then return false
- end
- end
- end
- end
- end
- function loadDisplay(lines, offset)
- term.clear()
- term.setCursorPos(1, 1)
- offset = offset or 0
- local height = ({term.getSize()})[2]
- term.clear()
- term.setCursorPos(1, 1)
- for index = 1,height do
- term.setCursorPos(1, index)
- if lines[index+offset] then term.write(lines[index+offset]) else break end
- end
- end
- function save(lines)
- local saved = false
- while not saved do
- while not saved do
- term.clear()
- term.setCursorPos(1, 1)
- print("Please enter a valid filepath:")
- local path = read()
- if string.find(path, "[^%w%-_/]+") or string.find(path, "//") then print("Error: path contains invalid characters") sleep(3) break end
- if string.sub(path, 1, 1) ~= "/" then path = "/"..path end
- if string.sub(path, path:len()) ~= "/" then path = path.."/" end
- local pathParts = {}
- while true do
- local part, subPos = string.match(path, "/(.-)()/")
- if not part then break end
- table.insert(pathParts, part)
- path = string.sub(path, subPos)
- end
- local constructedPath = ""
- if #pathParts < 1 then print("Error: No file specified") sleep(3) end
- for i,pathPart in ipairs(pathParts) do
- if pathPart == "" then
- print("Error: Missing directory in path")
- sleep(3)
- break
- end
- constructedPath = constructedPath .. "/" .. pathPart
- if (not fs.exists(constructedPath)) and (i<#pathParts) then fs.makeDir(constructedPath)
- elseif fs.exists(constructedPath) and (not fs.isDir(constructedPath)) then print("Error: directory\n", constructedPath, "\nis already a file.") sleep(3) break
- elseif fs.exists(constructedPath) and i==#pathParts then print("Error: file\n", constructedPath, "\nalready exists.") sleep(3) break
- else
- print("Path: "..constructedPath)
- local file = fs.open(constructedPath, "w")
- if not file then print("An unknown error occured while opening the file.") sleep(3) break end
- file.write(table.concat(lines, "\n"))
- file.close()
- saved = true
- term.clear()
- term.setCursorPos(1, 1)
- print("Successfully saved code to:")
- print(constructedPath)
- sleep(3)
- break
- end
- end
- end
- end
- end
- local function main()
- local html
- repeat
- local url = makeURL(getInfo("ID#"), getInfo("page"))
- html = getPage(url)
- if not html then print("Too many failed attemps, try again later.") return false end
- local valid = not hasError(html)
- if not valid then print("An error occured. Your ID may be invalid.") end
- until valid
- local codes = extractCode(html)
- local codes = unescape(codes)
- if #codes == 0 then print("There is no code on that page") return true end
- print("Press any key to view collected code.")
- while true do local e,p = os.pullEvent("key") if p~=keys.escape then break end end
- if displayCodes(codes) then
- term.clear()
- term.setCursorPos(1, 1)
- print("There is no code left to display")
- print("Press any key to exit")
- while true do local e,p = os.pullEvent("key") if p~=keys.escape then break end end sleep(0)
- end
- term.clear()
- term.setCursorPos(1, 1)
- return true
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment