Fabrimat

gist

Jun 17th, 2021 (edited)
559
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local function printUsage()
  2.     print( "Usages:" )
  3.     print( "gist get <url> <filename>" )
  4. end
  5.  
  6. local tArgs = { ... }
  7. if #tArgs < 2 then
  8.     printUsage()
  9.     return
  10. end
  11.  
  12. if not http then
  13.     print( "Github Gist requires http API" )
  14.     print( "Set enableAPI_http to 1 in mod_ComputerCraft.cfg" )
  15.     return
  16. end
  17.  
  18. local sCommand = tArgs[1]
  19. if sCommand == "get" then
  20.     -- Download a file from pastebin.com
  21.     if #tArgs < 3 then
  22.         printUsage()
  23.         return
  24.     end
  25.  
  26.     -- Determine file to download
  27.     local sCode = tArgs[2]
  28.     local sFile = tArgs[3]
  29.     local sPath = shell.resolve( sFile )
  30.     if fs.exists( sPath ) then
  31.         print( "File already exists" )
  32.         return
  33.     end
  34.    
  35.     -- GET the contents from pastebin
  36.     write( "Connecting to gist.github.com... " )
  37.     local response = http.get(
  38.         "http://gist.githubusercontent.lan/"..textutils.urlEncode( sCode )
  39.         )
  40.        
  41.     if response then
  42.         print( "Success." )
  43.        
  44.         local sResponse = response.readAll()
  45.         response.close()
  46.        
  47.         local file = fs.open( sPath, "w" )
  48.         file.write( sResponse )
  49.         file.close()
  50.        
  51.         print( "Downloaded as "..sFile )
  52.        
  53.     else
  54.         print( "Failed." )
  55.     end
  56.  
  57. else
  58.     printUsage()
  59.     return
  60. end
RAW Paste Data