Advertisement
xX-AAAAAAAAAA-Xx

ComputerCraft drivedown (Download files from google drive)

Aug 22nd, 2024 (edited)
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.02 KB | None | 0 0
  1. -- drivedown.lua
  2. local args = {...}
  3. if #args < 2 then
  4.     print("Usage: drivedown <URL> <filename>")
  5.     return
  6. end
  7.  
  8. local url = args[1]
  9. local filename = args[2]
  10.  
  11. -- Function to extract file ID from Google Drive URL
  12. local function getFileId(url)
  13.     local id = url:match("drive.google.com/file/d/([%w_-]+)/")
  14.     if not id then
  15.         id = url:match("drive.google.com/file/d/([%w_-]+)/view")
  16.     end
  17.     return id
  18. end
  19.  
  20. -- Function to download the file
  21. local function downloadFile(fileId, filename)
  22.     local downloadUrl = "https://drive.google.com/uc?export=download&id=" .. fileId
  23.     local response = http.get(downloadUrl)
  24.     if response then
  25.         local file = fs.open(filename, "wb")
  26.         file.write(response.readAll())
  27.         file.close()
  28.         response.close()
  29.         print("File downloaded successfully.")
  30.     else
  31.         print("Error downloading file.")
  32.     end
  33. end
  34.  
  35. local fileId = getFileId(url)
  36. if fileId then
  37.     downloadFile(fileId, filename)
  38. else
  39.     print("Invalid URL.")
  40. end
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement