Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function github_getRequestsLeft()
- local h = http.get("https://api.github.com/rate_limit")
- if h then
- local data = h.readAll()
- h.close()
- local s = string.match(data, "\"remaining\":(%d+)")
- if s then
- return tonumber(s) or 0
- end
- else
- return 0
- end
- end
- function github_getRequestLimit()
- local h = http.get("https://api.github.com/rate_limit")
- if h then
- local data = h.readAll()
- h.close()
- local s = string.match(data, "\"limit\":(%d+)")
- if s then
- return tonumber(s) or 0
- end
- else
- return 0
- end
- end
- function parse_contentsRequest(response)
- local tables = {}
- local id = 0
- for ident, data in string.gmatch(response, "\"(%a+)\":\"([^%\"]+)\",") do
- --local line = string.match(lines[i], "%s+(%.+)")
- --if line ~= "{" and line ~= "}" and line ~= "}," then
- -- local ident, data = string.match(line, "\"(%a+)\": \"([^%\"]+)\"")
- --print("["..ident.."] = "..data)
- if ident == "name" then
- id = id + 1
- tables[id] = {}
- tables[id]["name"] = data
- tables[id]["links"] = {}
- elseif ident == "self" or ident == "git" or ident == "html" then
- tables[id]["links"][ident] = data
- elseif ident ~= "_links" then
- tables[id][ident] = data
- end
- end
- return tables
- end
- function github_getContents(user, repo, path, verbose, fileNameColor, normalTextColor)
- if verbose then
- fileNameColor = fileNameColor or colors.orange
- normalTextColor = normalTextColor or colors.white
- write("Listing: ")
- term.setTextColor(fileNameColor)
- write(path)
- term.setTextColor(normalTextColor)
- write("...")
- end
- path = path or ""
- local url = "https://api.github.com/repos/"..user.."/"..repo.."/contents/"..path
- --print("HTTP GET: "..url)
- if github_getRequestsLeft() == 0 then
- return false, {}
- end
- local handle = http.get(url)
- if handle then
- local data = handle.readAll()
- --print("Retrieved "..#data.." bytes.")
- handle.close()
- --print(data)
- if verbose then
- print("Success.")
- end
- return true, parse_contentsRequest(data)
- else
- if verbose then
- print("Failed.")
- end
- return false, {}
- end
- end
- function github_getFile(user, repo, file, branch, verbose, fileNameColor, normalTextColor)
- if verbose then
- fileNameColor = fileNameColor or colors.orange
- normalTextColor = normalTextColor or colors.white
- write("Retrieving: ")
- term.setTextColor(fileNameColor)
- write(file)
- term.setTextColor(normalTextColor)
- write("...")
- end
- local url = "https://raw.github.com/"..user.."/"..repo.."/"..branch.."/"..file
- --print("HTTP GET: "..url)
- local handle = http.get(url)
- if handle then
- local data = handle.readAll()
- handle.close()
- if verbose then
- print("Success.")
- print("Retrieved "..#data.." bytes.")
- end
- return true, data
- else
- if verbose then
- print("Failed.")
- end
- return false
- end
- end
- function github_getAllFilesRecursive(user, repo, dir, branch, verbose, fileNameColor, normalTextColor)
- dir = dir or ""
- local files = {}
- local status, contents = github_getContents(user, repo, dir, verbose, fileNameColor, normalTextColor)
- if not status then
- return false
- end
- local totalFiles = 0
- local totalDirs = 0
- local failedFiles = 0
- local failedDirs = 0
- for i,v in ipairs(contents) do
- if v["type"] == "file" then
- totalFiles = totalFiles+1
- local status, data = github_getFile(user, repo, v["path"], branch, verbose, fileNameColor, normalTextColor)
- if status then
- files[v["path"]] = data
- else
- failedFiles = failedFiles+1
- end
- elseif v["type"] == "dir" then
- totalDirs = totalDirs+1
- local status, files2, fDirs, fFiles, tDirs, tFiles = github_getAllFilesRecursive(user, repo, v["path"], branch, verbose, fileNameColor, normalTextColor)
- if status then
- for i,v in pairs(files2) do
- files[i] = v
- end
- failedDirs = failedDirs+fDirs
- failedFiles = failedFiles+fFiles
- totalFiles = totalFiles + tFiles
- totalDirs = totalDirs + tDirs
- else
- failedDirs = failedDirs+1
- end
- end
- end
- return true, files, failedDirs, failedFiles, totalDirs, totalFiles
- end
- function pastebin_get(code)
- local wHandle = http.get("http://www.pastebin.com/raw.php?i="..code)
- if wHandle then
- local wData = wHandle.readAll()
- wHandle.close()
- return true, wData
- else
- return false
- end
- end
- local args = {...}
- local mode = args[1]
- local user = args[2]
- local localFile = args[3]
- local remoteFile = args[4]
- local remoteBranch = args[5]
- local remoteRepo = args[6]
- if mode == "get_dir" then
- local stat, files = github_getAllFilesRecursive(user, remoteRepo, remoteFile, remoteBranch, false, colors.white, colors.white)
- if stat then
- if not fs.exists(localFile) then
- fs.makeDir(localFile)
- end
- for i,v in pairs(files) do
- local baseDir = fs.combine(localFile, string.sub(i, 1, #i-#fs.getName(i)))
- if not fs.exists(baseDir) then
- fs.makeDir(baseDir)
- end
- local f = fs.open(fs.combine(localFile, i), "w")
- if f then
- f.write(v)
- f.close()
- else
- print("Could not write file: "..i)
- end
- end
- else
- error("could not access repository")
- end
- elseif mode == "get_file" then
- local stat, data = github_getFile(user, remoteRepo, remoteFile, remoteBranch, false, colors.white, color.white)
- if stat then
- local f = fs.open(localFile, "w")
- if f then
- f.write(data)
- f.close()
- else
- error("could not access repository")
- end
- end
- end
Add Comment
Please, Sign In to add comment