Googleドライブ共有ファイル一覧
kobakazu0429.iconなんとなくコードも載せときます
TAはリンク先のコードを見てください
700ファイルぐらいで40秒ぐらい
code:ipynb
import json
from google.colab import drive
from google.colab import auth
from googleapiclient.discovery import build
drive.mount('/content/drive')
auth.authenticate_user()
service = build('drive', 'v3')
BASE_FOLDER_ID = "スクエアのルートフォルダーのID"
FOLDER_MIME_TYPE = "application/vnd.google-apps.folder"
ROLES = {
"owner": "所有者",
"writer": "編集者",
"reader": "閲覧のみ",
"" : ""
}
def email_sort(email):
order = {
"knct.iwsq@gmail.com": 0,
"iwsq_ta@googlegroups.com": 1,
"iwsq_ta_admin@googlegroups.com": 2,
"iwsq_teacher@googlegroups.com": 3
}
return order.get(email, len(order))
def walk(parentId, name, result):
try:
page_token = None
while True:
response = (
service.files()
.list(
q=f"'{parentId}' in parents",
spaces="drive",
fields="nextPageToken, files(id, name, mimeType, owners/emailAddress, permissions(emailAddress, role, type))",
pageToken=page_token,
)
.execute()
)
for file in response.get("files", []):
currentPath = name + "/" + file.get("name")
"id": file.get("id"),
"owner": file.get("owners")0.get("emailAddress"), "permissions": file.get("permissions"),
}
if file.get("mimeType") == FOLDER_MIME_TYPE:
walk(file.get("id"), currentPath, result)
page_token = response.get("nextPageToken", None)
if page_token is None:
break
except Exception as e:
print(e)
def get_public_status(permissions):
if not any(permission"type" == "anyone" for permission in permissions): return "一般公開なし"
if any(permission"type" == "anyone" and permission"role" == "reader" for permission in permissions): return "URLによる一般公開(閲覧のみ)"
if any(permission"type" == "anyone" and permission"role" == "writer" for permission in permissions): return "URLによる一般公開(編集可)"
return "Unexpected Status"
def main():
result = {}
walk(BASE_FOLDER_ID, "スクエア", result)
# print(json.dumps(result, ensure_ascii=False))
users = set()
for file in result.values():
users.add(permission.get("emailAddress"))
# users.discard("knct.iwsq@gmail.com")
users.discard(None)
# print(users)
sorted_emails = sorted(users, key=email_sort)
tab = "\t"
for path, file in result.items():
roles = []
for email in sorted_emails:
role = next((permission"role" for permission in file"permissions" if permission.get("emailAddress") == email),"") main()
Deprecated
ファイル数の増加に伴い実行時間の制限に間に合わなくなったから