-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathgoogle_drive_client.py
220 lines (192 loc) · 8.46 KB
/
google_drive_client.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
import os
import json
from datetime import datetime
from google.oauth2.credentials import Credentials
from google.auth.transport.requests import Request
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
from googleapiclient.errors import HttpError
class Color:
PURPLE = "\033[95m"
CYAN = "\033[96m"
DARK_CYAN = "\033[36m"
BLUE = "\033[94m"
GREEN = "\033[92m"
YELLOW = "\033[93m"
RED = "\033[91m"
BOLD = "\033[1m"
UNDERLINE = "\033[4m"
END = "\033[0m"
SUCCESS_PAGE = """
<!DOCTYPE html>
<html>
<head>
<title>Authentication Successful</title>
</head>
<body>
<h1>Zoom Recording Downloader</h1>
<p>Authentication successful! You may close this window.</p>
</body>
</html>
"""
class GoogleDriveClient:
SCOPES = [
'https://www.googleapis.com/auth/drive.file',
'https://www.googleapis.com/auth/drive.metadata',
'https://www.googleapis.com/auth/drive.appdata'
]
def __init__(self, config):
self.config = config
self.service = None
self.credentials = None
self.root_folder_id = None
def authenticate(self):
"""Handle the OAuth flow and return True if successful."""
print(f"{Color.DARK_CYAN}Initializing Google Drive authentication...{Color.END}")
creds = None
token_file = self.config.get('token_file', 'token.json')
secrets_file = self.config.get('client_secrets_file', 'client_secrets.json')
if not os.path.exists(secrets_file):
print(f"{Color.RED}Error: {secrets_file} not found. Please configure OAuth credentials.{Color.END}")
return False
if os.path.exists(token_file):
try:
creds = Credentials.from_authorized_user_file(token_file, self.SCOPES)
except Exception as e:
print(f"{Color.YELLOW}Error reading token file: {e}{Color.END}")
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
print(f"{Color.DARK_CYAN}Refreshing expired token...{Color.END}")
try:
creds.refresh(Request())
except Exception as e:
print(f"{Color.YELLOW}Token refresh failed: {e}. Initiating new authentication...{Color.END}")
creds = None
if not creds:
try:
flow = InstalledAppFlow.from_client_secrets_file(secrets_file, self.SCOPES)
print(f"{Color.DARK_CYAN}Please login in your browser...{Color.END}")
creds = flow.run_local_server(port=0, success_message=SUCCESS_PAGE)
except Exception as e:
print(f"{Color.RED}Authentication failed: {e}{Color.END}")
return False
with open(token_file, 'w') as token:
token.write(creds.to_json())
print(f"{Color.GREEN}Token saved to {token_file}{Color.END}")
try:
self.service = build('drive', 'v3', credentials=creds)
self.credentials = creds
# Get user email
user_info = self.service.about().get(fields="user").execute()
email = user_info['user']['emailAddress']
print(f"{Color.GREEN}Successfully authenticated as {email}{Color.END}")
return True
except Exception as e:
print(f"{Color.RED}Failed to initialize Drive service: {e}{Color.END}")
return False
def _handle_upload_with_refresh(self, request):
"""Execute request with token refresh handling."""
try:
return request.execute()
except HttpError as e:
if e.resp.status in [401, 403]:
if self.credentials.refresh_token:
print(f"{Color.YELLOW}Token expired, refreshing...{Color.END}")
self.credentials.refresh(Request())
return self._handle_upload_with_refresh(request)
else:
print(f"{Color.YELLOW}Token refresh failed, re-authenticating...{Color.END}")
if self.authenticate():
return self._handle_upload_with_refresh(request)
raise
def create_folder(self, folder_name, parent_id=None):
"""Create a folder in Google Drive and return its ID."""
file_metadata = {
'name': folder_name,
'mimeType': 'application/vnd.google-apps.folder'
}
if parent_id:
file_metadata['parents'] = [parent_id]
try:
folder = self._handle_upload_with_refresh(
self.service.files().create(body=file_metadata, fields='id')
)
return folder.get('id')
except Exception as e:
print(f"{Color.RED}Failed to create folder {folder_name}: {str(e)}{Color.END}")
return None
def get_or_create_folder_path(self, folder_path, parent_id=None):
"""Navigate or create folder structure in Google Drive."""
current_parent = parent_id
for folder in folder_path.split(os.sep):
if not folder:
continue
query = f"name='{folder}' and mimeType='application/vnd.google-apps.folder'"
if current_parent:
query += f" and '{current_parent}' in parents"
try:
results = self._handle_upload_with_refresh(
self.service.files().list(
q=query,
spaces='drive',
fields='files(id)'
)
)
if results.get('files'):
current_parent = results['files'][0]['id']
else:
current_parent = self.create_folder(folder, current_parent)
if not current_parent:
return None
except Exception as e:
print(f"{Color.RED}Failed to navigate folders: {str(e)}{Color.END}")
return None
return current_parent
def upload_file(self, local_path, folder_name, filename):
"""Upload file to Google Drive with retry logic."""
try:
folder_id = self.get_or_create_folder_path(folder_name, self.root_folder_id)
if not folder_id:
return False
file_metadata = {
'name': filename,
'parents': [folder_id]
}
media = MediaFileUpload(
local_path,
resumable=True
)
max_retries = int(self.config.get('max_retries', 3))
retry_delay = int(self.config.get('retry_delay', 5))
failed_log = self.config.get('failed_log', 'failed-uploads.log')
for attempt in range(max_retries):
try:
print(f" Attempt {attempt + 1} of {max_retries}...")
request = self.service.files().create(
body=file_metadata,
media_body=media,
fields='id'
)
self._handle_upload_with_refresh(request)
print(f" {Color.GREEN}Success!{Color.END}")
return True
except Exception as e:
if attempt < max_retries - 1:
print(f" {Color.YELLOW}Retry after {retry_delay} seconds...{Color.END}")
import time
time.sleep(retry_delay)
else:
print(f"{Color.RED}Upload failed: {str(e)}{Color.END}")
with open(failed_log, 'a') as log:
log.write(f"{datetime.now()}: Failed to upload {filename} - {str(e)}\n")
return False
except Exception as e:
print(f"{Color.RED}Upload preparation failed: {str(e)}{Color.END}")
return False
def initialize_root_folder(self):
"""Create root folder with timestamp."""
timestamp = datetime.now().strftime("%Y-%m-%d-%H%M%S")
root_folder_name = f"{self.config.get('root_folder_name', 'zoom-recording-downloader')}-{timestamp}"
self.root_folder_id = self.create_folder(root_folder_name)
return self.root_folder_id is not None