59 lines
1.9 KiB
Python
59 lines
1.9 KiB
Python
import sys
|
|
import os
|
|
from fastapi import FastAPI, HTTPException, Query
|
|
from typing import List, Optional
|
|
|
|
# --- BLOQUE DE DIAGNÓSTICO E IMPORTACIÓN SEGURA ---
|
|
print("--- INICIO DE DIAGNÓSTICO ---")
|
|
try:
|
|
# Importamos el MÓDULO entero, no la clase directamente
|
|
import youtube_transcript_api
|
|
print(f"1. Archivo cargado desde: {youtube_transcript_api.__file__}")
|
|
|
|
# Ahora sacamos la clase del módulo manualmente
|
|
TranscriptsDisabled = youtube_transcript_api.TranscriptsDisabled
|
|
NoTranscriptFound = youtube_transcript_api.NoTranscriptFound
|
|
YTApi = youtube_transcript_api.YouTubeTranscriptApi
|
|
|
|
print(f"2. Clase detectada: {YTApi}")
|
|
print(f"3. Métodos en la clase: {dir(YTApi)}")
|
|
except ImportError as e:
|
|
print(f"!!! ERROR CRÍTICO DE IMPORTACIÓN: {e}")
|
|
sys.exit(1)
|
|
print("--- FIN DE DIAGNÓSTICO ---")
|
|
# --------------------------------------------------
|
|
|
|
app = FastAPI(title="YouTube Transcript API Self-Hosted")
|
|
|
|
@app.get("/")
|
|
def read_root():
|
|
return {"status": "online"}
|
|
|
|
@app.get("/transcript/{video_id}")
|
|
def get_transcript(
|
|
video_id: str,
|
|
lang: Optional[str] = Query("es", description="Código de idioma")
|
|
):
|
|
try:
|
|
languages_to_try = [lang, 'en', 'es']
|
|
|
|
# Usamos la referencia que capturamos arriba (YTApi)
|
|
transcript_list = YTApi.get_transcript(video_id, languages=languages_to_try)
|
|
|
|
return {
|
|
"video_id": video_id,
|
|
"requested_lang": lang,
|
|
"transcript": transcript_list
|
|
}
|
|
|
|
except TranscriptsDisabled:
|
|
raise HTTPException(status_code=403, detail="Subtítulos desactivados.")
|
|
except NoTranscriptFound:
|
|
raise HTTPException(status_code=404, detail="No se encontraron subtítulos.")
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
if __name__ == "__main__":
|
|
import uvicorn
|
|
uvicorn.run(app, host="0.0.0.0", port=8080)
|