Advertisement
TheOddByte

[ComputerCraft][API] Pastebin

May 2nd, 2014
771
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.57 KB | None | 0 0
  1. --[[
  2.     [API] Pastebin
  3.     @version 1.1, 2014-08-13, TOB
  4.     @author TheOddByte, TOB
  5. --]]
  6.  
  7.  
  8.  
  9. --[[
  10.     @description    "Uploads a file to pastebin"
  11.    
  12.    
  13.     @param    path, string
  14.     @param    name, string
  15.    
  16.     @return   boolean,  string,  string
  17. --]]
  18. function put( path, name )
  19.     if not fs.exists( path ) then
  20.         error( "File doesn't exist: " .. path, 2 )
  21.     end
  22.    
  23.     local f = fs.open( path, "r")
  24.     local code = f.readAll()
  25.     f.close()
  26.    
  27.     local key = "0ec2eb25b6166c0c27a394ae118ad829"
  28.     local response = http.post(
  29.         "http://pastebin.com/api/api_post.php",
  30.     "api_option=paste&"..
  31.     "api_dev_key="..key.."&"..
  32.     "api_paste_format=lua&"..
  33.     "api_paste_name="..textutils.urlEncode(name).."&"..
  34.     "api_paste_code="..textutils.urlEncode(code)
  35.     )
  36.        
  37.     if response then
  38.         local hResponse = response.readAll()
  39.         response.close()
  40.        
  41.         local sCode = string.match( hResponse, "[^/]+$" )
  42.         return true, name, sCode
  43.     else
  44.         return false
  45.     end
  46. end
  47.  
  48.  
  49. --[[
  50.     @description    "Downloads a file from pastebin"
  51.    
  52.    
  53.     @param    name,  string
  54.     @param    code,  string
  55.    
  56.     @return   boolean
  57. --]]
  58. function get( name, code )
  59.     local response = http.get( "http://pastebin.com/raw.php?i="..textutils.urlEncode(code) )
  60.     if response then
  61.         local sCode = response.readAll()
  62.         if sCode ~= nil and sCode ~= "" then
  63.             response.close()
  64.         local file = fs.open( name, "w" )
  65.         file.write(sCode)
  66.         file.close()
  67.         return true
  68.     end
  69.     end
  70.     return false
  71. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement