Advertisement
PeachGaming

Untitled

Jul 30th, 2020 (edited)
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. --Spaceb.in for OpenComputers.
  2. --Allows you to get things from Spacebin..in OpenComputers!
  3. --Why did I do this.
  4. --Author PeachMaster, 2020
  5. local component = require("component")
  6. local fs = require("filesystem")
  7. local interwebz = require("internet")
  8. local filesystem = require("filesystem")
  9. local shell = require("shell")
  10. local serialization = require("serialization")
  11.  
  12. if not component.isAvailable("internet") then
  13. print("This program requires an internet card to run.")
  14. return
  15. end
  16.  
  17. if not filesystem.exists("/usr/lib/json.lua") then
  18. shell.execute("mkdir /usr/lib")
  19. shell.execute("pastebin get E32pxWMu /usr/lib/json.lua")
  20. end
  21.  
  22. local json = require("json")
  23.  
  24. local args, options = shell.parse(...)
  25.  
  26. local function get(theID, filename)
  27. local f, reason = io.open(filename, "w")
  28. io.write("Downloading from spaceb.in...")
  29. local url = "https://api.spaceb.in/api/v1/document/"..theID.."/raw"
  30. local handle = interwebz.request(url, {}, {["User-Agent"] = "OpenComputers", ["Accept"] = "*/*"}, "GET")
  31. local result = ""
  32. for chunk in handle do
  33. result = result..chunk
  34.  
  35. end
  36. io.write("done! \n")
  37. local io = require("io")
  38. f:write(result)
  39. f:close()
  40. print("Completed successfully!")
  41. end
  42.  
  43. local function put(filename)
  44. local io = require("io")
  45. local file, reason = io.open(filename, "r")
  46.  
  47. if not file then
  48. io.stderr:write("Failed opening file for reading: " .. reason)
  49. return
  50. end
  51. local data = file:read("*a")
  52. file:close()
  53. local url = "https://api.spaceb.in/api/v1/document/"
  54. local handle = interwebz.request(url, json.encode({content = data}), {["Content-Type"] = "application/json", ["User-Agent"] = "OpenComputers", ["Accept"] = "*/*"}, "POST")
  55. local result = ""
  56. for chunk in handle do
  57. result = result..chunk
  58. end
  59.  
  60. local jsonResult = json.decode(result)
  61. print("Completed successfully!")
  62. print("Use the ID "..jsonResult["payload"]["id"].." to use your paste anywhere!")
  63. end
  64.  
  65. local function run(theID, ...)
  66. local tmpFile = os.tmpname()
  67. get(theID, tmpFile)
  68. print("Running "..theID.."...")
  69. local success, reason = shell.execute(tmpFile, nil, ...)
  70. if not success then
  71. io.stderr:write(reason)
  72. end
  73. fs.remove(tmpFile)
  74. end
  75.  
  76. local command = args[1]
  77.  
  78. if command == "help" or command == nil then
  79. print("Downloads a file from Spaceb.in, text sharing for the final frontier. ")
  80. print("")
  81. print("spacebin get <id> <filename> to get a file and save it.")
  82. print("spacebin put <filename> to put a file up on the site.")
  83. print("spacebin run <filename> to directly run a program, without saving it.")
  84. end
  85.  
  86. if command == "get" then
  87. if (args[2]==nil) then
  88. print("You need to put an id to get from.")
  89. print("spacebin get <id> <filename>")
  90. return
  91. end
  92. if (args[3]==nil) then
  93. print("You need a file to put it to.")
  94. print("spacebin get <id> <filename>")
  95. return
  96. end
  97. get(args[2], args[3])
  98. end
  99.  
  100.  
  101. if command == "put" then
  102. put(args[2])
  103. end
  104.  
  105. if command == "run" then
  106. run(args[2], table.unpack(args, 3))
  107. end
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement