Advertisement
JJC15433

fileListDownloadTest

Dec 6th, 2020 (edited)
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. -- for k,v in pairs(paths) do
  2. -- local splitpath ={}
  3. -- join(v:split('/',nil,true),splitpath)
  4. -- for i=1,#splitpath,1 do
  5. -- if i == 1 then path = "/repos/test" else path = path.."/"..splitpath[i] end
  6. -- if not fs.exists(path) then
  7. -- if i == #splitpath then
  8. -- -- code for pulling down a file from github goes here
  9. -- -- file should be made at "path" from github path
  10. -- else
  11. -- fs.mkDir(path)
  12. -- end
  13. -- end
  14. -- end
  15.  
  16. -- none of the above is needed at all. ccTweaked's CraftOS is capable of creating nested dirs/files where they didn't previously exist (no need to iterate over parts of a path)
  17.  
  18. function join(src,dest)
  19. if src == dest then
  20. local temp = {}
  21. join(src,temp)
  22. join(temp,dest)
  23. else
  24. for k,v in pairs(src) do
  25. -- print("src entry:",k,v)
  26. -- if #dest > 0 then print("dest@len:", #dest,dest[#dest]) end
  27. dest[#dest+1] = v
  28. -- print("dest@new len:", #dest,dest[#dest])
  29. -- os.sleep(5)
  30. end
  31. end
  32. end
  33.  
  34. targs = {...}
  35. paths={}
  36.  
  37. assert(targs[1]~=nil and fs.exists(targs[1]))
  38. input=fs.open(targs[1],"r")
  39. line=input.readLine()
  40. while line ~= nil do
  41. join({line},paths)
  42. line=input.readLine()
  43. end
  44. input.close()
  45.  
  46.  
  47. tbPath="repos/Kammon/ccTweaked" -- TODO: make this user-provided for on-board download function outside of startup
  48. rbPath="https://raw.githubusercontent.com/Kammon/ccTweaked/main" -- TODO: also make this user-provided outside of startup
  49. -- build paths
  50. for k,v in pairs(paths) do
  51. if string.find(v,"/",1,true)~=1 then paths[k]="/"..paths[k] end -- prepend w/ '/' if not present
  52. paths[k] = {base=paths[k],repo=paths[k],turtle=tbPath..paths[k]} -- build various paths
  53. -- clean up turtle paths to avoid trailing whitespace errors
  54. paths[k].turtle = string.gsub(paths[k].turtle,"%s*$","") -- remove trailing whitespace, if any
  55. paths[k].turtle = string.gsub(paths[k].turtle,"%s","_") -- replace whitespace with underscores
  56. -- url encoding characters for git repo
  57. paths[k].repo = string.gsub(paths[k].repo,"%%","%%25") -- replace all '%' first, as it's used in encoding
  58. paths[k].repo = string.gsub(paths[k].repo," ","%%20")
  59. paths[k].repo = string.gsub(paths[k].repo,"!","%%21")
  60. paths[k].repo = string.gsub(paths[k].repo,'"','%%22')
  61. paths[k].repo = string.gsub(paths[k].repo,"#","%%23")
  62. paths[k].repo = string.gsub(paths[k].repo,"%$","%%24")
  63. paths[k].repo = string.gsub(paths[k].repo,"&","%%26")
  64. paths[k].repo = string.gsub(paths[k].repo,"'","%%27")
  65. paths[k].repo = string.gsub(paths[k].repo,"%(","%%28")
  66. paths[k].repo = string.gsub(paths[k].repo,"%)","%%29")
  67. paths[k].repo = string.gsub(paths[k].repo,"%*","%%2A")
  68. paths[k].repo = string.gsub(paths[k].repo,"%+","%%2B")
  69. paths[k].repo = string.gsub(paths[k].repo,",","%%2C")
  70. paths[k].repo = string.gsub(paths[k].repo,"%-","%%2D")
  71. paths[k].repo = string.gsub(paths[k].repo,"%.","%%2E")
  72. paths[k].repo = string.gsub(paths[k].repo,":","%%3A")
  73. paths[k].repo = string.gsub(paths[k].repo,";","%%3B")
  74. paths[k].repo = string.gsub(paths[k].repo,"<","%%3C")
  75. paths[k].repo = string.gsub(paths[k].repo,"=","%%3D")
  76. paths[k].repo = string.gsub(paths[k].repo,">","%%3E")
  77. paths[k].repo = string.gsub(paths[k].repo,"%?","%%3F")
  78. paths[k].repo = string.gsub(paths[k].repo,"@","%%40")
  79. paths[k].repo = string.gsub(paths[k].repo,"%[","%%5B")
  80. paths[k].repo = string.gsub(paths[k].repo,"\\","%%5C")
  81. paths[k].repo = string.gsub(paths[k].repo,"%]","%%5D")
  82. paths[k].repo = string.gsub(paths[k].repo,"%^","%%5E")
  83. paths[k].repo = string.gsub(paths[k].repo,"_","%%5F")
  84. paths[k].repo = string.gsub(paths[k].repo,"`","%%60")
  85. paths[k].repo = string.gsub(paths[k].repo,"{","%%7B")
  86. paths[k].repo = string.gsub(paths[k].repo,"|","%%7C")
  87. paths[k].repo = string.gsub(paths[k].repo,"}","%%7D")
  88. paths[k].repo = string.gsub(paths[k].repo,"~","%%7E")
  89. paths[k].repo = rbPath..paths[k].repo -- prepend repo url with base repository url
  90. end
  91.  
  92. -- start downloading files
  93. for k,v in pairs(paths) do
  94. local resp = http.get(paths[k].repo)
  95. if resp.getResponseCode() == 200 and tonumber(resp.getResponseHeaders()["content-length"]) > 0 then
  96. if fs.exists(paths[k].turtle) then print("File Exists on Turtle. Overwriting...") end
  97. local file = fs.open(paths[k].turtle,"w")
  98. local line = resp.readLine(true)
  99. while line ~= nil do
  100. file.write(line)
  101. line=resp.readLine(true)
  102. end
  103. file.close()
  104. end
  105. resp.close()
  106. end
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement