Advertisement
Guest User

pastebin

a guest
May 20th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.69 KB | None | 0 0
  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.         return
  77.     end
  78.    
  79.     -- GET the contents from pastebin
  80.     write( "Connecting to pastebin.com... " )
  81.     http.request(
  82.         "https://pastebin.com/raw.php?i="..textutils.urlEncode( sCode )
  83.         )
  84.        
  85.     event, url, response = os.pullEvent()
  86.     if (event == "http_success") then
  87.         print( "Success." )
  88.        
  89.         local sResponse = response.readAll()
  90.         response.close()
  91.        
  92.         local file = fs.open( sPath, "w" )
  93.         file.write( sResponse )
  94.         file.close()
  95.        
  96.         print( "Downloaded as "..sFile )
  97.        
  98.     else
  99.         print( "Failed." )
  100.     end
  101.  
  102. else
  103.     printUsage()
  104.     return
  105. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement