Skip to content

Instantly share code, notes, and snippets.

@HirbodBehnam
Last active October 4, 2020 13:52
Show Gist options
  • Save HirbodBehnam/baa693a156e9d44cd386097a94b4f32c to your computer and use it in GitHub Desktop.
Save HirbodBehnam/baa693a156e9d44cd386097a94b4f32c to your computer and use it in GitHub Desktop.
A simple google drive downloader
# A script to extract the ID from links like: https://drive.google.com/file/d/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/view?usp=sharing
for arg in "$@"
do
arrIN=(${arg//// })
python3 google-drive-downloader.py "${arrIN[4]}"
done
"""A simple app to download the google drive files with the id
You must have the credentials.json in the script path. Get it from https://developers.google.com/drive/api/v3/quickstart/python
Use the application like: python3 google-drive-downloader.py file_id
"""
from __future__ import print_function
import pickle
import io
import os.path
import sys
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from googleapiclient.http import MediaIoBaseDownload
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/drive.readonly']
def main():
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists(sys.path[0] + '/token.pickle'):
with open(sys.path[0] + '/token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
sys.path[0] + '/credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open(sys.path[0] + '/token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('drive', 'v3', credentials=creds)
# Call the Drive v3 API
file_id = sys.argv[1]
file = service.files().get(fileId=file_id).execute()
print("Downloading " + file['name'])
request = service.files().get_media(fileId=file_id)
fh = io.FileIO(file['name'], 'wb')
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print("Downloading " + str(int(status.progress() * 100)) + "%", end="\r")
print("Download of " + file['name'] + " is done")
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment