|
from googleapiclient.discovery import build |
|
from httplib2 import Http |
|
from oauth2client import file, client, tools |
|
import os |
|
import sys |
|
import argparse |
|
|
|
parser = argparse.ArgumentParser( |
|
usage="python share_dir.py --dirID 1tfOuRuWR_wkbPnfVVBWjRHGm4pLsbknj --file-suffix .tar.gz --email-suffix berkeley.edu", |
|
description=""" |
|
Share the google drive file to corresponding student, |
|
it will map regex (.+)\{file_suffix\} to (.+)@\{email-suffix}. |
|
|
|
For example, given `--file-suffix .tar.gz --email-suffix berkeley.edu` and a file |
|
called xmo.tar.gz under the google drive directory, it will share the xmo.tar.gz |
|
to [email protected] |
|
""", |
|
) |
|
parser.add_argument("--dirID", required=True, help="Directory ID") |
|
parser.add_argument("--file-suffix", required=True) |
|
parser.add_argument("--email-suffix", required=True) |
|
parser.add_argument("--yes", "-y", action="store_true", help="Skip confirmation") |
|
args = parser.parse_args() |
|
|
|
print(args) |
|
|
|
# If modifying these scopes, delete the file token.json. |
|
SCOPES = "https://www.googleapis.com/auth/drive" |
|
|
|
store = file.Storage("token.json") |
|
creds = store.get() |
|
if not creds or creds.invalid: |
|
flow = client.flow_from_clientsecrets("credentials.json", SCOPES) |
|
creds = tools.run_flow(flow, store) |
|
service = build("drive", "v3", http=creds.authorize(Http())) |
|
files = service.files() |
|
|
|
|
|
def list_file_items(dir_id): |
|
result = files.list(q=f"'{dir_id}' in parents").execute() |
|
return result["files"] |
|
|
|
|
|
def get_file(f_id, dir_name=os.getcwd()): |
|
meta_data = files.get(fileId=f_id).execute() |
|
file_name = meta_data["name"] |
|
result = files.get_media(fileId=f_id).execute() |
|
with open(os.path.join(dir_name, file_name), "wb") as f: |
|
f.write(result) |
|
|
|
|
|
def is_dir(f_id): |
|
result = files.get(fileId=f_id).execute() |
|
return result["mimeType"] == "application/vnd.google-apps.folder" |
|
|
|
assert is_dir(args.dirID), "The dirID is not a directory" |
|
|
|
file_items = list_file_items(args.dirID) |
|
|
|
def callback(request_id, response, exception): |
|
if exception: |
|
# Handle error |
|
print(exception) |
|
else: |
|
print("Success") |
|
|
|
batch = service.new_batch_http_request(callback=callback) |
|
for file_item in file_items: |
|
file_id = file_item['id'] |
|
name_ = file_item['name'] |
|
email_to_share = name_.replace(args.file_suffix, '@'+args.email_suffix) |
|
print("sharing {file_name} to {email}".format(file_name=name_, email=email_to_share)) |
|
user_permission = { |
|
'type': 'user', |
|
'role': 'writer', |
|
'emailAddress': email_to_share |
|
} |
|
batch.add(service.permissions().create( |
|
fileId=file_id, |
|
body=user_permission, |
|
fields='id', |
|
)) |
|
domain_permission = { |
|
'type': 'domain', |
|
'role': 'reader', |
|
'domain': args.email_suffix |
|
} |
|
batch.add(service.permissions().create( |
|
fileId=file_id, |
|
body=domain_permission, |
|
fields='id', |
|
)) |
|
|
|
if not args.yes: |
|
inp = input("confirm? [y/n]") |
|
if inp.lower() != 'y': |
|
sys.exit(0) |
|
|
|
batch.execute() |