Repository navigation
Expand file tree
/
Copy pathgoogle_docs_client.py
More file actions
352 lines (291 loc) · 13.2 KB
/
Copy pathgoogle_docs_client.py
File metadata and controls
352 lines (291 loc) · 13.2 KB
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import os
import pickle
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
class GoogleDocsClient:
def __init__(self, credentials_path='token.pickle'):
"""Initialize Google Docs client using existing credentials"""
self.service = None
self.drive_service = None
self.load_credentials(credentials_path)
def load_credentials(self, credentials_path):
"""Load credentials from pickle file"""
if not os.path.exists(credentials_path):
raise FileNotFoundError(
f"Credentials file '{credentials_path}' not found. "
"Please authenticate with Gmail first."
)
with open(credentials_path, 'rb') as token:
creds = pickle.load(token)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
with open(credentials_path, 'wb') as token:
pickle.dump(creds, token)
self.service = build('docs', 'v1', credentials=creds)
self.drive_service = build('drive', 'v3', credentials=creds)
def get_plain_document_content(self, document_id: str) -> str:
"""
Fetch text content from a plain Google Doc (no tabs).
Optimized for documents without tab structure (e.g., from Drive folders).
Args:
document_id: The ID of the Google Doc
Returns:
str: The full text content of the document
"""
try:
# Get document without tabs content for better performance
document = self.service.documents().get(
documentId=document_id
).execute()
# Extract content from document body
doc_content = document.get('body', {}).get('content', [])
all_content = self._extract_content_from_elements(doc_content)
return ''.join(all_content)
except HttpError as error:
print(f'An error occurred fetching document {document_id}: {error}')
return ''
def get_document_content(self, document_id: str, prefer_transcript: bool = True) -> str:
"""
Fetch the text content from a Google Doc.
Prefers the 'Transcript' tab if available, falls back to 'Notes' tab.
Args:
document_id: The ID of the Google Doc
prefer_transcript: If True, prefer the Transcript tab over Notes tab (default: True)
Returns:
str: The full text content of the transcript or notes
"""
try:
# Get document with tabs content
document = self.service.documents().get(
documentId=document_id,
includeTabsContent=True
).execute()
all_content = []
# Check if document has tabs (newer format)
if 'tabs' in document:
transcript_tab = None
notes_tab = None
# Find Transcript and Notes tabs
for tab in document['tabs']:
tab_title = tab.get('tabProperties', {}).get('title', '')
if tab_title.lower() == 'transcript':
transcript_tab = tab
elif tab_title.lower() == 'notes':
notes_tab = tab
# Prefer Transcript tab if available, otherwise use Notes tab
selected_tab = None
if prefer_transcript and transcript_tab:
selected_tab = transcript_tab
print(f' → Processing Transcript Tab')
elif notes_tab:
selected_tab = notes_tab
if transcript_tab:
print(f' → Using Notes tab (Transcript tab exists but prefer_transcript=False)')
else:
print(f' → Using Notes tab (no Transcript tab available)')
if selected_tab:
doc_content = selected_tab.get('documentTab', {}).get('body', {}).get('content', [])
all_content.extend(self._extract_content_from_elements(doc_content))
else:
# No Transcript or Notes tab found, use first tab
print(f'Warning: Neither Transcript nor Notes tab found. Available tabs:')
for tab in document['tabs']:
tab_title = tab.get('tabProperties', {}).get('title', 'Untitled')
print(f' - {tab_title}')
print('Using first tab as fallback...')
first_tab = document['tabs'][0]
doc_content = first_tab.get('documentTab', {}).get('body', {}).get('content', [])
all_content.extend(self._extract_content_from_elements(doc_content))
else:
# Legacy format (no tabs)
doc_content = document.get('body', {}).get('content', [])
all_content.extend(self._extract_content_from_elements(doc_content))
return ''.join(all_content)
except HttpError as error:
print(f'An error occurred fetching document {document_id}: {error}')
return ''
def _extract_content_from_elements(self, elements):
"""Helper method to extract text from document elements"""
content = []
for element in elements:
if 'paragraph' in element:
paragraph = element['paragraph']
for text_run in paragraph.get('elements', []):
if 'textRun' in text_run:
content.append(text_run['textRun']['content'])
elif 'table' in element:
# Handle tables
table = element['table']
for row in table.get('tableRows', []):
for cell in row.get('tableCells', []):
for cell_element in cell.get('content', []):
if 'paragraph' in cell_element:
for text_run in cell_element['paragraph'].get('elements', []):
if 'textRun' in text_run:
content.append(text_run['textRun']['content'])
return content
def extract_doc_id_from_url(self, url: str) -> str:
"""
Extract document ID from a Google Docs URL
Args:
url: Google Docs URL (e.g., https://docs.google.com/document/d/DOCUMENT_ID/edit)
Returns:
str: The document ID
"""
import re
# Pattern: /document/d/{DOCUMENT_ID}/
match = re.search(r'/document/d/([a-zA-Z0-9-_]+)', url)
if match:
return match.group(1)
return url # Return as-is if not a URL pattern
def create_document(self, title: str, content: str, folder_id: str = None) -> dict:
"""
Create a new Google Doc with markdown-formatted content converted to proper formatting
Args:
title: Document title
content: Markdown content to convert and insert
folder_id: Optional Google Drive folder ID to place the document
Returns:
dict: Document metadata with 'id' and 'url'
"""
try:
# Create a new document
doc = self.service.documents().create(body={'title': title}).execute()
doc_id = doc.get('documentId')
# Parse markdown and generate formatting requests
requests = self._convert_markdown_to_docs_requests(content)
# Apply all formatting in a single batch
if requests:
self.service.documents().batchUpdate(
documentId=doc_id,
body={'requests': requests}
).execute()
# Move to folder if specified
if folder_id:
self._move_to_folder(doc_id, folder_id)
doc_url = f"https://docs.google.com/document/d/{doc_id}/edit"
return {
'id': doc_id,
'url': doc_url,
'title': title
}
except HttpError as error:
print(f'An error occurred creating document: {error}')
return None
def _convert_markdown_to_docs_requests(self, content: str) -> list:
"""
Convert markdown content to Google Docs API formatting requests
Args:
content: Markdown-formatted string
Returns:
list: Google Docs API requests for formatted content
"""
import re
requests = []
lines = content.split('\n')
current_index = 1 # Google Docs index starts at 1
# First pass: Insert all text
full_text = []
formatting_map = [] # Track (start_index, end_index, format_type, level)
for line in lines:
line_start = len(''.join(full_text))
# Check for headers
if line.startswith('## '):
clean_line = line[3:] + '\n'
full_text.append(clean_line)
formatting_map.append((line_start, line_start + len(clean_line) - 1, 'heading', 2))
elif line.startswith('# '):
clean_line = line[2:] + '\n'
full_text.append(clean_line)
formatting_map.append((line_start, line_start + len(clean_line) - 1, 'heading', 1))
# Check for horizontal rules (skip them or convert to line break)
elif line.strip() == '---' or line.strip().startswith('────'):
full_text.append('\n')
else:
# Handle inline formatting (bold, quotes, bullets)
processed_line = line + '\n'
# Track bold formatting
bold_pattern = r'\*\*([^*]+)\*\*'
for match in re.finditer(bold_pattern, line):
# Calculate position in full text
match_start = line_start + match.start()
match_end = line_start + match.end() - 4 # Subtract the ** markers
formatting_map.append((match_start, match_end, 'bold', None))
# Remove markdown syntax for the actual text
processed_line = re.sub(r'\*\*([^*]+)\*\*', r'\1', processed_line)
# Handle blockquotes (lines starting with >)
if line.strip().startswith('>'):
processed_line = ' ' + processed_line.lstrip('> ') # Indent instead
# Handle bullet points (lines starting with •)
if line.strip().startswith('•'):
# Keep the bullet
pass
full_text.append(processed_line)
full_text_str = ''.join(full_text)
# Insert all text at once
requests.append({
'insertText': {
'location': {'index': 1},
'text': full_text_str
}
})
# Apply formatting (must be done after text insertion)
# Sort by start position (descending) to apply from end to start
formatting_map.sort(key=lambda x: x[0], reverse=True)
for start_idx, end_idx, format_type, level in formatting_map:
# Adjust indices (Google Docs is 1-indexed)
start_idx += 1
end_idx += 1
if format_type == 'heading':
requests.append({
'updateParagraphStyle': {
'range': {
'startIndex': start_idx,
'endIndex': end_idx
},
'paragraphStyle': {
'namedStyleType': f'HEADING_{level}'
},
'fields': 'namedStyleType'
}
})
elif format_type == 'bold':
requests.append({
'updateTextStyle': {
'range': {
'startIndex': start_idx,
'endIndex': end_idx
},
'textStyle': {
'bold': True
},
'fields': 'bold'
}
})
return requests
def _move_to_folder(self, document_id: str, folder_id: str):
"""
Move a document to a specific folder
Args:
document_id: The ID of the document to move
folder_id: The ID of the destination folder
"""
try:
# Get the file's current parents
file = self.drive_service.files().get(
fileId=document_id,
fields='parents'
).execute()
previous_parents = ",".join(file.get('parents', []))
# Move the file to the new folder
self.drive_service.files().update(
fileId=document_id,
addParents=folder_id,
removeParents=previous_parents,
fields='id, parents'
).execute()
except HttpError as error:
print(f'An error occurred moving document to folder: {error}')