11import os
2- import re
2+ import sys
33import nltk
44import pytube
5- import youtube_transcript_api
65from youtube_transcript_api import YouTubeTranscriptApi
76from nltk .corpus import stopwords
87from nltk .tokenize import sent_tokenize , word_tokenize
2120nltk .download ('punkt' , quiet = True )
2221nltk .download ('stopwords' , quiet = True )
2322
24- # Initialize OpenAI client
25- client = OpenAI (
26- base_url = "https://openrouter.ai/api/v1" ,
27- api_key = "<api_key>" , # Add your OpenRouter API key here
28- )
23+ # Initialize OpenAI client from environment variable
24+ # Expect the OpenRouter API key to be provided via OPENROUTER_API_KEY
25+ api_key = os .getenv ("OPENROUTER_API_KEY" )
26+ if not api_key :
27+ print (Fore .RED + "Error: OPENROUTER_API_KEY environment variable is not set or is still the placeholder ('<api_key>')." )
28+ sys .exit (1 )
29+ else :
30+ client = OpenAI (
31+ base_url = "https://openrouter.ai/api/v1" ,
32+ api_key = api_key ,
33+ )
2934
3035def extract_video_id (youtube_url ):
3136 """Extract the video ID from a YouTube URL."""
@@ -48,8 +53,10 @@ def extract_video_id(youtube_url):
4853def get_transcript (video_id ):
4954 """Get the transcript of a YouTube video."""
5055 try :
51- transcript = YouTubeTranscriptApi .get_transcript (video_id )
52- return ' ' .join ([entry ['text' ] for entry in transcript ])
56+ youtube_transcript_api = YouTubeTranscriptApi ()
57+ fetched_transcript = youtube_transcript_api .fetch (video_id )
58+ full_transcript = " " .join ([snippet .text for snippet in fetched_transcript .snippets ])
59+ return full_transcript .strip ()
5360 except Exception as e :
5461 return f"Error retrieving transcript: { str (e )} ."
5562
0 commit comments