TubeScript-API/fix-and-restart.sh

125 lines
3.9 KiB
Bash
Executable File

#!/bin/bash
# Script para reconstruir y levantar TubeScript-API con soporte correcto de YouTube
set -e
REPO_DIR="/home/xesar/PycharmProjects/TubeScript-API"
cd "$REPO_DIR"
echo "======================================================"
echo " TubeScript-API - Fix & Restart"
echo "======================================================"
# 1. Parar contenedor anterior si existe
echo ""
echo ">>> [1/7] Parando contenedor anterior..."
docker stop tubescript_api 2>/dev/null && echo " Parado." || echo " No estaba corriendo."
docker rm tubescript_api 2>/dev/null && echo " Eliminado." || echo " No existia."
# 2. Construir imagen con tag explícito (siempre sin cache para forzar yt-dlp latest)
echo ""
echo ">>> [2/7] Construyendo imagen tubescript-api:latest ..."
docker build -f Dockerfile.api -t tubescript-api:latest .
echo " Build OK."
# 3. Asegurar permisos de ./data
echo ""
echo ">>> [3/7] Asegurando permisos de ./data ..."
mkdir -p ./data
chown -R "$(id -u):$(id -g)" ./data 2>/dev/null || sudo chown -R "$(id -u):$(id -g)" ./data
chmod -R u+rwX ./data
ls -la ./data
echo " Permisos OK."
# 4. Crear red si no existe
echo ""
echo ">>> [4/7] Asegurando red tubescript-network ..."
docker network create tubescript-network 2>/dev/null && echo " Red creada." || echo " Red ya existe."
# 5. Levantar contenedor
echo ""
echo ">>> [5/7] Levantando contenedor ..."
docker run -d \
--name tubescript_api \
--network tubescript-network \
-p 8282:8000 \
-v "${REPO_DIR}/data:/app/data:rw" \
-e PYTHONUNBUFFERED=1 \
-e API_COOKIES_PATH=/app/data/cookies.txt \
--restart unless-stopped \
tubescript-api:latest
echo " Contenedor iniciado. Esperando arranque de uvicorn..."
sleep 6
# 6. Verificaciones internas
echo ""
echo ">>> [6/7] Verificaciones del contenedor ..."
echo ""
echo "-- Estado:"
docker ps --filter "name=tubescript_api" --format " ID={{.ID}} STATUS={{.Status}} PORTS={{.Ports}}"
echo ""
echo "-- Logs uvicorn:"
docker logs tubescript_api 2>&1 | tail -6
echo ""
echo "-- Versiones:"
docker exec tubescript_api sh -c "
echo ' node :' \$(node --version 2>/dev/null || echo 'no instalado')
echo ' yt-dlp :' \$(yt-dlp --version 2>/dev/null || echo 'no instalado')
"
# 7. Prueba real de yt-dlp con player_client=android (evita n-challenge sin Node extras)
echo ""
echo ">>> [7/7] Prueba yt-dlp (android client) ..."
echo ""
echo "-- Sin cookies (android client):"
docker exec tubescript_api yt-dlp \
--no-warnings --skip-download \
--extractor-args "youtube:player_client=android" \
--print title \
"https://www.youtube.com/watch?v=dQw4w9WgXcQ" 2>&1 \
&& echo " OK" || echo " FALLO"
echo ""
echo "-- Con cookies (mweb client — acepta cookies web sin n-challenge):"
if [ -s "${REPO_DIR}/data/cookies.txt" ]; then
docker exec tubescript_api yt-dlp \
--cookies /app/data/cookies.txt \
--no-warnings --skip-download \
--extractor-args "youtube:player_client=mweb" \
--print title \
"https://www.youtube.com/watch?v=dQw4w9WgXcQ" 2>&1 \
&& echo " OK - título obtenido con cookies" || echo " FALLO con cookies"
else
echo " AVISO: cookies.txt vacío o no existe."
echo " Sube tus cookies: curl 'http://127.0.0.1:8282/upload_cookies' -F 'file=@/ruta/cookies.txt'"
fi
echo ""
echo "-- Endpoint /debug/metadata:"
sleep 2
curl -s --max-time 30 "http://127.0.0.1:8282/debug/metadata/dQw4w9WgXcQ" \
| python3 -c "
import sys, json
try:
d = json.loads(sys.stdin.read())
print(' title :', d.get('title','?'))
print(' is_live :', d.get('is_live','?'))
print(' id :', d.get('id','?'))
except Exception as e:
print(' ERROR:', e)
" 2>&1
echo ""
echo "======================================================"
echo " LISTO."
echo " API: http://127.0.0.1:8282"
echo " Docs: http://127.0.0.1:8282/docs"
echo ""
echo " Subir cookies:"
echo " curl 'http://127.0.0.1:8282/upload_cookies' -F 'file=@./data/cookies.txt'"
echo "======================================================"