ganesh3 commited on
Commit
15c3ea7
·
verified ·
1 Parent(s): e6ba543

Update app/transcript_extractor.py

Browse files
Files changed (1) hide show
  1. app/transcript_extractor.py +20 -14
app/transcript_extractor.py CHANGED
@@ -16,20 +16,26 @@ import requests
16
  logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
17
  logger = logging.getLogger(__name__)
18
 
19
- # Get the directory of the current script
20
- current_dir = os.path.dirname(os.path.abspath(__file__))
21
- # Construct the path to the .env file (one directory up from the current script)
22
- dotenv_path = os.path.join(os.path.dirname(current_dir), '.env')
23
- logger.info(f"The .env path is: {dotenv_path}")
24
- # Load environment variables from .env file
25
- load_dotenv(dotenv_path)
 
 
26
 
27
  # Get API key from environment variable
28
  API_KEY = os.getenv('YOUTUBE_API_KEY')
29
- logger.info(f"API_KEY: {API_KEY[:5]}...{API_KEY[-5:]}") # Log first and last 5 characters for verification
30
 
31
- if not API_KEY:
32
- raise ValueError("YouTube API key not found. Make sure it's set in your .env file in the parent directory of the 'app' folder.")
 
 
 
 
 
33
 
34
  def get_youtube_client():
35
  try:
@@ -49,6 +55,7 @@ def get_youtube_client():
49
  logger.error(f"Error initializing YouTube API client: {str(e)}")
50
  raise
51
 
 
52
  def extract_video_id(url):
53
  if not url:
54
  return None
@@ -69,7 +76,6 @@ def get_video_metadata(video_id):
69
  video = response['items'][0]
70
  snippet = video['snippet']
71
 
72
- # Get the description and set default if it's blank
73
  description = snippet.get('description', '').strip()
74
  if not description:
75
  description = 'Not Available'
@@ -82,7 +88,7 @@ def get_video_metadata(video_id):
82
  'like_count': video['statistics'].get('likeCount', '0'),
83
  'comment_count': video['statistics'].get('commentCount', '0'),
84
  'duration': video['contentDetails']['duration'],
85
- 'description': description # Add the description to the metadata
86
  }
87
  else:
88
  logger.error(f"No video found with id: {video_id}")
@@ -120,7 +126,7 @@ def get_channel_videos(channel_url):
120
  part="id,snippet",
121
  channelId=channel_id,
122
  type="video",
123
- maxResults=50 # Adjust as needed
124
  )
125
  response = request.execute()
126
 
@@ -147,8 +153,8 @@ def extract_channel_id(url):
147
  return None
148
 
149
  def test_api_key():
150
- youtube = get_youtube_client()
151
  try:
 
152
  request = youtube.videos().list(part="snippet", id="dQw4w9WgXcQ")
153
  response = request.execute()
154
  if 'items' in response:
 
16
  logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
17
  logger = logging.getLogger(__name__)
18
 
19
+ # Try to load from .env file if it exists, but don't fail if it doesn't
20
+ try:
21
+ current_dir = os.path.dirname(os.path.abspath(__file__))
22
+ dotenv_path = os.path.join(os.path.dirname(current_dir), '.env')
23
+ if os.path.exists(dotenv_path):
24
+ load_dotenv(dotenv_path)
25
+ logger.info(f"Loaded environment variables from {dotenv_path}")
26
+ except Exception as e:
27
+ logger.warning(f"Could not load .env file: {e}")
28
 
29
  # Get API key from environment variable
30
  API_KEY = os.getenv('YOUTUBE_API_KEY')
 
31
 
32
+ # Safe logging of API key
33
+ if API_KEY:
34
+ masked_key = f"{API_KEY[:3]}...{API_KEY[-3:]}" if len(API_KEY) > 6 else "***"
35
+ logger.info(f"API_KEY found (masked): {masked_key}")
36
+ else:
37
+ logger.error("YouTube API key not found in environment variables")
38
+ raise ValueError("YouTube API key not found. Make sure it's set in your environment variables or .env file.")
39
 
40
  def get_youtube_client():
41
  try:
 
55
  logger.error(f"Error initializing YouTube API client: {str(e)}")
56
  raise
57
 
58
+ # Rest of your existing functions remain the same...
59
  def extract_video_id(url):
60
  if not url:
61
  return None
 
76
  video = response['items'][0]
77
  snippet = video['snippet']
78
 
 
79
  description = snippet.get('description', '').strip()
80
  if not description:
81
  description = 'Not Available'
 
88
  'like_count': video['statistics'].get('likeCount', '0'),
89
  'comment_count': video['statistics'].get('commentCount', '0'),
90
  'duration': video['contentDetails']['duration'],
91
+ 'description': description
92
  }
93
  else:
94
  logger.error(f"No video found with id: {video_id}")
 
126
  part="id,snippet",
127
  channelId=channel_id,
128
  type="video",
129
+ maxResults=50
130
  )
131
  response = request.execute()
132
 
 
153
  return None
154
 
155
  def test_api_key():
 
156
  try:
157
+ youtube = get_youtube_client()
158
  request = youtube.videos().list(part="snippet", id="dQw4w9WgXcQ")
159
  response = request.execute()
160
  if 'items' in response: