Advertisement
krzys_h

pastebin (fixed)

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