forked from alexeygrigorev/loom-transcript-scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.py
More file actions
688 lines (591 loc) · 35.3 KB
/
process.py
File metadata and controls
688 lines (591 loc) · 35.3 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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException, WebDriverException, NoSuchElementException, StaleElementReferenceException
from webdriver_manager.chrome import ChromeDriverManager
import time
import re
import string
import os
import tempfile
import json
import argparse
# Parse command-line arguments
parser = argparse.ArgumentParser(description='Extract transcripts from Loom videos.')
parser.add_argument('--input-file', type=str, default='loom-videos.txt',
help='Path to the file containing Loom video URLs (default: loom-videos.txt)')
parser.add_argument('--force', action='store_true',
help='Force processing of videos even if they were previously processed')
parser.add_argument('--preserve', action='store_true',
help='Preserve the input file after processing (do not clear it)')
parser.add_argument('--process-llm', action='store_true',
help='Process transcripts for LLM after downloading')
parser.add_argument('--llm-dir', type=str, default="llm_ready_transcripts",
help='Directory to store LLM-ready transcripts (default: llm_ready_transcripts')
args = parser.parse_args()
# File paths
input_file = args.input_file
processed_file = "loom-videos-processed.txt"
download_dir = "/Users/mss/Desktop/BuildrWealth/Loom Transcripts"
# Initialize set of processed videos
processed_videos = set()
# Check if processed file exists and load already processed videos
if os.path.exists(processed_file):
with open(processed_file, "r") as f:
processed_videos = set(line.strip() for line in f if line.strip())
print(f"Loaded {len(processed_videos)} previously processed videos from {processed_file}")
# Ensure download directory exists
if not os.path.exists(download_dir):
os.makedirs(download_dir)
print(f"Created download directory: {download_dir}")
else:
pass # No action needed
# Ensure LLM directory exists if processing for LLM
if args.process_llm:
llm_dir = args.llm_dir
if not os.path.exists(llm_dir):
os.makedirs(llm_dir)
print(f"Created directory for LLM-ready transcripts: {llm_dir}")
else:
print(f"Using existing directory for LLM-ready transcripts: {llm_dir}")
print(f"Using existing download directory: {download_dir}")
# Create a temporary directory for the Chrome user data
temp_dir = tempfile.mkdtemp()
print(f"Using temporary directory for Chrome profile: {temp_dir}")
# Set up Chrome options
chrome_options = Options()
chrome_options.add_argument(f"user-data-dir={temp_dir}")
chrome_options.add_argument("--no-first-run")
chrome_options.add_argument("--no-default-browser-check")
chrome_options.add_argument("--start-maximized")
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.binary_location = "/Applications/Brave Browser.app/Contents/MacOS/Brave Browser"
# Set download directory preference
chrome_options.add_experimental_option("prefs", {
"download.default_directory": download_dir,
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"safebrowsing.enabled": True
})
# Set up ChromeDriver service
service = Service(ChromeDriverManager().install())
# Initialize the WebDriver
driver = None
# LLM transcript processing functions
def clean_transcript(text):
'''
Process transcript text to make it LLM-friendly.
Args:
text (str): Raw transcript text
Returns:
str: Cleaned and formatted transcript text
'''
# Step 1: Preserve timestamps by standardizing their format to [HH:MM:SS]
# Step 1: Preserve timestamps by standardizing their format to [HH:MM:SS]
# This regex matches common timestamp formats and standardizes them
text = re.sub(r'(\[?\(?\s*)(\d{1,2}:\d{2}(?:\d{2})?)\s*(?:\]|\))?', r'[]', text)
# Step 2: Normalize line breaks and ensure speaker names are properly formatted
# Step 2: Normalize line breaks and ensure speaker names are properly formatted
# This helps maintain the conversation structure
text = re.sub(r'\n{3,}', '\n\n', text) # Replace excessive newlines with double newlines
# Step 3: Fix common punctuation issues
# Step 3: Fix common punctuation issues
# Remove duplicate punctuation and ensure proper spacing
text = re.sub(r'([.!?])\s*([.!?])+', r'', text) # Remove duplicate punctuation
text = re.sub(r'\s+([.,;:!?])', r'', text) # Remove space before punctuation
text = re.sub(r'([.,;:!?])([^\s\d])', r' ', text) # Add space after punctuation if missing
# Step 4: Normalize whitespace
# Remove trailing/leading whitespace from each line and collapse multiple spaces
lines = [line.strip() for line in text.split('\n')]
text = '\n'.join(lines)
text = re.sub(r' +', ' ', text) # Replace multiple spaces with a single space
# Step 5: Remove empty lines while preserving paragraph structure
lines = text.split('\n')
non_empty_lines = []
for i, line in enumerate(lines):
# Keep the line if it's not empty or if it's a deliberate paragraph break
if line.strip() or (i > 0 and i < len(lines) - 1 and lines[i-1].strip() and lines[i+1].strip()):
non_empty_lines.append(line)
# Step 6: Final cleanup - remove any remaining problematic characters
# (but carefully preserve important special characters)
text = '\n'.join(non_empty_lines)
# Filter out any non-printable characters except for common line breaks
printable_chars = set(string.printable)
text = ''.join(c for c in text if c in printable_chars)
return text
def process_for_llm(transcript_filepath, llm_dir):
'''Process a transcript file for LLM and save to the LLM directory.
Args:
transcript_filepath (str): Path to the transcript file
llm_dir (str): Directory to save LLM-ready transcript
Returns:
bool: True if successful, False otherwise
'''
try:
# Get the base filename
base_name = os.path.basename(transcript_filepath)
name_without_ext = os.path.splitext(base_name)[0]
llm_filepath = os.path.join(llm_dir, f"{name_without_ext}_llm.txt")
# Skip if already processed
if os.path.exists(llm_filepath):
print(f"LLM version already exists: {llm_filepath}")
return False
# Read the transcript
with open(transcript_filepath, "r", encoding="utf-8") as f:
transcript_text = f.read()
# Clean and format for LLM
processed_text = clean_transcript(transcript_text)
# Save to LLM directory
with open(llm_filepath, "w", encoding="utf-8") as f:
f.write(processed_text)
print(f"Created LLM-ready transcript: {llm_filepath}")
return True
except Exception as e:
print(f"Error processing transcript for LLM: {str(e)}")
return False
try:
driver = webdriver.Chrome(service=service, options=chrome_options)
driver.set_page_load_timeout(30)
# Navigate to Loom login page
print("Navigating to Loom login page...")
driver.get("https://www.loom.com/login")
time.sleep(5)
print("Please log in to your Loom account manually in the opened browser.")
input("Press Enter when you have successfully logged in...")
# Now proceed with the Loom video processing
video_ids = [line.strip() for line in open(input_file, "r") if line.strip()]
for video_id in video_ids:
try:
# Check if video has already been processed
if not args.force and video_id in processed_videos:
print(f"Skipping {video_id} - already processed (use --force to process anyway)")
continue
# Check if a transcript file for this video already exists
clean_video_id = video_id.replace("https://www.loom.com/share/", "").replace("/", "_")
existing_files = [f for f in os.listdir(download_dir) if clean_video_id in f and f.endswith(".txt")]
if not args.force and existing_files:
print(f"Skipping {video_id} - transcript file already exists: {existing_files[0]} (use --force to process anyway)")
# Add to processed list if not already there
if video_id not in processed_videos:
with open(processed_file, "a") as f:
f.write(f"{video_id}\n")
processed_videos.add(video_id)
continue
elif args.force and existing_files:
print(f"Force processing {video_id} even though transcript file already exists: {existing_files[0]}")
# Check if the video_id already contains the full URL
if "https://www.loom.com/share/" in video_id:
url = video_id
else:
url = f"https://www.loom.com/share/{video_id}"
print(f"\nOpening URL: {url}")
driver.get(url)
print("Waiting for page to load...")
time.sleep(10) # Increased wait time to 10 seconds
print("Current page title:", driver.title)
# Taking screenshot for debugging
screenshot_dir = "debug_screenshots"
os.makedirs(screenshot_dir, exist_ok=True)
screenshot_path = os.path.join(screenshot_dir, f"loom_{video_id.replace('/', '_')}.png")
driver.save_screenshot(screenshot_path)
print(f"Screenshot saved to {screenshot_path}")
# Enhanced Debug Function
def comprehensive_debug():
debug_dir = "debug_output"
os.makedirs(debug_dir, exist_ok=True)
# 1. Save page source to file
page_source_path = os.path.join(debug_dir, f"page_source_{video_id.replace('/', '_')}.html")
with open(page_source_path, "w", encoding="utf-8") as f:
f.write(driver.page_source)
print(f"Page source saved to {page_source_path}")
# 2. Check for iframes
iframes = driver.find_elements(By.TAG_NAME, "iframe")
print(f"Found {len(iframes)} iframes on the page")
for i, iframe in enumerate(iframes):
iframe_id = iframe.get_attribute("id") or "no-id"
iframe_src = iframe.get_attribute("src") or "no-src"
print(f" Iframe {i+1}: ID={iframe_id}, Src={iframe_src}")
# 3. List all buttons
buttons = driver.find_elements(By.TAG_NAME, "button")
print(f"Found {len(buttons)} buttons on the page")
buttons_info = []
for i, button in enumerate(buttons):
try:
button_text = button.text
button_class = button.get_attribute("class") or "no-class"
button_id = button.get_attribute("id") or "no-id"
is_displayed = button.is_displayed()
is_enabled = button.is_enabled()
buttons_info.append({
"index": i+1,
"text": button_text,
"class": button_class,
"id": button_id,
"is_displayed": is_displayed,
"is_enabled": is_enabled
})
print(f" Button {i+1}: '{button_text[:30]}{'...' if len(button_text) > 30 else ''}', " +
f"Class={button_class}, ID={button_id}, " +
f"Displayed={is_displayed}, Enabled={is_enabled}")
except Exception as e:
print(f" Button {i+1}: Error getting details: {str(e)}")
# Save buttons info to file
buttons_info_path = os.path.join(debug_dir, f"buttons_info_{video_id.replace('/', '_')}.json")
with open(buttons_info_path, "w", encoding="utf-8") as f:
json.dump(buttons_info, f, indent=2)
print(f"Buttons info saved to {buttons_info_path}")
# 4. Check for shadow DOM elements
print("Checking for shadow DOM elements...")
shadow_hosts = driver.execute_script("""
return Array.from(document.querySelectorAll('*')).filter(
el => el.shadowRoot !== null
).map(el => {
return {
tag: el.tagName.toLowerCase(),
id: el.id || 'no-id',
class: el.className || 'no-class'
};
});
""")
print(f"Found {len(shadow_hosts)} shadow DOM hosts on the page")
for i, host in enumerate(shadow_hosts):
print(f" Shadow Host {i+1}: <{host['tag']}> ID={host['id']}, Class={host['class']}")
# Save shadow host info to file
shadow_info_path = os.path.join(debug_dir, f"shadow_dom_info_{video_id.replace('/', '_')}.json")
with open(shadow_info_path, "w", encoding="utf-8") as f:
json.dump(shadow_hosts, f, indent=2)
print(f"Shadow DOM info saved to {shadow_info_path}")
# Run the comprehensive debug
comprehensive_debug()
# Check for and switch to iframes
print("\nChecking for iframes that might contain the transcript...")
iframes = driver.find_elements(By.TAG_NAME, "iframe")
main_window = driver.current_window_handle
transcript_found_in_iframe = False
for i, iframe in enumerate(iframes):
try:
iframe_id = iframe.get_attribute("id") or "no-id"
print(f"Switching to iframe {i+1} (ID: {iframe_id})...")
driver.switch_to.frame(iframe)
# Take screenshot of iframe content
iframe_screenshot_path = os.path.join(screenshot_dir, f"iframe_{i+1}_{video_id.replace('/', '_')}.png")
driver.save_screenshot(iframe_screenshot_path)
print(f"Iframe screenshot saved to {iframe_screenshot_path}")
# Check if transcript elements exist in this iframe
transcript_elements = driver.find_elements(By.XPATH, "//*[contains(text(), 'Transcript')]")
download_elements = driver.find_elements(By.XPATH, "//*[contains(text(), 'Download')]")
if transcript_elements or download_elements:
print(f"Found potential transcript elements in iframe {i+1}!")
transcript_found_in_iframe = True
break
else:
print(f"No transcript elements found in iframe {i+1}")
driver.switch_to.default_content()
except Exception as e:
print(f"Error switching to iframe {i+1}: {str(e)}")
driver.switch_to.default_content()
if not transcript_found_in_iframe:
driver.switch_to.default_content()
print("Switched back to main content (no transcript found in iframes)")
# Debug: Find and print information about elements containing keywords
print("\n--- DEBUG: Searching for relevant elements ---")
for keyword in ["Transcript", "Activity", "Download"]:
elements = driver.find_elements(By.XPATH, f"//*[contains(text(), '{keyword}')]")
print(f"\nFound {len(elements)} elements containing '{keyword}':")
for i, element in enumerate(elements):
tag_name = element.tag_name
try:
element_text = element.text
element_class = element.get_attribute("class") or "no-class"
element_id = element.get_attribute("id") or "no-id"
is_displayed = element.is_displayed()
is_enabled = element.is_enabled()
# Get the XPath of the element
xpath = driver.execute_script("""
function getPathTo(element) {
if (element.id !== '')
return '//*[@id="' + element.id + '"]';
if (element === document.body)
return '/html/body';
var ix = 0;
var siblings = element.parentNode.childNodes;
for (var i = 0; i < siblings.length; i++) {
var sibling = siblings[i];
if (sibling === element)
return getPathTo(element.parentNode) + '/' + element.tagName.toLowerCase() + '[' + (ix + 1) + ']';
if (sibling.nodeType === 1 && sibling.tagName === element.tagName)
ix++;
}
}
return getPathTo(arguments[0]);
""", element)
print(f" {i+1}. <{tag_name}> - Text: '{element_text[:30]}{'...' if len(element_text) > 30 else ''}' - Class: {element_class}")
print(f" ID: {element_id}, Displayed: {is_displayed}, Enabled: {is_enabled}")
print(f" XPath: {xpath}")
# Print parent element info to understand context
try:
parent = driver.execute_script("return arguments[0].parentNode;", element)
parent_tag = driver.execute_script("return arguments[0].tagName;", parent).lower()
parent_class = driver.execute_script("return arguments[0].className;", parent) or "no-class"
print(f" Parent: <{parent_tag}> - Class: {parent_class}")
except:
print(" Could not get parent info")
except Exception as e:
print(f" {i+1}. <{tag_name}> - Error getting details: {str(e)}")
print("--- END DEBUG ---\n")
print("Looking for 'Transcript' section...")
# Wait for the page to fully load to ensure the Transcript section is visible
time.sleep(3)
# First, try to find and click on the Transcript tab if it's not already active
try:
# Try to find using specific class name or text content
transcript_tab = None
try:
# First attempt: Using the specific class name
transcript_tab = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, "button.css-1qz66q8"))
)
print("Found Transcript tab by class name. Clicking...")
except:
# Second attempt: Using text content
try:
transcript_tab = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//button[text()='Transcript']"))
)
print("Found Transcript tab by exact text. Clicking...")
except:
# Third attempt: Using contains text
transcript_tab = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Transcript')]"))
)
print("Found Transcript tab by partial text. Clicking...")
# Click the transcript tab
transcript_tab.click()
print("Clicked on Transcript tab. Waiting for content to load...")
# Wait longer after clicking the tab
time.sleep(5)
# Take a screenshot after clicking the Transcript tab
transcript_screenshot_path = os.path.join(screenshot_dir, f"transcript_tab_{video_id.replace('/', '_')}.png")
driver.save_screenshot(transcript_screenshot_path)
print(f"Screenshot after clicking Transcript tab saved to {transcript_screenshot_path}")
# Debug: Print elements that appear after switching to Transcript tab
print("\n--- DEBUG: Elements after switching to Transcript tab ---")
for keyword in ["Download", "Toggle", "Transcript", "Copy", "Action"]:
elements = driver.find_elements(By.XPATH, f"//*[contains(text(), '{keyword}')]")
print(f"\nFound {len(elements)} elements containing '{keyword}' after tab switch:")
for i, element in enumerate(elements):
tag_name = element.tag_name
try:
element_text = element.text
element_class = element.get_attribute("class") or "no-class"
element_id = element.get_attribute("id") or "no-id"
is_displayed = element.is_displayed()
print(f" {i+1}. <{tag_name}> - Text: '{element_text[:30]}{'...' if len(element_text) > 30 else ''}' - Class: {element_class}")
print(f" ID: {element_id}, Displayed: {is_displayed}")
except Exception as e:
print(f" {i+1}. <{tag_name}> - Error getting details: {str(e)}")
print("--- END DEBUG ---\n")
except Exception as e:
print(f"Transcript tab not found or already active: {str(e)}. Proceeding...")
print("Extracting transcript text from the page...")
# Take a screenshot before extraction for debugging
pre_extract_screenshot_path = os.path.join(screenshot_dir, f"before_extract_{video_id.replace('/', '_')}.png")
driver.save_screenshot(pre_extract_screenshot_path)
print(f"Screenshot before extraction saved to {pre_extract_screenshot_path}")
# Try multiple methods to find and extract the transcript text
transcript_text = ""
extraction_successful = False
# Method 1: Look for elements with transcript content
print("Method 1: Looking for transcript container elements...")
try:
# Try to find transcript container with multiple possible selectors
transcript_containers = driver.find_elements(By.XPATH,
"//div[contains(@class, 'transcript') or contains(@class, 'captions')]//div | " +
"//div[starts-with(@id, 'transcript-') or starts-with(@id, 'captions-')] | " +
"//div[@role='tabpanel' and .//div[contains(text(), 'Transcript')]]//div")
if transcript_containers:
print(f"Found {len(transcript_containers)} potential transcript container elements")
# Try to extract text from each container
for i, container in enumerate(transcript_containers):
try:
container_text = container.text.strip()
# Check if this looks like transcript content (contains timestamps or multiple lines)
if container_text and len(container_text) > 50 and ('\n' in container_text or ':' in container_text):
print(f"Container {i+1} appears to have transcript content ({len(container_text)} chars)")
if len(container_text) > len(transcript_text):
transcript_text = container_text
extraction_successful = True
elif container_text:
print(f"Container {i+1} text is too short or doesn't look like transcript content: {container_text[:30]}...")
except Exception as e:
print(f"Error extracting text from container {i+1}: {str(e)}")
except Exception as e:
print(f"Method 1 failed: {str(e)}")
# Method 2: Look for elements containing timestamps (which are common in transcripts)
if not extraction_successful or not transcript_text:
print("Method 2: Looking for elements containing timestamps...")
try:
# Look for elements that might contain timestamps (HH:MM:SS or MM:SS format)
timestamp_elements = driver.find_elements(By.XPATH,
"//div[contains(text(), ':') and string-length(normalize-space(text())) <= 8]/..")
if timestamp_elements:
print(f"Found {len(timestamp_elements)} potential timestamp parent elements")
# Find the parent container that might contain all transcripts
for i, element in enumerate(timestamp_elements):
try:
# Go up to a container that might hold multiple timestamps
parent = element.find_element(By.XPATH, "./..")
parent_text = parent.text.strip()
if parent_text and len(parent_text) > 100 and '\n' in parent_text:
print(f"Parent element {i+1} appears to have transcript content ({len(parent_text)} chars)")
if len(parent_text) > len(transcript_text):
transcript_text = parent_text
extraction_successful = True
except Exception as e:
print(f"Error processing timestamp parent {i+1}: {str(e)}")
except Exception as e:
print(f"Method 2 failed: {str(e)}")
# Method 3: Try to find all text paragraphs that might be transcript lines
if not extraction_successful or not transcript_text:
print("Method 3: Looking for paragraphs of text...")
try:
# Find elements that might be paragraphs of transcript text
paragraph_elements = driver.find_elements(By.CSS_SELECTOR,
"div.transcript p, div.transcript div, div[role='tabpanel'] p, div[data-testid*='transcript'] div")
if paragraph_elements:
print(f"Found {len(paragraph_elements)} potential paragraph elements")
# Combine all paragraph texts
all_paragraphs = []
for i, element in enumerate(paragraph_elements):
try:
paragraph_text = element.text.strip()
if paragraph_text:
all_paragraphs.append(paragraph_text)
except Exception as e:
print(f"Error processing paragraph {i+1}: {str(e)}")
if all_paragraphs:
combined_text = "\n\n".join(all_paragraphs)
if len(combined_text) > len(transcript_text):
transcript_text = combined_text
extraction_successful = True
print(f"Extracted {len(all_paragraphs)} paragraphs of text ({len(transcript_text)} chars)")
except Exception as e:
print(f"Method 3 failed: {str(e)}")
# Method 4: Last resort - look for any text content within the transcript section
if not extraction_successful or not transcript_text:
print("Method 4: Looking for any text in the transcript section...")
try:
# Try to find the transcript section and get all text
transcript_section = driver.find_element(By.XPATH,
"//div[@role='tabpanel' and .//div[contains(text(), 'Transcript')]] | " +
"//section[contains(@class, 'transcript')] | " +
"//div[contains(@class, 'transcript-container')]")
if transcript_section:
transcript_text = transcript_section.text.strip()
if transcript_text and len(transcript_text) > 50:
extraction_successful = True
print(f"Found transcript section with {len(transcript_text)} chars of text")
except Exception as e:
print(f"Method 4 failed: {str(e)}")
# Save the transcript to a file if extraction was successful
if extraction_successful and transcript_text:
print(f"Successfully extracted transcript text ({len(transcript_text)} characters)")
# Clean up the video_id to use as filename
clean_video_id = video_id.replace("https://www.loom.com/share/", "").replace("/", "_")
# Function to sanitize file names
def sanitize_filename(filename):
# Replace characters that are problematic in file paths
chars_to_replace = {
'/': '-',
'\\': '-',
':': '-',
'*': '',
'?': '',
'"': "'",
'<': '(',
'>': ')',
'|': '-'
}
for char, replacement in chars_to_replace.items():
filename = filename.replace(char, replacement)
return filename
# Get the video title from the page if possible
try:
video_title = driver.title.replace(" - Loom", "").strip()
if not video_title:
video_title = clean_video_id
except:
video_title = clean_video_id
# Sanitize the video title for use in the filename
video_title = sanitize_filename(video_title)
# Create the output filename
transcript_filename = f"{clean_video_id}.txt"
if video_title and video_title != clean_video_id:
# Make sure the filename is sanitized again as a final check
safe_title = sanitize_filename(video_title)
transcript_filename = f"{safe_title} - {clean_video_id}.txt"
# Further sanitize the complete filename as an extra precaution
transcript_filename = sanitize_filename(transcript_filename)
# Save to the download directory
transcript_filepath = os.path.join(download_dir, transcript_filename)
try:
with open(transcript_filepath, "w", encoding="utf-8") as f:
f.write(transcript_text)
print(f"Transcript saved to: {transcript_filepath}")
# Process for LLM if requested
except Exception as e:
print(f"Error saving transcript: {e}")
if args.process_llm:
process_for_llm(transcript_filepath, args.llm_dir)
# Take a screenshot after saving for debugging
after_save_screenshot_path = os.path.join(screenshot_dir, f"after_save_{video_id.replace('/', '_')}.png")
driver.save_screenshot(after_save_screenshot_path)
print(f"Screenshot after saving transcript saved to {after_save_screenshot_path}")
else:
print("Failed to extract transcript text from the page")
# Take a failure screenshot for debugging
failure_screenshot_path = os.path.join(screenshot_dir, f"extraction_failed_{video_id.replace('/', '_')}.png")
driver.save_screenshot(failure_screenshot_path)
print(f"Failure screenshot saved to {failure_screenshot_path}")
print(f"Processed video: {video_id}")
with open(processed_file, "a") as f:
f.write(f"{video_id}\n")
# No longer removing videos one by one - will clear all at once after processing
processed_videos.add(video_id)
except TimeoutException:
print(f"Timeout occurred while processing video {video_id}")
except Exception as e:
print(f"Error processing video {video_id}: {str(e)}")
print("Waiting before next video...")
time.sleep(5)
except Exception as e:
print(f"An error occurred: {str(e)}")
finally:
if driver:
input("Press Enter to close the browser...")
try:
driver.quit()
except WebDriverException:
print("WebDriver was already closed.")
except Exception as e:
print(f"Error while closing the browser: {str(e)}")
# Clean up the temporary directory
import shutil
shutil.rmtree(temp_dir, ignore_errors=True)
print(f"Removed temporary Chrome profile directory: {temp_dir}")
# Clear loom-videos.txt after all videos have been processed (unless --preserve is specified)
if os.path.exists(input_file):
if args.preserve:
print(f"Input file '{input_file}' has been preserved. All successfully processed videos are recorded in '{processed_file}'.")
else:
with open(input_file, "w") as f:
f.write("") # Clear the file
print(f"Input file '{input_file}' has been cleared. All successfully processed videos are recorded in '{processed_file}'.")