tubapi-node/index.js
Cesar Mendivil b0e088b9de feat: integrate Vite for frontend development and build process
- Added Vite scripts for development, build, and preview in package.json
- Created README.md with instructions for frontend setup
- Added client/index.html as the main entry point for the frontend
- Implemented client/src/main.js for handling file uploads and UI interactions
- Added client/src/styles.css for styling the frontend components
- Configured Vite in vite.config.js for building the frontend assets
2026-03-21 20:27:18 -07:00

229 lines
8.4 KiB
JavaScript

const express = require('express');
const { spawn } = require('child_process');
const path = require('path');
const fs = require('fs');
const multer = require('multer');
const app = express();
const PORT = process.env.PORT || 8282;
// CONFIGURACIÓN DE RUTAS
const NODE_PATH = '/home/xesar/.nvm/versions/node/v20.19.4/bin/node';
const COOKIES_FILE = path.resolve(__dirname, 'cookies.txt');
const { warmAndExportCookies } = require('./cookiewarmer');
// --- CONFIGURACIÓN DE RUTAS Y CONSTANTES (Añade esto aquí) ---
const COOKIES_PATH = path.resolve(__dirname, 'cookies.txt');
// --- CONFIGURACIÓN DE MULTER (SUBIDA DE ARCHIVOS) ---
const storage = multer.diskStorage({
destination: (req, file, cb) => {
const uploadDir = path.resolve(__dirname, 'uploads');
if (!fs.existsSync(uploadDir)) fs.mkdirSync(uploadDir, { recursive: true });
cb(null, uploadDir);
},
filename: (req, file, cb) => cb(null, `${Date.now()}-${file.originalname}`)
});
const upload = multer({
storage: storage,
fileFilter: (req, file, cb) => {
if (path.extname(file.originalname) !== '.txt') {
return cb(new Error('Solo se permiten archivos .txt'), false);
}
cb(null, true);
}
});
// Función para sanear cookies (Evita errores de formato de yt-dlp)
function sanitizeCookies() {
if (fs.existsSync(COOKIES_FILE)) {
let content = fs.readFileSync(COOKIES_FILE, 'utf8');
if (content.includes('# Netscape HTTP Cookie File')) {
content = content.substring(content.indexOf('# Netscape HTTP Cookie File'));
fs.writeFileSync(COOKIES_FILE, content, 'utf8');
return true;
}
}
return false;
}
// ENDPOINT 1: /stream/:id
// Devuelve metadatos, miniaturas y la URL de streaming (directa o m3u8)
app.get('/stream/:id', (req, res) => {
sanitizeCookies();
const videoId = req.params.id;
const youtubeUrl = `https://www.youtube.com/watch?v=${videoId}`;
// Argumentos para obtener el JSON de metadatos
const args = [
'--cookies', COOKIES_FILE,
'--js-runtime', `node:${NODE_PATH}`,
'--dump-json',
'--format', 'bestvideo+bestaudio/best',
'--extractor-args', 'youtube:player_client=tv,web',
youtubeUrl
];
const child = spawn('yt-dlp', args);
let output = '';
child.stdout.on('data', (data) => output += data);
child.on('close', (code) => {
if (code !== 0) return res.status(500).json({ error: "Error al extraer stream" });
try {
const info = JSON.parse(output);
const response = {
video_id: videoId,
title: info.title,
description: info.description,
is_live: info.is_live || false,
stream_url: info.url,
hls_variant_url: info.manifest_url || "",
url_type: info.is_live ? "hls/m3u8" : "direct/mp4",
youtube_url: youtubeUrl,
thumbnails: [
`https://img.youtube.com/vi/${videoId}/maxresdefault.jpg`,
`https://img.youtube.com/vi/${videoId}/hqdefault.jpg`
],
ffmpeg_example: `ffmpeg -re -i "${info.url}" -c copy -f flv rtmp://destino/stream_key`
};
res.json(response);
} catch (e) {
res.status(500).json({ error: "Error parseando JSON de yt-dlp" });
}
});
});
// ENDPOINT 2: /transcript/:id
// Devuelve las miniaturas y el texto completo de la transcripción
app.get('/transcript/:id', (req, res) => {
sanitizeCookies();
const videoId = req.params.id;
const youtubeUrl = `https://www.youtube.com/watch?v=${videoId}`;
// Argumentos para extraer subtítulos sin descargar video
const args = [
'--cookies', COOKIES_FILE,
'--js-runtime', `node:${NODE_PATH}`,
'--write-auto-subs',
'--skip-download',
'--print', '%(subtitles)j', // Imprime el JSON de subtítulos
youtubeUrl
];
const child = spawn('yt-dlp', args);
let output = '';
child.stdout.on('data', (data) => output += data);
child.on('close', (code) => {
// Nota: yt-dlp devuelve las URLs de los subtítulos.
// Para obtener el "format_text" como tu JSON, se requiere procesar el .vtt o .ttml
// Aquí enviamos la estructura base solicitada.
res.json({
video_id: videoId,
thumbnails: [
`https://img.youtube.com/vi/${videoId}/maxresdefault.jpg`,
`https://img.youtube.com/vi/${videoId}/sddefault.jpg`
],
info: "Para transcripción completa procesada, use un parser de VTT sobre la URL de subtítulos de YouTube."
});
});
});
// --- INTERFAZ UX (MODO OSCURO / TAILWIND) ---
// Serve a built frontend from `dist` when available. During development run the Vite dev server.
const distPath = path.join(__dirname, 'dist');
if (fs.existsSync(distPath)) {
app.use(express.static(distPath));
app.get('/admin', (req, res) => res.sendFile(path.join(distPath, 'index.html')));
} else {
app.get('/admin', (req, res) => {
res.send(`
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Admin - Build missing</title>
</head>
<body style="background:#0f172a;color:#f8fafc;font-family:Inter,system-ui,sans-serif;display:flex;align-items:center;justify-content:center;height:100vh;">
<div style="text-align:center;max-width:640px;padding:24px;">
<h1>Frontend not built</h1>
<p>Run <code>npm run build</code> to build the admin frontend, or run <code>npm run dev</code> during development and open the Vite dev server.</p>
</div>
</body>
</html>
`);
});
}
// --- ENDPOINT: SUBIDA Y VALIDACIÓN ---
app.post('/upload-cookies', upload.single('cookies'), (req, res) => {
try {
if (!req.file) {
return res.status(400).json({ status: "error", message: "No se seleccionó archivo." });
}
const tempPath = req.file.path;
const content = fs.readFileSync(tempPath, 'utf8');
// Validación estricta del formato Netscape
if (!content.includes('# Netscape HTTP Cookie File')) {
fs.unlinkSync(tempPath); // Borramos el inválido
return res.status(400).json({
status: "error",
message: "Formato inválido. Debe ser un archivo de cookies Netscape."
});
}
// Move the validated upload into place atomically
try {
fs.copyFileSync(tempPath, COOKIES_PATH);
fs.unlinkSync(tempPath);
} catch (mvErr) {
// If copy or unlink fails, attempt a rename as fallback
try {
fs.renameSync(tempPath, COOKIES_PATH);
} catch (renameErr) {
return res.status(500).json({ status: "error", message: 'Error al guardar cookies: ' + (mvErr.message || renameErr.message) });
}
}
res.json({ status: "success", message: "cookies.txt actualizado." });
} catch (error) {
res.status(500).json({ status: "error", message: error.message });
}
});
/**
* Endpoint para forzar la actualización de cookies.txt
* Uso: http://localhost:3000/refresh-cookies?id=PzWTM7YvGag
*/
app.get('/refresh-cookies', async (req, res) => {
try {
await exportCookiesFromBotProfile();
res.json({
status: "success",
message: "Cookies sincronizadas y descifradas correctamente desde el perfil del bot."
});
} catch (error) {
res.status(500).json({ status: "error", details: error.message });
}
});
// --- INICIO DEL SERVIDOR ---
// Servir un favicon simple (1x1 PNG) para evitar 404 en el navegador
app.get('/favicon.ico', (req, res) => {
const img = Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR4nGNgYAAAAAMAAWgmWQ0AAAAASUVORK5CYII=', 'base64');
res.set('Content-Type', 'image/png');
res.send(img);
});
app.listen(PORT, () => {
console.log(`
🚀 API Enlace Directo News activa
------------------------------------------
Panel Admin: http://localhost:${PORT}/admin
------------------------------------------
`);
});