Advertisement
Fyggh

pastebin.lua

Nov 19th, 2017
1,186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.12 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. local sCommand = tArgs[1]
  14. if sCommand == "put" then
  15.     -- Upload a file to pastebin.com
  16.     -- Determine file to upload
  17.     local sFile = tArgs[2]
  18.     local sPath = shell.resolve( sFile )
  19.     if not fs.exists( sPath ) or fs.isDir( sPath ) then
  20.         print( "No such file" )
  21.         return
  22.     end
  23.    
  24.     -- Read in the file
  25.     local sName = fs.getName( sPath )
  26.     local file = fs.open( sPath, "r" )
  27.     local sText = file.readAll()
  28.     file.close()
  29.    
  30.     -- POST the contents to pastebin
  31.     write( "Connecting to pastebin.com... " )
  32.     local key = "0ec2eb25b6166c0c27a394ae118ad829"
  33.     local response = http.post(
  34.         "http://pastebin.com/api/api_post.php",
  35.         "api_option=paste&"..
  36.         "api_dev_key="..key.."&"..
  37.         "api_paste_format=lua&"..
  38.         "api_paste_name="..textutils.urlEncode(sName).."&"..
  39.         "api_paste_code="..textutils.urlEncode(sText)
  40.         )
  41.        
  42.     if response then
  43.         print( "Success." )
  44.        
  45.         local sResponse = response.readAll()
  46.         response.close()
  47.                
  48.         local sCode = string.match( sResponse, "[^/]+$" )
  49.         print( "Uploaded as "..sResponse )
  50.         print( "Run \"pastebin get "..sCode.."\" to download anywhere" )
  51.  
  52.     else
  53.         print( "Failed." )
  54.     end
  55.    
  56. elseif sCommand == "get" then
  57.     -- Download a file from pastebin.com
  58.     if #tArgs < 3 then
  59.         printUsage()
  60.         return
  61.     end
  62.  
  63.     -- Determine file to download
  64.     local sCode = tArgs[2]
  65.     local sFile = tArgs[3]
  66.     local sPath = shell.resolve( sFile )
  67.     if fs.exists( sPath ) then
  68.         print( "File already exists" )
  69.         return
  70.     end
  71.    
  72.     -- GET the contents from pastebin
  73.     write( "Connecting to pastebin.com... " )
  74.     local response = http.get(
  75.         "http://pastebin.com/raw.php?i="..textutils.urlEncode( sCode )
  76.         )
  77.        
  78.     if response then
  79.         print( "Success." )
  80.        
  81.         local sResponse = response.readAll()
  82.         response.close()
  83.        
  84.         local file = fs.open( sPath, "w" )
  85.         file.write( sResponse )
  86.         file.close()
  87.        
  88.         print( "Downloaded as "..sFile )
  89.        
  90.     else
  91.         print( "Failed." )
  92.     end
  93.  
  94. else
  95.     printUsage()
  96.     return
  97. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement