Skip to content

Instantly share code, notes, and snippets.

@idcesares
Created July 4, 2023 20:45
Show Gist options
  • Save idcesares/64473ba27d7bc6c7e08d278db5889083 to your computer and use it in GitHub Desktop.
Save idcesares/64473ba27d7bc6c7e08d278db5889083 to your computer and use it in GitHub Desktop.
Script to download multiple files on Google Drive with Colab and save
# Mount Google Drive
from google.colab import drive
drive.mount('/gdrive')
# Script to download multiple files on Google Drive with Colab
import requests
import os
def download_file_from_google_drive(id, destination):
URL = "https://docs.google.com/uc?export=download"
session = requests.Session()
response = session.get(URL, params = { 'id' : id }, stream = True)
token = get_confirm_token(response)
if token:
params = { 'id' : id, 'confirm' : token }
response = session.get(URL, params = params, stream = True)
save_response_content(response, destination)
def get_confirm_token(response):
for key, value in response.cookies.items():
if key.startswith('download_warning'):
return value
return None
def save_response_content(response, destination):
CHUNK_SIZE = 32768
with open(destination, "wb") as f:
for chunk in response.iter_content(CHUNK_SIZE):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
# Array of Google Drive links
links = [
]
# Directory to save files
destination_dir = "/gdrive/MyDrive/Download-Links/"
# Ensure the directory exists
os.makedirs(destination_dir, exist_ok=True)
for link in links:
# Extract the file ID from the URL
file_id = link.split('/')[-2]
# Download the file
download_file_from_google_drive(file_id, os.path.join(destination_dir, f"{file_id}.pdf"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment