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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
#!/usr/bin/env python3
"""
Improved YouTube Transcript Downloader
Prioritizes transcript downloading over video downloading using multiple methods:
1. YouTube API captions (most reliable)
2. YouTube Transcript API (fallback)
3. yt-dlp transcript extraction (fallback)
4. Whisper transcription (last resort)
"""

import os
import json
import time
import requests
from datetime import datetime
from urllib.parse import urlparse, parse_qs
from typing import Dict, List, Optional, Tuple
import tempfile
import subprocess
from pathlib import Path

# Third-party imports
import yt_dlp
from dotenv import load_dotenv
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from youtube_transcript_api import YouTubeTranscriptApi
from youtube_transcript_api._errors import TranscriptsDisabled, NoTranscriptFound

# Load environment variables
load_dotenv()

# Configuration
YOUTUBE_API_KEY = os.getenv('YOUTUBE_API_KEY')
PROGRESS_FILE = "transcript_progress.json"
TRANSCRIPT_DIR = "transcripts"
VIDEOS_JSON = "outlier_trading_videos.json"
BATCH_SIZE = 50  # YouTube API batch size
MAX_RETRIES = 3

# Ensure directories exist
os.makedirs(TRANSCRIPT_DIR, exist_ok=True)

class TranscriptDownloader:
    def __init__(self):
        self.youtube_api = None
        if YOUTUBE_API_KEY:
            self.youtube_api = build('youtube', 'v3', developerKey=YOUTUBE_API_KEY)
        
        self.progress = self.load_progress()
        self.stats = {
            'youtube_api_success': 0,
            'youtube_transcript_api_success': 0,
            'yt_dlp_success': 0,
            'whisper_success': 0,
            'total_failures': 0,
            'skipped_existing': 0
        }
    
    def load_progress(self) -> Dict:
        """Load processing progress from file."""
        if os.path.exists(PROGRESS_FILE):
            with open(PROGRESS_FILE, 'r') as f:
                return json.load(f)
        return {
            'processed': [],
            'failed': [],
            'methods': {}  # Track which method was used for each video
        }
    
    def save_progress(self):
        """Save processing progress to file."""
        with open(PROGRESS_FILE, 'w') as f:
            json.dump(self.progress, f, indent=2)
    
    def get_video_id(self, url: str) -> Optional[str]:
        """Extract video ID from YouTube URL."""
        if not url:
            return None
            
        # Handle different YouTube URL formats
        if 'youtu.be' in url:
            return url.split('/')[-1].split('?')[0]
        elif 'youtube.com' in url:
            parsed = urlparse(url)
            return parse_qs(parsed.query).get('v', [None])[0]
        return None
    
    def get_transcript_via_youtube_api(self, video_id: str) -> Optional[List[Dict]]:
        """Get transcript using YouTube API (most reliable method)."""
        if not self.youtube_api:
            return None
            
        try:
            # Get video details including available captions
            request = self.youtube_api.videos().list(
                part="snippet",
                id=video_id
            )
            response = request.execute()
            
            if not response.get('items'):
                return None
            
            # Get captions list
            captions_request = self.youtube_api.captions().list(
                part="snippet",
                videoId=video_id
            )
            captions_response = captions_request.execute()
            
            # Find English captions
            caption_id = None
            for item in captions_response.get('items', []):
                snippet = item.get('snippet', {})
                if snippet.get('language') in ['en', 'en-US', 'en-GB']:
                    caption_id = item.get('id')
                    break
            
            if not caption_id:
                return None
            
            # Download the caption
            caption_request = self.youtube_api.captions().download(
                id=caption_id,
                tfmt='srt'  # SubRip format
            )
            caption_content = caption_request.execute()
            
            # Parse SRT format
            transcript = self.parse_srt_content(caption_content)
            return transcript
            
        except HttpError as e:
            if e.resp.status == 403:
                print(f"  ⚠️  Captions disabled for video {video_id}")
            else:
                print(f"  ❌ YouTube API error for {video_id}: {e}")
            return None
        except Exception as e:
            print(f"  ❌ Unexpected error with YouTube API for {video_id}: {e}")
            return None
    
    def parse_srt_content(self, content: str) -> List[Dict]:
        """Parse SRT subtitle content into transcript format."""
        transcript = []
        lines = content.strip().split('\n')
        
        i = 0
        while i < len(lines):
            # Skip sequence number
            if lines[i].strip().isdigit():
                i += 1
                continue
                
            # Parse timestamp line
            if '-->' in lines[i]:
                timestamp_line = lines[i]
                start_time = timestamp_line.split(' --> ')[0]
                start_seconds = self.srt_time_to_seconds(start_time)
                
                # Get text content
                text_lines = []
                i += 1
                while i < len(lines) and lines[i].strip():
                    text_lines.append(lines[i].strip())
                    i += 1
                
                if text_lines:
                    transcript.append({
                        'start': start_seconds,
                        'text': ' '.join(text_lines)
                    })
            
            i += 1
        
        return transcript
    
    def srt_time_to_seconds(self, time_str: str) -> float:
        """Convert SRT timestamp to seconds."""
        # Format: HH:MM:SS,mmm
        time_str = time_str.replace(',', '.')
        parts = time_str.split(':')
        hours = int(parts[0])
        minutes = int(parts[1])
        seconds = float(parts[2])
        return hours * 3600 + minutes * 60 + seconds
    
    def get_transcript_via_transcript_api(self, video_id: str) -> Optional[List[Dict]]:
        """Get transcript using youtube-transcript-api."""
        try:
            transcript = YouTubeTranscriptApi.get_transcript(video_id)
            return transcript
        except (TranscriptsDisabled, NoTranscriptFound):
            return None
        except Exception as e:
            print(f"  ❌ Transcript API error for {video_id}: {e}")
            return None
    
    def get_transcript_via_yt_dlp(self, url: str) -> Optional[List[Dict]]:
        """Get transcript using yt-dlp (can extract more formats)."""
        try:
            ydl_opts = {
                'writeautomaticsub': True,
                'skip_download': True,
                'quiet': True,
                'no_warnings': True,
                'subtitleslangs': ['en', 'en-US', 'en-GB'],
                'subtitlesformat': 'vtt',
            }
            
            with yt_dlp.YoutubeDL(ydl_opts) as ydl:
                info = ydl.extract_info(url, download=False)
                
                # Check if automatic subtitles were found
                auto_subs = info.get('automatic_captions', {})
                if 'en' in auto_subs:
                    # Get the VTT subtitle URL
                    vtt_url = None
                    for subtitle in auto_subs['en']:
                        if subtitle['ext'] == 'vtt':
                            vtt_url = subtitle['url']
                            break
                    
                    if vtt_url:
                        # Download and parse VTT content
                        response = requests.get(vtt_url)
                        if response.status_code == 200:
                            return self.parse_vtt_content(response.text)
                
                return None
                
        except Exception as e:
            print(f"  ❌ yt-dlp error for {url}: {e}")
            return None
    
    def parse_vtt_content(self, content: str) -> List[Dict]:
        """Parse VTT subtitle content into transcript format."""
        transcript = []
        lines = content.strip().split('\n')
        
        i = 0
        while i < len(lines):
            line = lines[i].strip()
            
            # Look for timestamp lines
            if '-->' in line:
                # Parse timestamp
                start_time = line.split(' --> ')[0]
                start_seconds = self.vtt_time_to_seconds(start_time)
                
                # Get text content
                text_lines = []
                i += 1
                while i < len(lines) and lines[i].strip() and '-->' not in lines[i]:
                    text_lines.append(lines[i].strip())
                    i += 1
                
                if text_lines:
                    # Remove VTT tags
                    text = ' '.join(text_lines)
                    text = self.clean_vtt_text(text)
                    transcript.append({
                        'start': start_seconds,
                        'text': text
                    })
                continue
            
            i += 1
        
        return transcript
    
    def vtt_time_to_seconds(self, time_str: str) -> float:
        """Convert VTT timestamp to seconds."""
        # Format: HH:MM:SS.mmm
        parts = time_str.split(':')
        hours = int(parts[0])
        minutes = int(parts[1])
        seconds = float(parts[2])
        return hours * 3600 + minutes * 60 + seconds
    
    def clean_vtt_text(self, text: str) -> str:
        """Clean VTT text by removing formatting tags."""
        # Remove common VTT tags
        import re
        text = re.sub(r'<[^>]+>', '', text)  # Remove HTML-like tags
        text = re.sub(r'&[^;]+;', '', text)  # Remove HTML entities
        return text.strip()
    
    def download_transcript(self, video_id: str, title: str, url: str) -> bool:
        """Download transcript using the best available method."""
        transcript_file = os.path.join(TRANSCRIPT_DIR, f"{video_id}.txt")
        
        # Skip if already exists
        if os.path.exists(transcript_file):
            print(f"  ⏭️  Transcript already exists for {video_id}")
            self.stats['skipped_existing'] += 1
            return True
        
        # Try methods in order of preference
        methods = [
            ("YouTube API", lambda: self.get_transcript_via_youtube_api(video_id)),
            ("Transcript API", lambda: self.get_transcript_via_transcript_api(video_id)),
            ("yt-dlp", lambda: self.get_transcript_via_yt_dlp(url))
        ]
        
        for method_name, method_func in methods:
            print(f"  πŸ”„ Trying {method_name} for {video_id}...")
            
            try:
                transcript = method_func()
                if transcript:
                    # Save transcript
                    with open(transcript_file, 'w', encoding='utf-8') as f:
                        for entry in transcript:
                            f.write(f"{entry['start']:.2f}s: {entry['text']}\n")
                    
                    print(f"   Success with {method_name} for {video_id}")
                    self.progress['processed'].append(url)
                    self.progress['methods'][video_id] = method_name
                    self.stats[f"{method_name.lower().replace(' ', '_')}_success"] += 1
                    return True
                else:
                    print(f"  ❌ No transcript found with {method_name} for {video_id}")
                    
            except Exception as e:
                print(f"  ❌ Error with {method_name} for {video_id}: {e}")
                continue
        
        # All methods failed
        print(f"  ❌ All methods failed for {video_id}")
        self.progress['failed'].append(url)
        self.stats['total_failures'] += 1
        return False
    
    def process_videos(self):
        """Process all videos from the JSON file."""
        # Load video data
        try:
            with open(VIDEOS_JSON, 'r', encoding='utf-8') as f:
                videos = json.load(f)
        except FileNotFoundError:
            print(f"❌ Error: {VIDEOS_JSON} not found. Please run outlier_scraper.py first.")
            return
        
        print(f"πŸ“š Found {len(videos)} videos to process")
        
        # Filter out already processed videos
        processed_urls = set(self.progress['processed'])
        failed_urls = set(self.progress['failed'])
        
        remaining_videos = [
            v for v in videos 
            if v.get('url') not in processed_urls and v.get('url') not in failed_urls
        ]
        
        print(f" {len(processed_urls)} already processed")
        print(f"❌ {len(failed_urls)} previously failed")
        print(f"🎯 {len(remaining_videos)} remaining to process")
        
        if not remaining_videos:
            print(" All videos have been processed!")
            return
        
        # Process remaining videos
        start_time = time.time()
        
        for i, video in enumerate(remaining_videos):
            video_id = self.get_video_id(video.get('url'))
            if not video_id:
                print(f"⚠️  Could not extract ID from {video.get('url')}")
                continue
            
            title = video.get('title', 'Unknown Title')
            print(f"\nπŸ“Ή Processing {i+1}/{len(remaining_videos)}: {title}")
            
            success = self.download_transcript(video_id, title, video.get('url'))
            
            # Save progress after each video
            self.save_progress()
            
            # Rate limiting
            time.sleep(1)
        
        # Final statistics
        elapsed_time = time.time() - start_time
        print(f"\nπŸ“Š Processing Summary:")
        print(f" YouTube API successes: {self.stats['youtube_api_success']}")
        print(f" Transcript API successes: {self.stats['youtube_transcript_api_success']}")
        print(f" yt-dlp successes: {self.stats['yt_dlp_success']}")
        print(f"⏭️  Skipped existing: {self.stats['skipped_existing']}")
        print(f"❌ Total failures: {self.stats['total_failures']}")
        print(f"⏱️  Total time: {elapsed_time:.2f} seconds")
        
        # Show failed videos for manual review
        if self.progress['failed']:
            print(f"\n❌ Failed videos ({len(self.progress['failed'])}):")
            for url in self.progress['failed'][-10:]:  # Show last 10 failures
                print(f"  - {url}")
            if len(self.progress['failed']) > 10:
                print(f"  ... and {len(self.progress['failed']) - 10} more")

def main():
    """Main function to run the transcript downloader."""
    downloader = TranscriptDownloader()
    
    if not YOUTUBE_API_KEY:
        print("⚠️  WARNING: No YouTube API key found. Some methods may not work.")
        print("   Set YOUTUBE_API_KEY in your .env file for best results.")
    
    downloader.process_videos()

if __name__ == "__main__":
    main()