Guest User

djk

a guest
Sep 26th, 2016
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.06 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. local tArgs = { ... }
  7. if #tArgs < 2 then
  8. printUsage()
  9. return
  10. end
  11. local sCommand = tArgs[1]
  12. if sCommand == "put" then
  13. -- Upload a file to pastebin.com
  14. -- Determine file to upload
  15. local sFile = tArgs[2]
  16. local sPath = shell.resolve( sFile )
  17. if not fs.exists( sPath ) or fs.isDir( sPath ) then
  18. print( "No such file" )
  19. return
  20. end
  21. write("Enter paste name: ")
  22. sName = read()
  23.  
  24. -- Read in the file
  25. if sName == "" then local sName = fs.getName( sPath ) end
  26. local file = fs.open( sPath, "r" )
  27. local sText = file.readAll()
  28. file.close()
  29. -- Log in
  30. print("Log in? y/n")
  31. while true do
  32. login = read()
  33. if login == "y" or login == "n" then break end
  34. end
  35. if login == "y" then
  36. write("Username: ")
  37. username = textutils.urlEncode(read())
  38. write("Password: ")
  39. password = textutils.urlEncode(read("*"))
  40. end
  41. write( "Connecting to pastebin.com... " )
  42. local key = "0ec2eb25b6166c0c27a394ae118ad829"
  43. if login == "y" then
  44. local userraw = http.post(
  45. "http://pastebin.com/api/api_login.php",
  46. "api_dev_key="..key.."&"..
  47. "api_user_name="..username.."&"..
  48. "api_user_password="..password
  49. )
  50. -- get user code in annoying roundabout way
  51. -- error/bug/?: userraw[1]() doesn't work
  52. for i,v in pairs(userraw) do
  53. user = v()
  54. break
  55. end
  56. -- check for errors with pastebin
  57. if string.sub(user, 1, 3) == "Bad" then print("\n"..user) error("") end
  58. local response = http.post(
  59. "http://pastebin.com/api/api_post.php",
  60. "api_option=paste&"..
  61. "api_dev_key="..key.."&"..
  62. "api_user_key="..user.."&"..
  63. "api_paste_format=lua&"..
  64. "api_paste_name="..textutils.urlEncode(sName).."&"..
  65. "api_paste_code="..textutils.urlEncode(sText)
  66. )
  67.  
  68. print("Done")
  69. print("Check your pastes for find your file")
  70. else
  71. local response = http.post(
  72. "http://pastebin.com/api/api_post.php",
  73. "api_option=paste&"..
  74. "api_dev_key="..key.."&"..
  75. "api_paste_format=lua&"..
  76. "api_paste_name="..textutils.urlEncode(sName).."&"..
  77. "api_paste_code="..textutils.urlEncode(sText)
  78. )
  79. if response then
  80. print( "Success." )
  81.  
  82. local sResponse = response.readAll()
  83. response.close()
  84.  
  85. local sCode = string.match( sResponse, "[^/]+$" )
  86. print( "Uploaded as "..sResponse )
  87. print( "Run \"pastebin get "..sCode.."\" to download anywhere" )
  88. else
  89. print( "Failed." )
  90. end
  91. end
  92.  
  93. elseif sCommand == "get" then
  94. -- Download a file from pastebin.com
  95. if #tArgs < 3 then
  96. printUsage()
  97. return
  98. end
  99. -- Determine file to download
  100. local sCode = tArgs[2]
  101. local sFile = tArgs[3]
  102. local sPath = shell.resolve( sFile )
  103. if fs.exists( sPath ) then
  104. print( "File already exists" )
  105. return
  106. end
  107.  
  108. -- GET the contents from pastebin
  109. write( "Connecting to pastebin.com... " )
  110. local response = http.get(
  111. "http://pastebin.com/raw.php?i="..textutils.urlEncode( sCode )
  112. )
  113.  
  114. if response then
  115. print( "Success." )
  116.  
  117. local sResponse = response.readAll()
  118. response.close()
  119.  
  120. local file = fs.open( sPath, "w" )
  121. file.write( sResponse )
  122. file.close()
  123.  
  124. print( "Downloaded as "..sFile )
  125.  
  126. else
  127. print( "Failed." )
  128. end
  129. else
  130. printUsage()
  131. return
  132. end
Add Comment
Please, Sign In to add comment