ztoolx commited on
Commit
8496d82
·
verified ·
1 Parent(s): 84ca6c2

uopdatred

Browse files
Files changed (1) hide show
  1. main.py +14 -20
main.py CHANGED
@@ -37,32 +37,26 @@ async def get_transcript(req: TranscriptRequest):
37
  try:
38
  print(f"Attempting to fetch transcript for video_id: {video_id}")
39
 
40
- # Fetch available transcripts
41
- transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
42
 
43
- # Find the best transcript (Prefer manual English, then auto English, then any other)
44
- transcript = None
45
  try:
46
- transcript = transcript_list.find_manually_created_transcript(['en'])
47
- print("Found manual English transcript.")
 
48
  except NoTranscriptFound:
 
 
49
  try:
50
- transcript = transcript_list.find_generated_transcript(['en'])
51
- print("Found auto-generated English transcript.")
52
- except NoTranscriptFound:
53
- # If no English, get the first available language safely
54
- try:
55
- transcript = next(iter(transcript_list))
56
- print(f"Found fallback transcript: {transcript.language_code}")
57
- except StopIteration:
58
- pass
59
 
60
- if not transcript:
61
  raise HTTPException(status_code=404, detail="No captions found for this video.")
62
 
63
- # Fetch the actual text data
64
- transcript_data = transcript.fetch()
65
-
66
  # Combine all text lines safely
67
  full_text = " ".join([line.get('text', '') for line in transcript_data])
68
 
@@ -85,7 +79,7 @@ async def get_transcript(req: TranscriptRequest):
85
 
86
  # Check if it's a rate limit error dynamically
87
  if "TooManyRequests" in error_name or "429" in str(e):
88
- raise HTTPException(status_code=429, detail="YouTube is rate-limiting requests from this server. Please try again in a minute.")
89
 
90
  raise HTTPException(status_code=500, detail=f"Failed to fetch transcript: {error_name} - {str(e)}")
91
 
 
37
  try:
38
  print(f"Attempting to fetch transcript for video_id: {video_id}")
39
 
40
+ transcript_data = None
 
41
 
42
+ # 1. Try fetching English transcript first
 
43
  try:
44
+ print("Trying English transcript...")
45
+ transcript_data = YouTubeTranscriptApi.get_transcript(video_id, languages=['en'])
46
+ print("Found English transcript.")
47
  except NoTranscriptFound:
48
+ print("No English transcript found. Trying any available language...")
49
+ # 2. If no English, fetch the first available language automatically
50
  try:
51
+ transcript_data = YouTubeTranscriptApi.get_transcript(video_id)
52
+ print("Found fallback language transcript.")
53
+ except Exception as e:
54
+ print(f"Fallback failed: {e}")
55
+ pass
 
 
 
 
56
 
57
+ if not transcript_data:
58
  raise HTTPException(status_code=404, detail="No captions found for this video.")
59
 
 
 
 
60
  # Combine all text lines safely
61
  full_text = " ".join([line.get('text', '') for line in transcript_data])
62
 
 
79
 
80
  # Check if it's a rate limit error dynamically
81
  if "TooManyRequests" in error_name or "429" in str(e):
82
+ raise HTTPException(status_code=429, detail="YouTube is rate-limiting requests. Try again in a minute.")
83
 
84
  raise HTTPException(status_code=500, detail=f"Failed to fetch transcript: {error_name} - {str(e)}")
85