add credentials.json workflow

This commit is contained in:
guidotheelen 2020-04-15 11:53:27 +02:00
parent b61711bcba
commit 4d82f5a9d3
2 changed files with 18 additions and 10 deletions

1
.gitignore vendored
View file

@ -2,3 +2,4 @@ __pycache__/
*.pyc *.pyc
waveflag waveflag
build/ build/
credentials.json

View file

@ -2,7 +2,7 @@
from __future__ import print_function from __future__ import print_function
import pickle import pickle
import os.path from os import path, makedirs
import io import io
import fire import fire
import sys import sys
@ -23,13 +23,13 @@ def main(folder_name, output_dir):
# Get the folder instance # Get the folder instance
folder_id = get_folder_id(service, folder_name) folder_id = get_folder_id(service, folder_name)
# Create output dir # Create output_dir
output_dir = create_dir(output_dir) output_dir = create_dir(output_dir)
# Get the file IDs # Get the file IDs
file_list = get_file_list(service, folder_id) file_list = get_file_list(service, folder_id)
# Fetch the files from the drive # Download the files from the drive and put them in the output_dir
download_files(service, file_list, output_dir) download_files(service, file_list, output_dir)
@ -40,7 +40,7 @@ def get_service():
# time. # time.
creds = None creds = None
if os.path.exists("token.pickle"): if path.exists("token.pickle"):
with open("token.pickle", "rb") as token: with open("token.pickle", "rb") as token:
creds = pickle.load(token) creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in. # If there are no (valid) credentials available, let the user log in.
@ -48,6 +48,14 @@ def get_service():
if creds and creds.expired and creds.refresh_token: if creds and creds.expired and creds.refresh_token:
creds.refresh(Request()) creds.refresh(Request())
else: else:
if not path.exists("credentials.json"):
print("""
Your missing 'credentials.json'.
Please get this file here:
https://developers.google.com/drive/api/v3/quickstart/python
and include it in the root of your project.
""")
sys.exit(1)
flow = InstalledAppFlow.from_client_secrets_file("credentials.json", SCOPES) flow = InstalledAppFlow.from_client_secrets_file("credentials.json", SCOPES)
creds = flow.run_local_server(port=0) creds = flow.run_local_server(port=0)
# Save the credentials for the next run # Save the credentials for the next run
@ -69,6 +77,7 @@ def get_folder_id(service, folder_name):
if total != 1: if total != 1:
print(f'{total} folders found, needs exately one') print(f'{total} folders found, needs exately one')
sys.exit(1) sys.exit(1)
else: else:
folder_id = folder['files'][0]['id'] folder_id = folder['files'][0]['id']
@ -76,12 +85,10 @@ def get_folder_id(service, folder_name):
def create_dir(dir_name): def create_dir(dir_name):
if not os.path.exists(dir_name): if not path.exists(dir_name):
os.makedirs(dir_name) makedirs(dir_name)
folder = os.path(dir_name) return dir_name
return folder
def get_file_list(service, folder_id): def get_file_list(service, folder_id):
@ -118,7 +125,7 @@ def download_files(service, file_list, output_dir):
while done is False: while done is False:
status, done = downloader.next_chunk() status, done = downloader.next_chunk()
filelocation = f"{output_dir}/{filename['name']}" filelocation = f"./{output_dir}/{filename}"
with open(filelocation, "wb") as f: with open(filelocation, "wb") as f:
f.write(fh.getbuffer()) f.write(fh.getbuffer())