Zekrommaster110

[LUA | CC] getany 2.0.0

Apr 7th, 2020 (edited)
555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.00 KB | None | 0 0
  1. local VERSION = "2.0.0"
  2.  
  3. local silentMode = false
  4. local yesMode = false
  5.  
  6. -------------------------------------------
  7.  
  8. local function printHelp()
  9.     local pname = shell.getRunningProgram()
  10.     print("Usages:")
  11.     print(pname .. " <URL> <Output> [-y] [-s] [-h] [-v]")
  12. end
  13.  
  14. local function printVersion()
  15.     print("getany v." .. VERSION)
  16.     print("(c) 2020 zekro Development")
  17. end
  18.  
  19. local function contains(tab, val)
  20.     for _, v in pairs(tab) do
  21.         if v == val then return true end
  22.     end
  23.     return false
  24. end
  25.  
  26. local function out(str)
  27.     if not silentMode then
  28.         print(str)
  29.     end
  30. end
  31.  
  32. local function startsWith(str, val)
  33.     return str:sub(1, #val) == val
  34. end
  35.  
  36. local function getRawArgs(args)
  37.     local out = {}
  38.     for _, v in pairs(args) do
  39.         if not startsWith(v, "-") then
  40.             table.insert(out, v)
  41.         end
  42.     end
  43.     return out
  44. end
  45.  
  46. local function getBytesStr(bytes)
  47.     if bytes < 1024 then
  48.         return bytes .. " B"
  49.     elseif bytes < 1024 * 1024 then
  50.         return bytes / 1024 .. " kiB"
  51.     else
  52.         return bytes / 1024 / 1024 .. " miB"
  53.     end
  54. end
  55.  
  56. local function getDownloadURI(inpt)
  57.     if not startsWith(inpt, "http") then
  58.         inpt = "https://" .. inpt
  59.     end
  60.  
  61.     -- pastebin
  62.     if startsWith(inpt, "https://pastebin.com/") then
  63.         code = inpt:sub(#"https://pastebin.com/" + 1)
  64.         return "https://pastebin.com/raw/" .. code
  65.     end
  66.  
  67.     -- github
  68.     if startsWith(inpt, "https://github.com/") then
  69.         res, _ = inpt:gsub("/blob/", "/raw/")
  70.         return res
  71.     end
  72.  
  73.     return inpt
  74. end
  75.  
  76. local function get(uri)
  77.     res = http.get(uri)
  78.     if not res then
  79.         return false, nil
  80.     end
  81.     return true, res.readAll()
  82. end
  83.  
  84. -------------------------------------------
  85.  
  86. local args = {...}
  87. local rawArgs = getRawArgs(args)
  88.  
  89. silentMode = contains(args, "-s")
  90. yesMode = contains(args, "-y")
  91.  
  92. if contains(args, "-v") then
  93.     printVersion()
  94.     return
  95. end
  96.  
  97. if #rawArgs < 2 or contains(args, "-h") then
  98.     printHelp()
  99.     return
  100. end
  101.  
  102. local uri = getDownloadURI(rawArgs[1])
  103. out("Downloading from:\n" .. uri)
  104.  
  105. local ok, res = get(uri)
  106. if not ok then
  107.     out("Download failed.")
  108.     return
  109. end
  110.  
  111. out("Downloaded " .. getBytesStr(#res))
  112.  
  113. local output = rawArgs[2]
  114.  
  115. if fs.exists(output) then
  116.     local cont = fs.open(output, "r").readAll()
  117.     if cont == res then
  118.         out("Received content is same as file content.")
  119.         out("Abort.")
  120.         return
  121.     end
  122.  
  123.     if silentMode and not yesMode then
  124.         return
  125.     end
  126.  
  127.     if not yesMode then
  128.         write(
  129.             "Do you want to overwrite the existign file '" ..
  130.             output .. "'? [yN] "
  131.         )
  132.         yn = read()
  133.         if yn ~= "y" and yn ~= "Y" then
  134.             print("Abort.")
  135.             return
  136.         end
  137.     end
  138.  
  139.     out("Overwriting existing file: " .. output)
  140. end
  141.  
  142. out("Writing file output...")
  143. local fh = fs.open(output, "w")
  144. fh.write(res)
  145. fh.close()
  146.  
  147. out("Finished.")
Add Comment
Please, Sign In to add comment