ztoolx commited on
Commit
84ca6c2
·
verified ·
1 Parent(s): 346f627
Files changed (1) hide show
  1. main.py +9 -7
main.py CHANGED
@@ -1,6 +1,6 @@
1
  from fastapi import FastAPI, HTTPException
2
  from fastapi.middleware.cors import CORSMiddleware
3
- from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound, VideoUnavailable, TooManyRequests, NoTranscriptAvailable
4
  import re
5
  from pydantic import BaseModel
6
 
@@ -74,18 +74,20 @@ async def get_transcript(req: TranscriptRequest):
74
 
75
  except HTTPException:
76
  raise # Re-raise our custom HTTP exceptions
77
- except TooManyRequests:
78
- raise HTTPException(status_code=429, detail="YouTube is rate-limiting requests from this server. Please try again in a minute.")
79
  except TranscriptsDisabled:
80
  raise HTTPException(status_code=403, detail="Captions are disabled for this video.")
81
  except VideoUnavailable:
82
  raise HTTPException(status_code=404, detail="Video is unavailable.")
83
- except NoTranscriptAvailable:
84
- raise HTTPException(status_code=404, detail="No transcript is available for this video.")
85
  except Exception as e:
86
  # Catch-all: Log the exact error to Hugging Face logs so we can see what went wrong!
87
- print(f"UNHANDLED ERROR for {video_id}: {type(e).__name__} - {str(e)}")
88
- raise HTTPException(status_code=500, detail=f"Failed to fetch transcript: {type(e).__name__} - {str(e)}")
 
 
 
 
 
 
89
 
90
  # KEEP-ALIVE ENDPOINT
91
  @app.get("/api/health")
 
1
  from fastapi import FastAPI, HTTPException
2
  from fastapi.middleware.cors import CORSMiddleware
3
+ from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound, VideoUnavailable
4
  import re
5
  from pydantic import BaseModel
6
 
 
74
 
75
  except HTTPException:
76
  raise # Re-raise our custom HTTP exceptions
 
 
77
  except TranscriptsDisabled:
78
  raise HTTPException(status_code=403, detail="Captions are disabled for this video.")
79
  except VideoUnavailable:
80
  raise HTTPException(status_code=404, detail="Video is unavailable.")
 
 
81
  except Exception as e:
82
  # Catch-all: Log the exact error to Hugging Face logs so we can see what went wrong!
83
+ error_name = type(e).__name__
84
+ print(f"UNHANDLED ERROR for {video_id}: {error_name} - {str(e)}")
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
 
92
  # KEEP-ALIVE ENDPOINT
93
  @app.get("/api/health")