|
0981a08…
|
noreply
|
1 |
"""Google Meet recording source using the gws CLI (googleworkspace/cli). |
|
0981a08…
|
noreply
|
2 |
|
|
0981a08…
|
noreply
|
3 |
Fetches Meet recordings and companion transcripts from Google Drive |
|
0981a08…
|
noreply
|
4 |
via the `gws` CLI tool. |
|
0981a08…
|
noreply
|
5 |
|
|
0981a08…
|
noreply
|
6 |
Requires: npm install -g @googleworkspace/cli |
|
0981a08…
|
noreply
|
7 |
Auth: gws auth login (interactive) or GOOGLE_WORKSPACE_CLI_CREDENTIALS_FILE (headless) |
|
0981a08…
|
noreply
|
8 |
""" |
|
0981a08…
|
noreply
|
9 |
|
|
0981a08…
|
noreply
|
10 |
import json |
|
0981a08…
|
noreply
|
11 |
import logging |
|
0981a08…
|
noreply
|
12 |
import re |
|
0981a08…
|
noreply
|
13 |
import shutil |
|
0981a08…
|
noreply
|
14 |
import subprocess |
|
0981a08…
|
noreply
|
15 |
from pathlib import Path |
|
0981a08…
|
noreply
|
16 |
from typing import List, Optional |
|
0981a08…
|
noreply
|
17 |
|
|
0981a08…
|
noreply
|
18 |
from video_processor.sources.base import BaseSource, SourceFile |
|
0981a08…
|
noreply
|
19 |
from video_processor.sources.gws_source import _run_gws |
|
0981a08…
|
noreply
|
20 |
|
|
0981a08…
|
noreply
|
21 |
logger = logging.getLogger(__name__) |
|
0981a08…
|
noreply
|
22 |
|
|
0981a08…
|
noreply
|
23 |
|
|
0981a08…
|
noreply
|
24 |
class MeetRecordingSource(BaseSource): |
|
0981a08…
|
noreply
|
25 |
""" |
|
0981a08…
|
noreply
|
26 |
Fetch Google Meet recordings and transcripts from Google Drive via the gws CLI. |
|
0981a08…
|
noreply
|
27 |
|
|
0981a08…
|
noreply
|
28 |
Meet stores recordings as MP4 files in Drive (typically in a "Meet Recordings" |
|
0981a08…
|
noreply
|
29 |
folder) and auto-generated transcripts as Google Docs. |
|
0981a08…
|
noreply
|
30 |
|
|
0981a08…
|
noreply
|
31 |
Usage: |
|
0981a08…
|
noreply
|
32 |
source = MeetRecordingSource() |
|
0981a08…
|
noreply
|
33 |
source.authenticate() |
|
0981a08…
|
noreply
|
34 |
recordings = source.list_videos() |
|
0981a08…
|
noreply
|
35 |
source.download_all(recordings, Path("./recordings")) |
|
0981a08…
|
noreply
|
36 |
|
|
0981a08…
|
noreply
|
37 |
# Fetch transcript for a specific recording |
|
0981a08…
|
noreply
|
38 |
transcript = source.fetch_transcript("Meet Recording 2026-03-07") |
|
0981a08…
|
noreply
|
39 |
""" |
|
0981a08…
|
noreply
|
40 |
|
|
0981a08…
|
noreply
|
41 |
def __init__(self, drive_folder_id: Optional[str] = None): |
|
0981a08…
|
noreply
|
42 |
self.drive_folder_id = drive_folder_id |
|
0981a08…
|
noreply
|
43 |
|
|
0981a08…
|
noreply
|
44 |
def authenticate(self) -> bool: |
|
0981a08…
|
noreply
|
45 |
"""Check if gws CLI is installed and authenticated.""" |
|
0981a08…
|
noreply
|
46 |
if not shutil.which("gws"): |
|
0981a08…
|
noreply
|
47 |
logger.error("gws CLI not found. Install with: npm install -g @googleworkspace/cli") |
|
0981a08…
|
noreply
|
48 |
return False |
|
0981a08…
|
noreply
|
49 |
try: |
|
0981a08…
|
noreply
|
50 |
_run_gws(["auth", "status"], timeout=10) |
|
0981a08…
|
noreply
|
51 |
return True |
|
0981a08…
|
noreply
|
52 |
except (RuntimeError, subprocess.TimeoutExpired): |
|
0981a08…
|
noreply
|
53 |
logger.error("gws not authenticated. Run: gws auth login") |
|
0981a08…
|
noreply
|
54 |
return False |
|
0981a08…
|
noreply
|
55 |
|
|
0981a08…
|
noreply
|
56 |
def list_videos( |
|
0981a08…
|
noreply
|
57 |
self, |
|
0981a08…
|
noreply
|
58 |
folder_id: Optional[str] = None, |
|
0981a08…
|
noreply
|
59 |
folder_path: Optional[str] = None, |
|
0981a08…
|
noreply
|
60 |
patterns: Optional[List[str]] = None, |
|
0981a08…
|
noreply
|
61 |
) -> List[SourceFile]: |
|
0981a08…
|
noreply
|
62 |
"""List Google Meet recordings in Drive. |
|
0981a08…
|
noreply
|
63 |
|
|
0981a08…
|
noreply
|
64 |
Searches for MP4 files with 'Meet Recording' in the name. If a |
|
0981a08…
|
noreply
|
65 |
drive_folder_id is set, restricts search to that folder. |
|
0981a08…
|
noreply
|
66 |
Also discovers companion transcript docs for each recording. |
|
0981a08…
|
noreply
|
67 |
""" |
|
0981a08…
|
noreply
|
68 |
target_folder = folder_id or self.drive_folder_id |
|
0981a08…
|
noreply
|
69 |
files: List[SourceFile] = [] |
|
0981a08…
|
noreply
|
70 |
|
|
0981a08…
|
noreply
|
71 |
# Build the Drive search query for Meet recordings |
|
0981a08…
|
noreply
|
72 |
q_parts = [ |
|
0981a08…
|
noreply
|
73 |
"mimeType='video/mp4'", |
|
0981a08…
|
noreply
|
74 |
"name contains 'Meet Recording'", |
|
0981a08…
|
noreply
|
75 |
"trashed=false", |
|
0981a08…
|
noreply
|
76 |
] |
|
0981a08…
|
noreply
|
77 |
if target_folder: |
|
0981a08…
|
noreply
|
78 |
q_parts.append(f"'{target_folder}' in parents") |
|
0981a08…
|
noreply
|
79 |
|
|
0981a08…
|
noreply
|
80 |
params = { |
|
0981a08…
|
noreply
|
81 |
"q": " and ".join(q_parts), |
|
0981a08…
|
noreply
|
82 |
"fields": "files(id,name,mimeType,size,modifiedTime)", |
|
0981a08…
|
noreply
|
83 |
"pageSize": 50, |
|
0981a08…
|
noreply
|
84 |
"orderBy": "modifiedTime desc", |
|
0981a08…
|
noreply
|
85 |
} |
|
0981a08…
|
noreply
|
86 |
|
|
0981a08…
|
noreply
|
87 |
try: |
|
0981a08…
|
noreply
|
88 |
result = _run_gws( |
|
0981a08…
|
noreply
|
89 |
[ |
|
0981a08…
|
noreply
|
90 |
"drive", |
|
0981a08…
|
noreply
|
91 |
"files", |
|
0981a08…
|
noreply
|
92 |
"list", |
|
0981a08…
|
noreply
|
93 |
"--params", |
|
0981a08…
|
noreply
|
94 |
json.dumps(params), |
|
0981a08…
|
noreply
|
95 |
], |
|
0981a08…
|
noreply
|
96 |
timeout=60, |
|
0981a08…
|
noreply
|
97 |
) |
|
0981a08…
|
noreply
|
98 |
except RuntimeError as e: |
|
0981a08…
|
noreply
|
99 |
logger.error(f"Failed to list Meet recordings: {e}") |
|
0981a08…
|
noreply
|
100 |
return [] |
|
0981a08…
|
noreply
|
101 |
|
|
0981a08…
|
noreply
|
102 |
recordings = result.get("files", []) |
|
0981a08…
|
noreply
|
103 |
for item in recordings: |
|
0981a08…
|
noreply
|
104 |
size = item.get("size") |
|
0981a08…
|
noreply
|
105 |
files.append( |
|
0981a08…
|
noreply
|
106 |
SourceFile( |
|
0981a08…
|
noreply
|
107 |
name=item.get("name", "Meet Recording"), |
|
0981a08…
|
noreply
|
108 |
id=item.get("id", ""), |
|
0981a08…
|
noreply
|
109 |
size_bytes=int(size) if size else None, |
|
0981a08…
|
noreply
|
110 |
mime_type=item.get("mimeType", "video/mp4"), |
|
0981a08…
|
noreply
|
111 |
modified_at=item.get("modifiedTime"), |
|
0981a08…
|
noreply
|
112 |
) |
|
0981a08…
|
noreply
|
113 |
) |
|
0981a08…
|
noreply
|
114 |
|
|
0981a08…
|
noreply
|
115 |
# Also search for auto-generated transcript docs |
|
0981a08…
|
noreply
|
116 |
transcript_params = { |
|
0981a08…
|
noreply
|
117 |
"q": " and ".join( |
|
0981a08…
|
noreply
|
118 |
[ |
|
0981a08…
|
noreply
|
119 |
"mimeType='application/vnd.google-apps.document'", |
|
0981a08…
|
noreply
|
120 |
"(name contains 'Transcript' or name contains 'Meeting notes')", |
|
0981a08…
|
noreply
|
121 |
"trashed=false", |
|
0981a08…
|
noreply
|
122 |
] |
|
0981a08…
|
noreply
|
123 |
+ ([f"'{target_folder}' in parents"] if target_folder else []) |
|
0981a08…
|
noreply
|
124 |
), |
|
0981a08…
|
noreply
|
125 |
"fields": "files(id,name,mimeType,modifiedTime)", |
|
0981a08…
|
noreply
|
126 |
"pageSize": 50, |
|
0981a08…
|
noreply
|
127 |
"orderBy": "modifiedTime desc", |
|
0981a08…
|
noreply
|
128 |
} |
|
0981a08…
|
noreply
|
129 |
|
|
0981a08…
|
noreply
|
130 |
try: |
|
0981a08…
|
noreply
|
131 |
transcript_result = _run_gws( |
|
0981a08…
|
noreply
|
132 |
[ |
|
0981a08…
|
noreply
|
133 |
"drive", |
|
0981a08…
|
noreply
|
134 |
"files", |
|
0981a08…
|
noreply
|
135 |
"list", |
|
0981a08…
|
noreply
|
136 |
"--params", |
|
0981a08…
|
noreply
|
137 |
json.dumps(transcript_params), |
|
0981a08…
|
noreply
|
138 |
], |
|
0981a08…
|
noreply
|
139 |
timeout=60, |
|
0981a08…
|
noreply
|
140 |
) |
|
0981a08…
|
noreply
|
141 |
transcript_files = transcript_result.get("files", []) |
|
0981a08…
|
noreply
|
142 |
logger.info( |
|
0981a08…
|
noreply
|
143 |
f"Found {len(recordings)} recording(s) and " |
|
0981a08…
|
noreply
|
144 |
f"{len(transcript_files)} transcript doc(s) in Drive" |
|
0981a08…
|
noreply
|
145 |
) |
|
0981a08…
|
noreply
|
146 |
except RuntimeError as e: |
|
0981a08…
|
noreply
|
147 |
logger.debug(f"Transcript search failed: {e}") |
|
0981a08…
|
noreply
|
148 |
|
|
0981a08…
|
noreply
|
149 |
if not files: |
|
0981a08…
|
noreply
|
150 |
logger.warning("No Google Meet recordings found in Drive") |
|
0981a08…
|
noreply
|
151 |
|
|
0981a08…
|
noreply
|
152 |
logger.info(f"Found {len(files)} Meet recording(s)") |
|
0981a08…
|
noreply
|
153 |
return files |
|
0981a08…
|
noreply
|
154 |
|
|
0981a08…
|
noreply
|
155 |
def download(self, file: SourceFile, destination: Path) -> Path: |
|
0981a08…
|
noreply
|
156 |
"""Download a Meet recording from Drive.""" |
|
0981a08…
|
noreply
|
157 |
destination = Path(destination) |
|
0981a08…
|
noreply
|
158 |
destination.parent.mkdir(parents=True, exist_ok=True) |
|
0981a08…
|
noreply
|
159 |
|
|
0981a08…
|
noreply
|
160 |
# For video files, download binary content via alt=media |
|
0981a08…
|
noreply
|
161 |
result = _run_gws( |
|
0981a08…
|
noreply
|
162 |
[ |
|
0981a08…
|
noreply
|
163 |
"drive", |
|
0981a08…
|
noreply
|
164 |
"files", |
|
0981a08…
|
noreply
|
165 |
"get", |
|
0981a08…
|
noreply
|
166 |
"--params", |
|
0981a08…
|
noreply
|
167 |
json.dumps({"fileId": file.id, "alt": "media"}), |
|
0981a08…
|
noreply
|
168 |
], |
|
0981a08…
|
noreply
|
169 |
timeout=300, |
|
0981a08…
|
noreply
|
170 |
) |
|
0981a08…
|
noreply
|
171 |
|
|
0981a08…
|
noreply
|
172 |
# Write the content — result may be raw binary or a dict wrapper |
|
0981a08…
|
noreply
|
173 |
raw = result.get("raw", "") if isinstance(result, dict) else str(result) |
|
0981a08…
|
noreply
|
174 |
destination.write_text(raw, encoding="utf-8") |
|
0981a08…
|
noreply
|
175 |
logger.info(f"Downloaded {file.name} to {destination}") |
|
0981a08…
|
noreply
|
176 |
return destination |
|
0981a08…
|
noreply
|
177 |
|
|
0981a08…
|
noreply
|
178 |
def fetch_transcript(self, recording_name: str) -> Optional[str]: |
|
0981a08…
|
noreply
|
179 |
"""Fetch the companion transcript for a Meet recording. |
|
0981a08…
|
noreply
|
180 |
|
|
0981a08…
|
noreply
|
181 |
Google Meet creates transcript docs with names that typically match |
|
0981a08…
|
noreply
|
182 |
the recording date/time. This method searches for the matching |
|
0981a08…
|
noreply
|
183 |
Google Doc and extracts its text content. |
|
0981a08…
|
noreply
|
184 |
""" |
|
0981a08…
|
noreply
|
185 |
transcript_id = self._find_matching_transcript(recording_name) |
|
0981a08…
|
noreply
|
186 |
if not transcript_id: |
|
0981a08…
|
noreply
|
187 |
logger.info(f"No matching transcript found for: {recording_name}") |
|
0981a08…
|
noreply
|
188 |
return None |
|
0981a08…
|
noreply
|
189 |
|
|
0981a08…
|
noreply
|
190 |
# Fetch the Google Doc content via the Docs API |
|
0981a08…
|
noreply
|
191 |
try: |
|
0981a08…
|
noreply
|
192 |
result = _run_gws( |
|
0981a08…
|
noreply
|
193 |
[ |
|
0981a08…
|
noreply
|
194 |
"docs", |
|
0981a08…
|
noreply
|
195 |
"documents", |
|
0981a08…
|
noreply
|
196 |
"get", |
|
0981a08…
|
noreply
|
197 |
"--params", |
|
0981a08…
|
noreply
|
198 |
json.dumps({"documentId": transcript_id}), |
|
0981a08…
|
noreply
|
199 |
], |
|
0981a08…
|
noreply
|
200 |
timeout=60, |
|
0981a08…
|
noreply
|
201 |
) |
|
0981a08…
|
noreply
|
202 |
except RuntimeError as e: |
|
0981a08…
|
noreply
|
203 |
logger.warning(f"Failed to fetch transcript doc {transcript_id}: {e}") |
|
0981a08…
|
noreply
|
204 |
return None |
|
0981a08…
|
noreply
|
205 |
|
|
0981a08…
|
noreply
|
206 |
# Extract text from the Docs API structural response |
|
0981a08…
|
noreply
|
207 |
body = result.get("body", {}) |
|
0981a08…
|
noreply
|
208 |
text_parts: list[str] = [] |
|
0981a08…
|
noreply
|
209 |
for element in body.get("content", []): |
|
0981a08…
|
noreply
|
210 |
paragraph = element.get("paragraph", {}) |
|
0981a08…
|
noreply
|
211 |
for pe in paragraph.get("elements", []): |
|
0981a08…
|
noreply
|
212 |
text_run = pe.get("textRun", {}) |
|
0981a08…
|
noreply
|
213 |
text = text_run.get("content", "") |
|
0981a08…
|
noreply
|
214 |
if text.strip(): |
|
0981a08…
|
noreply
|
215 |
text_parts.append(text) |
|
0981a08…
|
noreply
|
216 |
|
|
0981a08…
|
noreply
|
217 |
if not text_parts: |
|
0981a08…
|
noreply
|
218 |
logger.warning(f"Transcript doc {transcript_id} had no extractable text") |
|
0981a08…
|
noreply
|
219 |
return None |
|
0981a08…
|
noreply
|
220 |
|
|
0981a08…
|
noreply
|
221 |
return "".join(text_parts) |
|
0981a08…
|
noreply
|
222 |
|
|
0981a08…
|
noreply
|
223 |
def _find_matching_transcript(self, recording_name: str) -> Optional[str]: |
|
0981a08…
|
noreply
|
224 |
"""Search Drive for a transcript doc that matches a recording name. |
|
0981a08…
|
noreply
|
225 |
|
|
0981a08…
|
noreply
|
226 |
Meet recordings are typically named like: |
|
0981a08…
|
noreply
|
227 |
"Meet Recording 2026-03-07T14:30:00" |
|
0981a08…
|
noreply
|
228 |
And transcripts are named like: |
|
0981a08…
|
noreply
|
229 |
"Meeting Transcript 2026-03-07" or "2026-03-07 - Transcript" |
|
0981a08…
|
noreply
|
230 |
|
|
0981a08…
|
noreply
|
231 |
This extracts the date portion and searches for matching transcript docs. |
|
0981a08…
|
noreply
|
232 |
""" |
|
0981a08…
|
noreply
|
233 |
# Extract a date string from the recording name (YYYY-MM-DD pattern) |
|
0981a08…
|
noreply
|
234 |
date_match = re.search(r"\d{4}-\d{2}-\d{2}", recording_name) |
|
0981a08…
|
noreply
|
235 |
date_str = date_match.group(0) if date_match else recording_name |
|
0981a08…
|
noreply
|
236 |
|
|
0981a08…
|
noreply
|
237 |
# Search for transcript docs matching the date |
|
0981a08…
|
noreply
|
238 |
search_query = " and ".join( |
|
0981a08…
|
noreply
|
239 |
[ |
|
0981a08…
|
noreply
|
240 |
"mimeType='application/vnd.google-apps.document'", |
|
0981a08…
|
noreply
|
241 |
f"name contains '{date_str}'", |
|
0981a08…
|
noreply
|
242 |
"(name contains 'Transcript' or name contains 'transcript' " |
|
0981a08…
|
noreply
|
243 |
"or name contains 'Meeting notes')", |
|
0981a08…
|
noreply
|
244 |
"trashed=false", |
|
0981a08…
|
noreply
|
245 |
] |
|
0981a08…
|
noreply
|
246 |
) |
|
0981a08…
|
noreply
|
247 |
if self.drive_folder_id: |
|
0981a08…
|
noreply
|
248 |
search_query += f" and '{self.drive_folder_id}' in parents" |
|
0981a08…
|
noreply
|
249 |
|
|
0981a08…
|
noreply
|
250 |
try: |
|
0981a08…
|
noreply
|
251 |
result = _run_gws( |
|
0981a08…
|
noreply
|
252 |
[ |
|
0981a08…
|
noreply
|
253 |
"drive", |
|
0981a08…
|
noreply
|
254 |
"files", |
|
0981a08…
|
noreply
|
255 |
"list", |
|
0981a08…
|
noreply
|
256 |
"--params", |
|
0981a08…
|
noreply
|
257 |
json.dumps( |
|
0981a08…
|
noreply
|
258 |
{ |
|
0981a08…
|
noreply
|
259 |
"q": search_query, |
|
0981a08…
|
noreply
|
260 |
"fields": "files(id,name,modifiedTime)", |
|
0981a08…
|
noreply
|
261 |
"pageSize": 5, |
|
0981a08…
|
noreply
|
262 |
"orderBy": "modifiedTime desc", |
|
0981a08…
|
noreply
|
263 |
} |
|
0981a08…
|
noreply
|
264 |
), |
|
0981a08…
|
noreply
|
265 |
], |
|
0981a08…
|
noreply
|
266 |
timeout=60, |
|
0981a08…
|
noreply
|
267 |
) |
|
0981a08…
|
noreply
|
268 |
except RuntimeError as e: |
|
0981a08…
|
noreply
|
269 |
logger.debug(f"Transcript search failed for '{date_str}': {e}") |
|
0981a08…
|
noreply
|
270 |
return None |
|
0981a08…
|
noreply
|
271 |
|
|
0981a08…
|
noreply
|
272 |
files = result.get("files", []) |
|
0981a08…
|
noreply
|
273 |
if not files: |
|
0981a08…
|
noreply
|
274 |
logger.debug(f"No transcript docs found matching '{date_str}'") |
|
0981a08…
|
noreply
|
275 |
return None |
|
0981a08…
|
noreply
|
276 |
|
|
0981a08…
|
noreply
|
277 |
# Return the most recently modified match |
|
0981a08…
|
noreply
|
278 |
best = files[0] |
|
0981a08…
|
noreply
|
279 |
logger.info(f"Matched transcript: {best.get('name')} for recording: {recording_name}") |
|
0981a08…
|
noreply
|
280 |
return best.get("id") |