-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport.py
159 lines (119 loc) · 4.76 KB
/
export.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
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from google.auth.transport.requests import Request
from google.oauth2 import credentials
import json
import os
import sys
CLIENT_SECRETS_FILE = 'client_secrets.json'
CREDENTIALS_FILE = 'credentials.json'
EXPORT_DIR = 'export'
def get_credentials():
with open(CREDENTIALS_FILE, 'r', encoding='utf-8') as f:
data = json.load(f)
return credentials.Credentials(
data['token'],
data["refresh_token"],
data["id_token"],
data["token_uri"],
data["client_id"],
data["client_secret"],
data["scopes"])
return None
def save_credentials(credentials):
data = {
"token": "",
"refresh_token": credentials.refresh_token,
"id_token": credentials.id_token,
"token_uri": credentials.token_uri,
"client_id": credentials.client_id,
"client_secret": credentials.client_secret,
"scopes": credentials.scopes
}
with open(CREDENTIALS_FILE, 'w', encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, indent=4)
def get_authenticated_service():
print('Authenticating ...')
flow = InstalledAppFlow.from_client_secrets_file(
'client_secrets.json',
scopes=['https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/userinfo.profile', 'openid', 'https://www.googleapis.com/auth/drive.readonly'])
credentials = get_credentials()
if (credentials is None):
flow.run_local_server()
credentials = flow.credentials
save_credentials(credentials)
elif (credentials.expired):
credentials.refresh()
save_credentials(credentials)
print('Authentication successful.')
return build('drive', 'v3', credentials=credentials)
def get_extension(item):
if item['mimeType'] == 'application/vnd.google-apps.document':
return ".odt"
elif item['mimeType'] == 'application/vnd.google-apps.presentation':
return ".odp"
elif item['mimeType'] == 'application/vnd.google-apps.spreadsheet':
return ".ods"
return ""
def get_target_mime_type(item):
if item['mimeType'] == 'application/vnd.google-apps.document':
return "application/vnd.oasis.opendocument.text"
elif item['mimeType'] == 'application/vnd.google-apps.presentation':
return "application/vnd.oasis.opendocument.presentation"
elif item['mimeType'] == 'application/vnd.google-apps.spreadsheet':
return "application/vnd.oasis.opendocument.spreadsheet"
return ""
def download_file(service, item, path):
content = None
extension = get_extension(item)
mimeType = get_target_mime_type(item)
folderPath = EXPORT_DIR + "/" + path
filePath = folderPath + "/" + item['name'].replace("/", "_") + extension
if not extension or not mimeType:
print("Error: File {} of type {} can not be exported!".format(item['name'], item['mimeType']))
return
if os.path.exists(filePath):
print(" File already exits!")
return
if not os.path.exists(folderPath):
os.makedirs(folderPath)
try:
# Download exported file
content = service.files().export(fileId=item['id'], mimeType=mimeType).execute()
# Save downloaded file
file = open(filePath, "wb")
file.write(content)
file.close()
except:
e = sys.exc_info()[0]
print("Error: Could not export file! ({})".format(e))
def export_docs(service, parentId, parentName):
results = service.files().list(
q="'{}' in parents and trashed = false and (mimeType='application/vnd.google-apps.document' or mimeType='application/vnd.google-apps.presentation' or mimeType='application/vnd.google-apps.spreadsheet')".format(parentId),
pageSize=1000
).execute()
items = results.get('files', [])
for item in items:
print(" " + item['name'] + " ({})".format(item['mimeType']))
download_file(service, item, parentName)
def export_folders(service, parentId, parentName):
print(parentName + ":")
# Files
export_docs(service, parentId, parentName)
print("")
# Folders
results = service.files().list(
q="'{}' in parents and mimeType = 'application/vnd.google-apps.folder' and trashed = false".format(parentId),
pageSize=1000
).execute()
items = results.get('files', [])
for item in items:
export_folders(service, item['id'].replace("/", "_"), parentName + "/" + item['name'])
print("")
def start_export():
service = get_authenticated_service()
print('Starting docs export ...\n')
export_folders(service, 'root', "MyDrive")
if __name__== "__main__":
start_export()
print("Finished.")