Advertisement
Sim_Piko

[lua]betterpastebin

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