Last active
December 31, 2020 12:35
-
-
Save smartass08/a3ae83a0a58ba2ee461b4c5672c5e67a to your computer and use it in GitHub Desktop.
AutoMount all gclone teamdrives available in account
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function | |
from google.oauth2 import service_account | |
from googleapiclient.discovery import build | |
import sys | |
import os | |
import time | |
SCOPES = ['https://www.googleapis.com/auth/drive.readonly'] | |
gc = "gclone mount --daemon" # Always use --daemon or it may break stuff | |
folder_location = "/Users/smartass08/Mounts" # Edit this to your path for Mounting folder | |
remote_name = "gc:" # Your base remote name which has sa and sa path added in rclone config | |
def getaccess(): | |
SERVICE_ACCOUNT_FILE = sys.argv[1] | |
creds = service_account.Credentials.from_service_account_file( | |
SERVICE_ACCOUNT_FILE, scopes=SCOPES) | |
service = build('drive', 'v3', credentials=creds) | |
return service | |
def folder_check(res): | |
lol = f"{res}" | |
if not os.path.exists(lol): | |
os.makedirs(lol) | |
return(lol) | |
else: | |
return(lol) | |
def mount(id, name): | |
id = "{" + id + "}" | |
folder = folder_check(f"{folder_location}/{name}") | |
folder = f'"{folder}"' | |
time.sleep(1) | |
command = os.system(f"{gc} {remote_name}:{id} {folder}") | |
if command is not 0: | |
print(f"Error mounting {name} : error code {command}, Retrying") | |
os.system(f"umount {folder}") # Edit this path to your system native fuse unnmounter | |
sleep(1) | |
os.system(f"{gc} {remote_name}:{id} {folder}") | |
def loop(service, NPT): | |
results = service.drives().list(pageSize=100).execute() | |
if NPT is not "None": | |
results = service.drives().list(pageSize=100, pageToken=NPT).execute() | |
for elements in results["drives"]: | |
print(f'Mounting {elements["name"]}') | |
if 'nextPageToken' in results: | |
loop(service, results["nextPageToken"]) | |
def main(): | |
service = getaccess() | |
loop(service, "None") | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage -
python3 mount.py "Path/to/sa.json"
Works pretty good on linux and mac, Windows needs testing and probably some edits too.
Needs donwa's gclone