Spaces:
Sleeping
Sleeping
uopdatred
Browse files
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 |
-
|
| 41 |
-
transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
transcript = None
|
| 45 |
try:
|
| 46 |
-
|
| 47 |
-
|
|
|
|
| 48 |
except NoTranscriptFound:
|
|
|
|
|
|
|
| 49 |
try:
|
| 50 |
-
|
| 51 |
-
print("Found
|
| 52 |
-
except
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
transcript = next(iter(transcript_list))
|
| 56 |
-
print(f"Found fallback transcript: {transcript.language_code}")
|
| 57 |
-
except StopIteration:
|
| 58 |
-
pass
|
| 59 |
|
| 60 |
-
if not
|
| 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
|
| 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 |
|