Tatantyler

Github Downloader

Oct 1st, 2013
445
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.03 KB | None | 0 0
  1. function github_getRequestsLeft()
  2. local h = http.get("https://api.github.com/rate_limit")
  3. if h then
  4. local data = h.readAll()
  5. h.close()
  6. local s = string.match(data, "\"remaining\":(%d+)")
  7. if s then
  8. return tonumber(s) or 0
  9. end
  10. else
  11. return 0
  12. end
  13. end
  14.  
  15. function github_getRequestLimit()
  16. local h = http.get("https://api.github.com/rate_limit")
  17. if h then
  18. local data = h.readAll()
  19. h.close()
  20. local s = string.match(data, "\"limit\":(%d+)")
  21. if s then
  22. return tonumber(s) or 0
  23. end
  24. else
  25. return 0
  26. end
  27. end
  28.  
  29. function parse_contentsRequest(response)
  30. local tables = {}
  31. local id = 0
  32. for ident, data in string.gmatch(response, "\"(%a+)\":\"([^%\"]+)\",") do
  33. --local line = string.match(lines[i], "%s+(%.+)")
  34. --if line ~= "{" and line ~= "}" and line ~= "}," then
  35. -- local ident, data = string.match(line, "\"(%a+)\": \"([^%\"]+)\"")
  36. --print("["..ident.."] = "..data)
  37. if ident == "name" then
  38. id = id + 1
  39. tables[id] = {}
  40. tables[id]["name"] = data
  41. tables[id]["links"] = {}
  42. elseif ident == "self" or ident == "git" or ident == "html" then
  43. tables[id]["links"][ident] = data
  44. elseif ident ~= "_links" then
  45. tables[id][ident] = data
  46. end
  47. end
  48. return tables
  49. end
  50.  
  51. function github_getContents(user, repo, path, verbose, fileNameColor, normalTextColor)
  52. if verbose then
  53. fileNameColor = fileNameColor or colors.orange
  54. normalTextColor = normalTextColor or colors.white
  55. write("Listing: ")
  56. term.setTextColor(fileNameColor)
  57. write(path)
  58. term.setTextColor(normalTextColor)
  59. write("...")
  60. end
  61. path = path or ""
  62. local url = "https://api.github.com/repos/"..user.."/"..repo.."/contents/"..path
  63. --print("HTTP GET: "..url)
  64. if github_getRequestsLeft() == 0 then
  65. return false, {}
  66. end
  67. local handle = http.get(url)
  68. if handle then
  69. local data = handle.readAll()
  70. --print("Retrieved "..#data.." bytes.")
  71. handle.close()
  72. --print(data)
  73. if verbose then
  74. print("Success.")
  75. end
  76. return true, parse_contentsRequest(data)
  77. else
  78. if verbose then
  79. print("Failed.")
  80. end
  81. return false, {}
  82. end
  83. end
  84.  
  85. function github_getFile(user, repo, file, branch, verbose, fileNameColor, normalTextColor)
  86. if verbose then
  87. fileNameColor = fileNameColor or colors.orange
  88. normalTextColor = normalTextColor or colors.white
  89. write("Retrieving: ")
  90. term.setTextColor(fileNameColor)
  91. write(file)
  92. term.setTextColor(normalTextColor)
  93. write("...")
  94. end
  95. local url = "https://raw.github.com/"..user.."/"..repo.."/"..branch.."/"..file
  96. --print("HTTP GET: "..url)
  97. local handle = http.get(url)
  98. if handle then
  99. local data = handle.readAll()
  100. handle.close()
  101. if verbose then
  102. print("Success.")
  103. print("Retrieved "..#data.." bytes.")
  104. end
  105. return true, data
  106. else
  107. if verbose then
  108. print("Failed.")
  109. end
  110. return false
  111. end
  112. end
  113.  
  114. function github_getAllFilesRecursive(user, repo, dir, branch, verbose, fileNameColor, normalTextColor)
  115. dir = dir or ""
  116. local files = {}
  117. local status, contents = github_getContents(user, repo, dir, verbose, fileNameColor, normalTextColor)
  118. if not status then
  119. return false
  120. end
  121. local totalFiles = 0
  122. local totalDirs = 0
  123. local failedFiles = 0
  124. local failedDirs = 0
  125. for i,v in ipairs(contents) do
  126. if v["type"] == "file" then
  127. totalFiles = totalFiles+1
  128. local status, data = github_getFile(user, repo, v["path"], branch, verbose, fileNameColor, normalTextColor)
  129. if status then
  130. files[v["path"]] = data
  131. else
  132. failedFiles = failedFiles+1
  133. end
  134. elseif v["type"] == "dir" then
  135. totalDirs = totalDirs+1
  136. local status, files2, fDirs, fFiles, tDirs, tFiles = github_getAllFilesRecursive(user, repo, v["path"], branch, verbose, fileNameColor, normalTextColor)
  137. if status then
  138. for i,v in pairs(files2) do
  139. files[i] = v
  140. end
  141. failedDirs = failedDirs+fDirs
  142. failedFiles = failedFiles+fFiles
  143. totalFiles = totalFiles + tFiles
  144. totalDirs = totalDirs + tDirs
  145. else
  146. failedDirs = failedDirs+1
  147. end
  148. end
  149. end
  150. return true, files, failedDirs, failedFiles, totalDirs, totalFiles
  151. end
  152.  
  153. function pastebin_get(code)
  154. local wHandle = http.get("http://www.pastebin.com/raw.php?i="..code)
  155. if wHandle then
  156. local wData = wHandle.readAll()
  157. wHandle.close()
  158. return true, wData
  159. else
  160. return false
  161. end
  162. end
  163.  
  164. local args = {...}
  165.  
  166. local mode = args[1]
  167. local user = args[2]
  168. local localFile = args[3]
  169. local remoteFile = args[4]
  170. local remoteBranch = args[5]
  171. local remoteRepo = args[6]
  172.  
  173. if mode == "get_dir" then
  174. local stat, files = github_getAllFilesRecursive(user, remoteRepo, remoteFile, remoteBranch, false, colors.white, colors.white)
  175. if stat then
  176. if not fs.exists(localFile) then
  177. fs.makeDir(localFile)
  178. end
  179. for i,v in pairs(files) do
  180. local baseDir = fs.combine(localFile, string.sub(i, 1, #i-#fs.getName(i)))
  181. if not fs.exists(baseDir) then
  182. fs.makeDir(baseDir)
  183. end
  184. local f = fs.open(fs.combine(localFile, i), "w")
  185. if f then
  186. f.write(v)
  187. f.close()
  188. else
  189. print("Could not write file: "..i)
  190. end
  191. end
  192. else
  193. error("could not access repository")
  194. end
  195. elseif mode == "get_file" then
  196. local stat, data = github_getFile(user, remoteRepo, remoteFile, remoteBranch, false, colors.white, color.white)
  197. if stat then
  198. local f = fs.open(localFile, "w")
  199. if f then
  200. f.write(data)
  201. f.close()
  202. else
  203. error("could not access repository")
  204. end
  205. end
  206. end
Add Comment
Please, Sign In to add comment