Advertisement
MagmaLP

Untitled

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