Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
anasty17
GitHub Repository: anasty17/mirror-leech-telegram-bot
Path: blob/master/add_to_team_drive.py
1635 views
1
from __future__ import print_function
2
3
import argparse
4
import glob
5
import googleapiclient.discovery
6
import json
7
import os
8
import pickle
9
import progress.bar
10
import sys
11
import time
12
from google.auth.transport.requests import Request
13
from google_auth_oauthlib.flow import InstalledAppFlow
14
15
stt = time.time()
16
17
parse = argparse.ArgumentParser(
18
description="A tool to add service accounts to a shared drive from a folder containing credential files."
19
)
20
parse.add_argument(
21
"--path",
22
"-p",
23
default="accounts",
24
help="Specify an alternative path to the service accounts folder.",
25
)
26
parse.add_argument(
27
"--credentials",
28
"-c",
29
default="./credentials.json",
30
help="Specify the relative path for the credentials file.",
31
)
32
parse.add_argument(
33
"--yes", "-y", default=False, action="store_true", help="Skips the sanity prompt."
34
)
35
parsereq = parse.add_argument_group("required arguments")
36
parsereq.add_argument(
37
"--drive-id", "-d", help="The ID of the Shared Drive.", required=True
38
)
39
40
args = parse.parse_args()
41
acc_dir = args.path
42
did = args.drive_id
43
credentials = glob.glob(args.credentials)
44
45
try:
46
open(credentials[0], "r")
47
print(">> Found credentials.")
48
except IndexError:
49
print(">> No credentials found.")
50
sys.exit(0)
51
52
if not args.yes:
53
# input('Make sure the following client id is added to the shared drive as Manager:\n' + json.loads((open(
54
# credentials[0],'r').read()))['installed']['client_id'])
55
input(
56
">> Make sure the **Google account** that has generated credentials.json\n is added into your Team Drive "
57
"(shared drive) as Manager\n>> (Press any key to continue)"
58
)
59
60
creds = None
61
if os.path.exists("token_sa.pickle"):
62
with open("token_sa.pickle", "rb") as token:
63
creds = pickle.load(token)
64
# If there are no (valid) credentials available, let the user log in.
65
if not creds or not creds.valid:
66
if creds and creds.expired and creds.refresh_token:
67
creds.refresh(Request())
68
else:
69
flow = InstalledAppFlow.from_client_secrets_file(
70
credentials[0],
71
scopes=[
72
"https://www.googleapis.com/auth/admin.directory.group",
73
"https://www.googleapis.com/auth/admin.directory.group.member",
74
],
75
)
76
# creds = flow.run_local_server(port=0)
77
creds = flow.run_console()
78
# Save the credentials for the next run
79
with open("token_sa.pickle", "wb") as token:
80
pickle.dump(creds, token)
81
82
drive = googleapiclient.discovery.build("drive", "v3", credentials=creds)
83
batch = drive.new_batch_http_request()
84
85
aa = glob.glob(f"{acc_dir}/*.json")
86
pbar = progress.bar.Bar("Readying accounts", max=len(aa))
87
for i in aa:
88
ce = json.loads(open(i, "r").read())["client_email"]
89
batch.add(
90
drive.permissions().create(
91
fileId=did,
92
supportsAllDrives=True,
93
body={"role": "organizer", "type": "user", "emailAddress": ce},
94
)
95
)
96
pbar.next()
97
pbar.finish()
98
print("Adding...")
99
batch.execute()
100
101
print("Complete.")
102
hours, rem = divmod((time.time() - stt), 3600)
103
minutes, sec = divmod(rem, 60)
104
print("Elapsed Time:\n{:0>2}:{:0>2}:{:05.2f}".format(int(hours), int(minutes), sec))
105
106