JoeNoIce

,

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