Advertisement
Guest User

spacebin.lua

a guest
Dec 28th, 2020
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.04 KB | None | 0 0
  1. local function printUsage()
  2.     local programName = arg[0] or fs.getName(shell.getRunningProgram())
  3.     print("Usages:")
  4.     print(programName .. " put <filename>")
  5.     print(programName .. " get <code> <filename>")
  6.     print(programName .. " run <code> <arguments>")
  7. end
  8.  
  9. local tArgs = { ... }
  10. if #tArgs < 2 then
  11.     printUsage()
  12.     return
  13. end
  14.  
  15. if not http then
  16.     printError("Spacebin requires the http API")
  17.     printError("Set http.enabled to true in CC's config")
  18.     return
  19. end
  20.  
  21. --- Attempts to guess the spacebin ID from the given code or URL
  22. local function extractId(paste)
  23.     local patterns = {
  24.         "^([%a%d]+)$",
  25.         "^https?://spaceb.in/([%a%d]+)$",
  26.         "^spaceb.in/([%a%d]+)$",
  27.         "^https?://spaceb.in/([%a%d]+)/raw$",
  28.     }
  29.  
  30.     for i = 1, #patterns do
  31.         local code = paste:match(patterns[i])
  32.         if code then return code end
  33.     end
  34.  
  35.     return nil
  36. end
  37.  
  38. local function get(url)
  39.     local paste = extractId(url)
  40.     if not paste then
  41.         io.stderr:write("Invalid spacebin code.\n")
  42.         io.write("The code is the ID at the end of the spaceb.in URL.\n")
  43.         return
  44.     end
  45.  
  46.     write("Connecting to spaceb.in... ")
  47.     local response, err = http.get(
  48.         "https://spaceb.in/api/v1/documents/"..textutils.urlEncode(paste).."/raw"
  49.     )
  50.  
  51.     if response then
  52.         local headers = response.getResponseHeaders()
  53.         if not headers["Content-Type"] or not headers["Content-Type"]:find("^text/plain") then
  54.             io.stderr:write("Failed.\n")
  55.             print("Failed to download paste.")
  56.             return
  57.         end
  58.  
  59.         print("Success.")
  60.  
  61.         local sResponse = response.readAll()
  62.         response.close()
  63.         return sResponse
  64.     else
  65.         io.stderr:write("Failed.\n")
  66.         print(err)
  67.     end
  68. end
  69.  
  70. local sCommand = tArgs[1]
  71. if sCommand == "put" then
  72.     -- Upload a file to spaceb.in
  73.     -- Determine file to upload
  74.     local sFile = tArgs[2]
  75.     local sPath = shell.resolve(sFile)
  76.     if not fs.exists(sPath) or fs.isDir(sPath) then
  77.         print("No such file")
  78.         return
  79.     end
  80.  
  81.     -- Read in the file
  82.     local sName = fs.getName(sPath)
  83.     local file = fs.open(sPath, "r")
  84.     local sText = file.readAll()
  85.     file.close()
  86.  
  87.     -- POST the contents to spacebin
  88.     write("Connecting to spaceb.in... ")
  89.     local response, err = http.post(
  90.         "https://spaceb.in/api/v1/documents",
  91.         textutils.serialiseJSON(
  92.             {
  93.                 ["content"] = sText;
  94.                 ["extension"] = "txt";
  95.             }
  96.         ),
  97.         {
  98.             ["Content-Type"] = "application/json";
  99.         }
  100.     )
  101.    
  102.     if response then
  103.         print("Success.")
  104.  
  105.         local sResponse = response.readAll()
  106.         response.close()
  107.        
  108.         local sCode = string.match(sResponse, "\"id\":[\s]*\"([a-zA-Z]+)\"")
  109.         print("Uploaded as " .. sCode)
  110.         print("Run \"spacebin get " .. sCode .. "\" to download anywhere")
  111.  
  112.     else
  113.         print("Failed.")
  114.         error(err)
  115.     end
  116.  
  117. elseif sCommand == "get" then
  118.     -- Download a file from spaceb.in
  119.     if #tArgs < 3 then
  120.         printUsage()
  121.         return
  122.     end
  123.  
  124.     -- Determine file to download
  125.     local sCode = tArgs[2]
  126.     local sFile = tArgs[3]
  127.     local sPath = shell.resolve(sFile)
  128.     if fs.exists(sPath) then
  129.         print("File already exists")
  130.         return
  131.     end
  132.  
  133.     -- GET the contents from spacebin
  134.     local res = get(sCode)
  135.     if res then
  136.         local file = fs.open(sPath, "w")
  137.         file.write(res)
  138.         file.close()
  139.  
  140.         print("Downloaded as " .. sFile)
  141.     end
  142. elseif sCommand == "run" then
  143.     local sCode = tArgs[2]
  144.  
  145.     local res = get(sCode)
  146.     if res then
  147.         local func, err = load(res, sCode, "t", _ENV)
  148.         if not func then
  149.             printError(err)
  150.             return
  151.         end
  152.         local success, msg = pcall(func, select(3, ...))
  153.         if not success then
  154.             printError(msg)
  155.         end
  156.     end
  157. else
  158.     printUsage()
  159.     return
  160. end
  161.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement