Advertisement
Guest User

pastebin.lua

a guest
Jun 24th, 2018
1,186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.24 KB | None | 0 0
  1.  
  2. local function printUsage()
  3.     print( "Usages:" )
  4.     print( "pastebin put <filename>" )
  5.     print( "pastebin get <code> <filename>" )
  6.     print( "pastebin 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( "Pastebin requires http API" )
  17.     printError( "Set http_enable to true in ComputerCraft.cfg" )
  18.     return
  19. end
  20.  
  21. local function get(paste)
  22.     write( "Connecting to pastebin.com... " )
  23.     local response = http.get(
  24.         "https://pastebin.com/raw/"..textutils.urlEncode( paste )
  25.     )
  26.        
  27.     if response then
  28.         print( "Success." )
  29.        
  30.         local sResponse = response.readAll()
  31.         response.close()
  32.         return sResponse
  33.     else
  34.         print( "Failed." )
  35.     end
  36. end
  37.  
  38. local sCommand = tArgs[1]
  39. if sCommand == "put" then
  40.     -- Upload a file to pastebin.com
  41.     -- Determine file to upload
  42.     local sFile = tArgs[2]
  43.     local sPath = syscall.getcwd() .. "/" .. sFile
  44.     if not fs.exists( sPath ) or fs.isDir( sPath ) then
  45.         print(sPath)
  46.         print(fs.exists(sPath))
  47.         print(fs.isDir(sPath))
  48.         print( "No such file" )
  49.         return
  50.     end
  51.    
  52.     -- Read in the file
  53.     local sName = fs.getName( sPath )
  54.     local file = fs.open( sPath, "r" )
  55.     local sText = file.readAll()
  56.     file.close()
  57.    
  58.     -- POST the contents to pastebin
  59.     write( "Connecting to pastebin.com... " )
  60.     local key = "0ec2eb25b6166c0c27a394ae118ad829"
  61.     local response = http.post(
  62.         "https://pastebin.com/api/api_post.php",
  63.         "api_option=paste&"..
  64.         "api_dev_key="..key.."&"..
  65.         "api_paste_format=lua&"..
  66.         "api_paste_name="..textutils.urlEncode(sName).."&"..
  67.         "api_paste_code="..textutils.urlEncode(sText)
  68.     )
  69.        
  70.     if response then
  71.         print( "Success." )
  72.        
  73.         local sResponse = response.readAll()
  74.         response.close()
  75.                
  76.         local sCode = string.match( sResponse, "[^/]+$" )
  77.         print( "Uploaded as "..sResponse )
  78.         print( "Run \"pastebin get "..sCode.."\" to download anywhere" )
  79.  
  80.     else
  81.         print( "Failed." )
  82.     end
  83.    
  84. elseif sCommand == "get" then
  85.     -- Download a file from pastebin.com
  86.     if #tArgs < 3 then
  87.         printUsage()
  88.         return
  89.     end
  90.  
  91.     -- Determine file to download
  92.     local sCode = tArgs[2]
  93.     local sFile = tArgs[3]
  94.     local sPath = shell.resolve( sFile )
  95.     if fs.exists( sPath ) then
  96.         print( "File already exists" )
  97.         return
  98.     end
  99.    
  100.     -- GET the contents from pastebin
  101.     local res = get(sCode)
  102.     if res then        
  103.         local file = fs.open( sPath, "w" )
  104.         file.write( res )
  105.         file.close()
  106.        
  107.         print( "Downloaded as "..sFile )
  108.     end
  109. elseif sCommand == "run" then
  110.     local sCode = tArgs[2]
  111.  
  112.     local res = get(sCode)
  113.     if res then
  114.         local func, err = load(res, sCode, "t", _ENV)
  115.         if not func then
  116.             printError( err )
  117.             return
  118.         end
  119.         local success, msg = pcall(func, table.unpack(tArgs, 3))
  120.         if not success then
  121.             printError( msg )
  122.         end
  123.     end
  124. else
  125.     printUsage()
  126.     return
  127. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement