Advertisement
Wyvern67

fonction pastebin

Jan 19th, 2013
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.51 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 = "4d29bff115c86375847f6b671cbdda8d"
  39.     local userKey = http.post(
  40.         "http://pastebin.com/api/api_login.php",
  41.         "api_dev_key="..key.."&"..
  42.         "api_user_name="..textutils.urlEncode("Wyvern67").."&"..
  43.         "api_user_password="..textutils.urlEncode("Gamecube")
  44.         )
  45.     local response = http.post(
  46.         "http://pastebin.com/api/api_post.php",
  47.         "api_option=paste&"..
  48.         "api_dev_key="..key.."&"..
  49.         "api_paste_format=lua&"..
  50.         "api_paste_name="..textutils.urlEncode(sName).."&"..
  51.         "api_user_key="..userKey.."&"..
  52.         "api_paste_code="..textutils.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="..textutils.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