-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrive.py
284 lines (248 loc) · 10.6 KB
/
drive.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
from google.oauth2 import service_account
from googleapiclient.discovery import build
from googleapiclient.http import MediaIoBaseUpload
from io import BytesIO
import os
import pdb
from base64 import b64decode
from clint.textui import progress
from logger import logger
# If modifying these scopes, delete the file token.pickle.
G_DRIVE_SCOPES = [
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/drive.file'
]
GSUITE_USER_EMAIL = '[email protected]'
G_DRIVE_FOLDER_ID = '1dcI6AN_IidAT20BXXQYBv1Oxih0DKQFw'
G_DRIVE_FOLDER = 'https://drive.google.com/drive/folders/0AK09kp6LWV7OUk9PVA'
G_DRIVE_MANAGER_EMAILS = [
'zoom-recordings-to-drive@zoom-recordings-to-drive.iam.gserviceaccount.com'
]
G_DRIVE_CONTRIBUTER_EMAILS = [
'zoom-recordings-to-drive@zoom-recordings-to-drive.iam.gserviceaccount.com'
]
G_DRIVE_VIEWER_EMAILS = [
'zoom-recordings-to-drive@zoom-recordings-to-drive.iam.gserviceaccount.com'
]
# folders
ADMIN_FOLDER = 'Administration'
STUDENT_WORK_FOLDER = 'Student Work'
RECORDINGS_FOLDER = 'Recordings'
# roles
MANAGER_ROLE = 'writer'
CONTRIBUTOR_ROLE = 'commenter'
VIEWER_ROLE = 'reader'
# columns
COURSE_LINK = 'Google Drive: Course Link'
ADMIN_LINK = 'Google Drive: Admin'
STUDENT_WORK_LINK = 'Google Drive: Student Work'
RECORDINGS_LINK = 'Google Drive: Recordings'
UPLOAD_CHUNKSIZE = 1024
class GDrive:
students = []
folders = []
def __init__(self):
# OAuth2 using service key
BASE_PATH = os.path.abspath(os.curdir)
# BASE_PATH = '/root/miami-scripts'
self.credentials = service_account.Credentials.from_service_account_file(
f'{BASE_PATH}/creds/google_secret.json',
scopes=G_DRIVE_SCOPES,
subject=GSUITE_USER_EMAIL)
self.drive_service = build('drive', 'v3', credentials=self.credentials, cache_discovery=False)
def setup(self, spread):
self.ccs = spread.ccs # CampusCafe Course Schedule Sheet
self.sr = spread.sr # Student Roster
# self.fc = fc # Faculty CRM
# self.cs = cs # Calendar Schedule
def read_students_for_course(self, unique_id):
students = ['[email protected]']
for course, addr in zip(self.sr['COURSE'], self.sr['ADDRESS']):
if unique_id in course:
students.append(addr)
return students
def check_folder(self, folder_name, parent_id):
query = f"'{parent_id}' in parents"
page_token = None
folder_id = None
while True:
response = self.drive_service.files().list(q=query,
spaces='drive',
fields='nextPageToken, files(id, name)',
pageToken=page_token).execute()
for file in response.get('files', []):
# Process change
if file.get('name') == folder_name:
folder_id = file.get('id')
break
page_token = response.get('nextPageToken', None)
if page_token is None:
break
if not folder_id:
file_metadata = {'name': folder_name,"parents": [parent_id],'mimeType': 'application/vnd.google-apps.folder'}
file = self.drive_service.files().create(body=file_metadata,fields='id').execute()
folder_id = file.get('id')
return folder_id
def clear_old_recordings(self, meeting, recordings):
logger.info(f'--- clear old recordings for meeting {meeting["uuid"]} as not all of them uploaded correctly ')
try:
for rec in recordings:
self.drive_service.files().delete(fileId=rec['file_id']).execute()
except Exception as E:
logger.warning(str(E))
def upload_file(self, temporary_file_name, filename, file_type, vid, parent_id):
total_size = int(vid.headers.get('content-length'))
file_id = None
mimetype = ""
if file_type == 'MP4':
mimetype = "video/mp4"
filename = f'{filename}.mp4'
elif file_type == 'M4A':
mimetype = "audio/m4a"
filename = f'{filename}.m4a'
elif file_type == 'CHAT':
mimetype = 'text/plain'
filename = f'{filename}.txt'
elif file_type == 'TRANSCRIPT':
filename = f'{filename}.vtt'
mimetype = 'text/vtt'
with open(temporary_file_name, 'rb') as temporary_file:
chunk_size = 1024*1024
# file_bytes = BytesIO(vid.content)
media = MediaIoBaseUpload(temporary_file, mimetype, resumable=True, chunksize=chunk_size)
body = { "name": filename, "parents": [parent_id], "mimetype": mimetype }
res = self.drive_service.files().create(body=body, media_body=media, fields='id').execute()
os.remove(temporary_file_name)
file_id = res.get('id')
logger.info(f'**** uploaded file {filename} in drive folder_id {parent_id}')
return file_id
def create_drive_folder(self, name, parent_id, supportsAllDrives=True):
try:
file_metadata = {
'name': name,
'parents': [parent_id],
'writersCanShare': True,
'mimeType': 'application/vnd.google-apps.folder'
}
file = self.drive_service.files().create(
body=file_metadata,
fields='id',
supportsAllDrives=supportsAllDrives
).execute()
return file.get('id')
except Exception as E:
logger.warning(str(E))
def callback(self, request_id, response, exception):
if exception:
# Handle error
logger.warning(str(exception))
else:
pass
def share_drive_folder(self, file_id, emails, role):
try:
batch = self.drive_service.new_batch_http_request(callback=self.callback)
for email in emails:
user_permission = {
'type': 'user',
'role': role,
'emailAddress': email
}
batch.add(self.drive_service.permissions().create(
fileId=file_id,
body=user_permission,
supportsAllDrives=True,
fields='id',
))
batch.execute()
except Exception as E:
logger.warning(str(E))
def share_drive_folder_without_batch(self, file_id, emails, role):
try:
for email in emails:
user_permission = {
'type': 'user',
'role': role,
'emailAddress': email
}
self.drive_service.permissions().create(
fileId=file_id,
body=user_permission,
supportsAllDrives=True,
fields='id',
)
except Exception as E:
logger.warning(str(E))
def create_share_folder(self, name, parent_id, emails, role):
file_id = self.create_drive_folder(name, parent_id)
if role == 'commenter':
self.share_drive_folder_without_batch(file_id, emails, role)
else:
self.share_drive_folder(file_id, emails, role)
return file_id
def update_ccs_data(self, index, folder):
parent_link = f"https://drive.google.com/drive/folders/{folder['parent_id']}"
admin_link = f"https://drive.google.com/drive/folders/{folder['admin_id']}"
contr_link = f"https://drive.google.com/drive/folders/{folder['contr_id']}"
viewer_link = f"https://drive.google.com/drive/folders/{folder['viewer_id']}"
self.ccs.at[index, COURSE_LINK] = parent_link
self.ccs.at[index, ADMIN_LINK] = admin_link
self.ccs.at[index, STUDENT_WORK_LINK] = contr_link
self.ccs.at[index, RECORDINGS_LINK] = viewer_link
def create_drive_folders(self, name, index, unique_id):
# Create parent folder and then update df with link
parent_id = self.create_drive_folder(name, G_DRIVE_FOLDER_ID)
# Create Administration folder and then share manager permission and then update df with link
admin_id = self.create_share_folder(ADMIN_FOLDER, parent_id, G_DRIVE_MANAGER_EMAILS, MANAGER_ROLE)
students = self.read_students_for_course(unique_id)
# Create Student Work folder and then share contributor permission and then update df with link
contr_id = self.create_share_folder(STUDENT_WORK_FOLDER, parent_id, students, CONTRIBUTOR_ROLE)
# Create Recordings folder and then share viewer permission and then update df with link
viewer_id = self.create_share_folder(RECORDINGS_FOLDER, parent_id, G_DRIVE_VIEWER_EMAILS, VIEWER_ROLE)
folder = {
'name': name,
'parent_id': parent_id,
'admin_id': admin_id,
'contr_id': contr_id,
'viewer_id': viewer_id
}
self.folders.append(folder)
return folder
def is_new_folder(self, name):
myfolder = None
for folder in self.folders:
if folder['name'] == name:
myfolder = folder
break
return myfolder
def generate_links_and_share_permission(self):
'''
Read names from CampusCafe Course Schedule Sheet
@name: Course Number (Column C) + ": " + Description (Column H)
'''
index = 0
for cn, desc, unique_id in zip(self.ccs['Course Number'], self.ccs['Description'], self.ccs['Unique ID']):
name = f"{cn}: {desc}"
myfolder = self.is_new_folder(name)
if not myfolder:
myfolder = self.create_drive_folders(name, index, unique_id)
self.update_ccs_data(index, myfolder)
index += 1
# break
return self.ccs
if __name__ == '__main__':
g_drive = GDrive()