Spaces:
Sleeping
Sleeping
duck3-create Claude Opus 4.6 commited on
Commit Β·
49a1ac5
1
Parent(s): 0e7d362
Add retry logic for YouTube transcript fetching
Browse filesRetries up to 3 times with 1.5s delay to handle intermittent
YouTube API blocks on cloud server IPs (Railway).
Skips retry for non-retryable errors (no subtitles, disabled).
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
main.py
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from fastapi.responses import FileResponse, JSONResponse
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
from pydantic import BaseModel
|
| 5 |
from youtube_transcript_api import YouTubeTranscriptApi
|
| 6 |
import re
|
|
|
|
| 7 |
import asyncio
|
| 8 |
from concurrent.futures import ThreadPoolExecutor
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
app = FastAPI(title="YouTube Transcript Extractor")
|
| 11 |
|
| 12 |
app.add_middleware(
|
|
@@ -52,6 +57,9 @@ KOREAN_FILLERS = {
|
|
| 52 |
|
| 53 |
NOISE_PATTERN = re.compile(r"^\[.*\]$")
|
| 54 |
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
def denoise_text(text: str) -> str:
|
| 57 |
lines = text.split("\n")
|
|
@@ -73,49 +81,62 @@ def denoise_text(text: str) -> str:
|
|
| 73 |
|
| 74 |
|
| 75 |
def _fetch_transcript(video_id: str, language: str, denoise: bool, fmt: str) -> dict:
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
text =
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
|
| 121 |
@app.post("/api/transcripts")
|
|
|
|
| 1 |
+
import logging
|
| 2 |
from fastapi import FastAPI
|
| 3 |
from fastapi.responses import FileResponse, JSONResponse
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
from pydantic import BaseModel
|
| 6 |
from youtube_transcript_api import YouTubeTranscriptApi
|
| 7 |
import re
|
| 8 |
+
import time
|
| 9 |
import asyncio
|
| 10 |
from concurrent.futures import ThreadPoolExecutor
|
| 11 |
|
| 12 |
+
logging.basicConfig(level=logging.INFO)
|
| 13 |
+
logger = logging.getLogger(__name__)
|
| 14 |
+
|
| 15 |
app = FastAPI(title="YouTube Transcript Extractor")
|
| 16 |
|
| 17 |
app.add_middleware(
|
|
|
|
| 57 |
|
| 58 |
NOISE_PATTERN = re.compile(r"^\[.*\]$")
|
| 59 |
|
| 60 |
+
MAX_RETRIES = 3
|
| 61 |
+
RETRY_DELAY = 1.5
|
| 62 |
+
|
| 63 |
|
| 64 |
def denoise_text(text: str) -> str:
|
| 65 |
lines = text.split("\n")
|
|
|
|
| 81 |
|
| 82 |
|
| 83 |
def _fetch_transcript(video_id: str, language: str, denoise: bool, fmt: str) -> dict:
|
| 84 |
+
languages = [language]
|
| 85 |
+
if language == "ko":
|
| 86 |
+
languages.append("en")
|
| 87 |
+
elif language == "en":
|
| 88 |
+
languages.append("ko")
|
| 89 |
+
|
| 90 |
+
last_error = None
|
| 91 |
+
for attempt in range(1, MAX_RETRIES + 1):
|
| 92 |
+
try:
|
| 93 |
+
data = _yt_api.fetch(video_id, languages=languages)
|
| 94 |
+
|
| 95 |
+
if fmt == "json":
|
| 96 |
+
entries = [
|
| 97 |
+
{"text": e.text, "start": e.start, "duration": e.duration}
|
| 98 |
+
for e in data
|
| 99 |
+
]
|
| 100 |
+
if denoise:
|
| 101 |
+
deduped = []
|
| 102 |
+
prev_text = None
|
| 103 |
+
for entry in entries:
|
| 104 |
+
t = entry["text"].strip()
|
| 105 |
+
if t in KOREAN_FILLERS or NOISE_PATTERN.match(t):
|
| 106 |
+
continue
|
| 107 |
+
if t == prev_text:
|
| 108 |
+
continue
|
| 109 |
+
if t:
|
| 110 |
+
entry["text"] = t
|
| 111 |
+
deduped.append(entry)
|
| 112 |
+
prev_text = t
|
| 113 |
+
entries = deduped
|
| 114 |
+
return {"transcript": entries, "error": None}
|
| 115 |
+
else:
|
| 116 |
+
text = "\n".join(e.text for e in data)
|
| 117 |
+
if denoise:
|
| 118 |
+
text = denoise_text(text)
|
| 119 |
+
return {"transcript": text, "error": None}
|
| 120 |
+
except Exception as e:
|
| 121 |
+
last_error = str(e)
|
| 122 |
+
logger.error(f"Attempt {attempt}/{MAX_RETRIES} failed for {video_id}: {last_error}")
|
| 123 |
+
|
| 124 |
+
# Don't retry if video genuinely has no subtitles
|
| 125 |
+
if "No transcripts" in last_error or "disabled" in last_error.lower():
|
| 126 |
+
break
|
| 127 |
+
|
| 128 |
+
if attempt < MAX_RETRIES:
|
| 129 |
+
time.sleep(RETRY_DELAY)
|
| 130 |
+
|
| 131 |
+
# All retries exhausted or non-retryable error
|
| 132 |
+
error_msg = last_error or "Unknown error"
|
| 133 |
+
if "No transcripts" in error_msg or "Could not retrieve" in error_msg:
|
| 134 |
+
error_msg = f"μλ§μ μ°Ύμ μ μμ΅λλ€. ({error_msg[:120]})"
|
| 135 |
+
elif "disabled" in error_msg.lower():
|
| 136 |
+
error_msg = "μ΄ μμμ μλ§μ΄ λΉνμ±νλμ΄ μμ΅λλ€."
|
| 137 |
+
elif "unavailable" in error_msg.lower():
|
| 138 |
+
error_msg = "μμμ μ°Ύμ μ μμ΅λλ€."
|
| 139 |
+
return {"transcript": None, "error": error_msg}
|
| 140 |
|
| 141 |
|
| 142 |
@app.post("/api/transcripts")
|