Skip to content

Instantly share code, notes, and snippets.

@amane-katagiri
Last active May 21, 2020 10:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amane-katagiri/fde4b52e57660081c9c2ddeef8252de5 to your computer and use it in GitHub Desktop.
Save amane-katagiri/fde4b52e57660081c9c2ddeef8252de5 to your computer and use it in GitHub Desktop.
List shared links in your Google Drive.
{
"web":
{
"client_id": "XXX",
"project_id": "XXX",
"auth_uri":"https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_secret": "XXX",
"redirect_uris": ["http://localhost:8080/"],
"javascript_origins": ["http://localhost:8080"]
}
}
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from collections import defaultdict
import json
import sys
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
FILE_BASE = "https://drive.google.com/file/d/"
FOLDER_BASE = "https://drive.google.com/drive/folders/"
def get_list(fname, drive):
i = 0
fields = "kind,nextPageToken,incompleteSearch,items(id,permissions,parents)"
result = defaultdict(dict)
parents = list()
for file_list in drive.ListFile({"q": "trashed=false",
"fields": fields,
"maxResults": 100}):
for f in file_list:
i += 1
permissions = [x for x in f["permissions"] if x["role"] != "owner"]
if permissions:
for x in [x["id"] for x in f.get("parents")] or ["#ROOT"]:
parents.append(x)
result[x][f["id"]] = {"url": FILE_BASE + f["id"],
"users": {x["id"]: {"role": x["role"],
"name": x.get("name"),
"mail": x.get("emailAddress")}
for x in permissions
if x["id"] != "anyoneWithLink"}}
sys.stderr.write("{}: {}\n".format(i, f["id"]))
elif not i % 100:
sys.stderr.write("{}\n".format(i))
for k, v in result.items():
for x in v.keys():
if x in parents:
result[k][x]["url"] = FOLDER_BASE + x
if fname:
with open(fname, "w") as f:
f.write(json.dumps(result, ensure_ascii=False, indent=4, sort_keys=True))
else:
print(json.dumps(result, ensure_ascii=False, indent=4, sort_keys=True))
if __name__ == "__main__":
gauth = GoogleAuth()
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)
get_list(sys.argv[1] if len(sys.argv) > 1 else None, drive)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment