ztoolx commited on
Commit
eceba31
·
verified ·
1 Parent(s): 4d69ddd
Files changed (1) hide show
  1. main.py +12 -12
main.py CHANGED
@@ -35,23 +35,25 @@ async def get_transcript(req: TranscriptRequest):
35
  raise HTTPException(status_code=400, detail="Invalid YouTube URL")
36
 
37
  try:
38
- print(f"Attempting to fetch transcript for video_id: {video_id} using youtube-transcript-api v0.6+")
39
 
40
- # Initialize the API client (New syntax for 0.6+)
41
- ytt_api = YouTubeTranscriptApi()
42
-
43
- # Fetch the transcript (prefers English)
44
  try:
45
- transcript = ytt_api.fetch(video_id, languages=['en'])
 
46
  print("Found English transcript.")
47
  except NoTranscriptFound:
48
  print("No English found, trying any available language...")
49
  # Fallback to any language if English is not found
50
- transcript = ytt_api.fetch(video_id)
51
- print(f"Found fallback transcript.")
 
 
 
 
52
 
53
- # Join the text from the snippets (New syntax for 0.6+)
54
- full_text = " ".join([snippet.text for snippet in transcript])
55
 
56
  # Clean up HTML entities
57
  full_text = full_text.replace("'", "'").replace(""", '"').replace("&", "&").replace("'", "'")
@@ -63,8 +65,6 @@ async def get_transcript(req: TranscriptRequest):
63
  raise HTTPException(status_code=403, detail="Captions are disabled for this video.")
64
  except VideoUnavailable:
65
  raise HTTPException(status_code=404, detail="Video is unavailable.")
66
- except NoTranscriptFound:
67
- raise HTTPException(status_code=404, detail="No captions found for this video.")
68
  except Exception as e:
69
  error_name = type(e).__name__
70
  print(f"UNHANDLED ERROR for {video_id}: {error_name} - {str(e)}")
 
35
  raise HTTPException(status_code=400, detail="Invalid YouTube URL")
36
 
37
  try:
38
+ print(f"Attempting to fetch transcript for video_id: {video_id} using youtube-transcript-api 0.5.0")
39
 
40
+ # Try fetching English transcript first
 
 
 
41
  try:
42
+ print("Trying English transcript...")
43
+ transcript_data = YouTubeTranscriptApi.get_transcript(video_id, languages=['en'])
44
  print("Found English transcript.")
45
  except NoTranscriptFound:
46
  print("No English found, trying any available language...")
47
  # Fallback to any language if English is not found
48
+ try:
49
+ transcript_data = YouTubeTranscriptApi.get_transcript(video_id)
50
+ print("Found fallback language transcript.")
51
+ except Exception as e:
52
+ print(f"Fallback failed: {e}")
53
+ raise HTTPException(status_code=404, detail="No captions found for this video.")
54
 
55
+ # Combine all text lines safely
56
+ full_text = " ".join([line.get('text', '') for line in transcript_data])
57
 
58
  # Clean up HTML entities
59
  full_text = full_text.replace("'", "'").replace(""", '"').replace("&", "&").replace("'", "'")
 
65
  raise HTTPException(status_code=403, detail="Captions are disabled for this video.")
66
  except VideoUnavailable:
67
  raise HTTPException(status_code=404, detail="Video is unavailable.")
 
 
68
  except Exception as e:
69
  error_name = type(e).__name__
70
  print(f"UNHANDLED ERROR for {video_id}: {error_name} - {str(e)}")