import dropbox from dropbox import DropboxOAuth2FlowNoRedirect import os import re from normalize import normalize_name def get_dropbox_client(refresh_token, app_key, app_secret): """ Create Dropbox client from refresh token """ dbx = dropbox.Dropbox( oauth2_refresh_token=refresh_token, app_key=app_key, app_secret=app_secret ) return dbx def get_srt_files(refresh_token, app_key, app_secret, course_path): """ Get all SRT files from team folder course course_path examples: - "C150 Echocardiography in Heart Failure (Cristiana Monteiro)" - "Archive/C141 Pediatric ECG (Peter Aziz)" Always looks in: Courses/{course_path}/Transcripts/ """ dbx = get_dropbox_client(refresh_token, app_key, app_secret) # First, get the "Courses" team folder ID try: team_folders = dbx.sharing_list_folders().entries courses_folder = None for folder in team_folders: if folder.name == "Courses": courses_folder = folder break if not courses_folder: print("Error: 'Courses' team folder not found") return [] print(f"DEBUG: Found 'Courses' team folder (ID: {courses_folder.shared_folder_id})") # Create a new client with the team folder as root dbx_team = dbx.with_path_root( dropbox.common.PathRoot.namespace_id(courses_folder.shared_folder_id) ) # Now access the path relative to the Courses folder transcripts_path = f"/{course_path}/Transcripts" print(f"DEBUG: Looking at: {transcripts_path}") srt_files = [] result = dbx_team.files_list_folder(transcripts_path) # Process entries and handle pagination while True: for entry in result.entries: if isinstance(entry, dropbox.files.FileMetadata) and entry.name.endswith('.srt'): srt_files.append({ 'filename': entry.name, 'path': entry.path_display, 'normalized_name': normalize_name(entry.name, is_filename=True) }) # Check if there are more results if not result.has_more: break result = dbx_team.files_list_folder_continue(result.cursor) print(f"DEBUG: Found {len(srt_files)} SRT files") return srt_files except dropbox.exceptions.ApiError as e: print(f"Error accessing Dropbox: {e}") return [] def get_srt_files_translated(refresh_token, app_key, app_secret, course_name, language): """ Get all SRT files from user folder for translated courses (Spanish/German) Structure: Course Translations/{course_name}/{language}/{Intros|Lessons|Quizzes}/{folder_name}/random.srt course_name examples: - "C150 Echocardiography" (without teacher name) - "C141 Pediatric ECG" language: "Spanish" or "German" Returns: tuple of (srt_files list, debug_messages list) """ dbx = get_dropbox_client(refresh_token, app_key, app_secret) try: # Build path: /Course Translations/{course_name}/{language}/ base_path = f"/Course Translations/{course_name}/{language}" print(f"DEBUG: Looking at: {base_path}") srt_files = [] debug_messages = [] debug_messages.append(f"📁 Scanning: {base_path}") # Iterate through type folders (Intros, Lessons, Quizzes) for type_folder in ['Intros', 'Lessons', 'Quizzes']: type_path = f"{base_path}/{type_folder}" try: # List all folders in this type folder with pagination result = dbx.files_list_folder(type_path) while True: for entry in result.entries: # Each entry should be a folder named after the video (e.g., $_to_ftusmini_Le_1_1) if isinstance(entry, dropbox.files.FolderMetadata): folder_name = entry.name folder_path = entry.path_display # List files inside this folder try: folder_contents = dbx.files_list_folder(folder_path) for file_entry in folder_contents.entries: if isinstance(file_entry, dropbox.files.FileMetadata) and file_entry.name.endswith('.srt'): # Use the folder name for matching (not the random SRT filename) srt_files.append({ 'filename': folder_name, # Use folder name for display 'path': file_entry.path_display, # Actual SRT file path 'normalized_name': normalize_name(folder_name, is_filename=True), 'is_translated': True # Flag for download logic }) break # Only take the first .srt file in each folder except dropbox.exceptions.ApiError as e: print(f"Warning: Could not access folder {folder_path}: {e}") continue # Check if there are more results if not result.has_more: break result = dbx.files_list_folder_continue(result.cursor) except dropbox.exceptions.ApiError as e: # Type folder might not exist (e.g., no Intros), skip it msg = f"â„šī¸ {type_folder} folder not found or empty" print(f"Note: {type_folder} folder not found or empty in {base_path}") debug_messages.append(msg) continue print(f"DEBUG: Found {len(srt_files)} SRT files across all type folders") debug_messages.append(f"✓ Found {len(srt_files)} SRT files total") return srt_files, debug_messages except dropbox.exceptions.ApiError as e: error_msg = f"❌ Error accessing Dropbox: {e}" print(error_msg) return [], [error_msg] def download_srt_content(refresh_token, app_key, app_secret, file_path, is_translated=False): """ Download SRT file content from Dropbox Args: file_path: Path to the SRT file is_translated: If True, file is in user folder (Spanish/German). If False, file is in "Courses" team folder (English) Returns: string content of the SRT file """ dbx = get_dropbox_client(refresh_token, app_key, app_secret) try: if is_translated: # For translated files (Spanish/German), use regular user folder access metadata, response = dbx.files_download(file_path) else: # For English files, use team folder namespace # Get the "Courses" team folder ID and set path_root team_folders = dbx.sharing_list_folders().entries courses_folder = None for folder in team_folders: if folder.name == "Courses": courses_folder = folder break if not courses_folder: print("Error: 'Courses' team folder not found") return None # Create a new client with the team folder as root dbx_team = dbx.with_path_root( dropbox.common.PathRoot.namespace_id(courses_folder.shared_folder_id) ) # Download using the team folder client metadata, response = dbx_team.files_download(file_path) return response.content.decode('utf-8') except dropbox.exceptions.ApiError as e: print(f"Error downloading file: {e}") return None