-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
210 lines (170 loc) · 6.48 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import os
import io
from pathlib import Path
from datetime import datetime
from concurrent.futures import ThreadPoolExecutor
from googleapiclient.http import MediaIoBaseDownload
from google.oauth2 import service_account
from googleapiclient.discovery import build
# Define the SCOPES. If modifying these scopes, delete the token.json.
# This scope allows for full read/write access to the authenticated user's account.
SCOPES = ["https://www.googleapis.com/auth/drive"]
# The path to the service account key file.
SERVICE_ACCOUNT_FILE = "service_key.json"
def create_service():
"""Create a service object for the Google Drive API."""
# Load the service account credentials from the key file
credentials = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, scopes=SCOPES
)
# Build the service object for the Google Drive API
service = build("drive", "v3", credentials=credentials)
return service
def list_files(service) -> list:
all_files = []
page_token = None
while True:
results = (
service.files()
.list(
pageSize=1000,
fields="nextPageToken, files(id, name, parents, mimeType)",
pageToken=page_token,
)
.execute()
)
items = results.get("files", [])
all_files.extend(items)
page_token = results.get("nextPageToken", None)
if page_token is None:
break
print(f"Found {len(all_files)} files.")
# print(all_files)
def recurse_to_the_root(item_id):
item = next((item for item in all_files if item["id"] == item_id), None)
if "parents" in item:
parent_id = item["parents"][0]
return f"{recurse_to_the_root(parent_id)}/{item['name']}"
else:
return item["name"]
files = [
item
for item in all_files
if item["mimeType"] != "application/vnd.google-apps.folder"
]
for file in files:
file["path"] = recurse_to_the_root(file["id"])
return files
# def list_files(service) -> list:
# # Call the Drive v3 API
# results = (
# service.files()
# .list(
# pageSize=1000, # you can change the page size
# fields="nextPageToken, files(id, name, parents, mimeType)",
# )
# .execute()
# )
# items = results.get("files", [])
# if len(items) == 1000:
# raise Exception(
# "You might have more than 1000 files. You need to handle pagination."
# )
# else:
# print(f"Found {len(items)} files.")
# def recurse_to_the_root(item_id):
# item = next((item for item in items if item["id"] == item_id), None)
# if "parents" in item:
# parent_id = item["parents"][0]
# return f"{recurse_to_the_root(parent_id)}/{item['name']}"
# else:
# return item["name"]
# files = [
# item
# for item in items
# if item["mimeType"] != "application/vnd.google-apps.folder"
# ]
# for file in files:
# file["path"] = recurse_to_the_root(file["id"])
# # need to handle pagination here if you have more than 1000 files
# return files
def is_folder_and_download(service, file):
if file["mimeType"] == "application/vnd.google-apps.folder":
print(f"Folder: {file['name']}")
# List files in the folder
results = (
service.files()
.list(
q=f"'{file['id']}' in parents",
fields="nextPageToken, files(id, name, mimeType)",
)
.execute()
)
items = results.get("files", [])
if not items:
print("No files found in the folder.")
else:
download_files(service, items) # Download each file in the folder
return True
return False
def download_file_kernel(service, file):
# for file in files:
print(f"File: {file['name']}, MIME Type: {file['mimeType']}")
if is_folder_and_download(service, file):
raise Exception("Folder found among files.")
# Check the file's MIME type. If it's a Google Doc, Sheet, Slide, etc.,
# we need to export it. Otherwise, we can just download it directly.
if file["mimeType"] == "application/vnd.google-apps.document":
request = service.files().export_media(
fileId=file["id"],
mimeType="application/vnd.openxmlformats-officedocument.wordprocessingml.document",
)
filename = file["name"] + ".docx"
elif file["mimeType"] == "application/vnd.google-apps.spreadsheet":
request = service.files().export_media(
fileId=file["id"],
mimeType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
)
filename = file["name"] + ".xlsx"
elif file["mimeType"] == "application/vnd.google-apps.presentation":
request = service.files().export_media(
fileId=file["id"],
mimeType="application/vnd.openxmlformats-officedocument.presentationml.presentation",
)
filename = file["name"] + ".pptx"
else:
request = service.files().get_media(fileId=file["id"])
filename = file["name"]
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print(f"Download {int(status.progress() * 100)}%")
# Create the directory if it doesn't exist
os.makedirs(Path(file["path"]).parent, exist_ok=True)
# The file's content is stored in fh (a BytesIO object)
with io.open(Path(file["path"]).parent / filename, "wb") as f:
fh.seek(0)
f.write(fh.read())
def download_files(service, files):
# It's generally recommended to be set to the number of processors on the machine, times 5
# with ThreadPoolExecutor(max_workers=os.cpu_count() / 4) as executor:
# executor.map(lambda file: download_file_kernel(service, file), files)
for file in files:
download_file_kernel(service, file)
def main():
start = datetime.now()
service = create_service()
files = list_files(service)
if not files:
print("No files found.")
else:
print("Files:")
for item in files:
print("{0} ({1})".format(item["name"], item["id"]))
download_files(service, files)
print(f"Time taken: {(datetime.now() - start)}")
print("All downloads complete!")
if __name__ == "__main__":
main()