const express = require('express'); const fs = require('fs'); const path = require('path'); const https = require('https'); const { execSync } = require('child_process'); const app = express(); const PORT = process.env.PORT || 3000; const VIDEOS_FILE = path.join(__dirname, 'videos.json'); const MIDNIGHT_VIDEO = 'https://youtu.be/I88qRwVirTo?si=saUHc-HWvuFQyAzS'; // YouTube Cookies from environment variable const ytCookies = process.env.yt_cookies || ''; // Start time of the server to calculate offsets let serverStartTime = Date.now(); let forceTickerUntil = 0; function getYouTubeMetadata(url) { let cookieFile = null; try { let cookieOption = ''; if (ytCookies) { if (ytCookies.startsWith('# Netscape') || ytCookies.length > 100) { // It looks like cookie content, write to a temp file cookieFile = path.join(__dirname, 'temp_cookies.txt'); fs.writeFileSync(cookieFile, ytCookies); cookieOption = `--cookies "${cookieFile}"`; } else if (ytCookies.includes('/') || ytCookies.includes('\\') || ytCookies.endsWith('.txt')) { // It looks like a file path cookieOption = `--cookies "${ytCookies}"`; } else { // It looks like a browser name cookieOption = `--cookies-from-browser ${ytCookies}`; } } const cmd = `yt-dlp --get-title --get-duration --get-id -j ${cookieOption} "${url}"`; const output = execSync(cmd, { encoding: 'utf-8' }); const data = JSON.parse(output); // Convert duration to seconds let seconds = 0; if (data.duration) { seconds = data.duration; } else if (data.duration_string) { const parts = data.duration_string.split(':').map(p => parseInt(p)); if (parts.length === 3) seconds = parts[0] * 3600 + parts[1] * 60 + parts[2]; else if (parts.length === 2) seconds = parts[0] * 60 + parts[1]; else seconds = parts[0]; } return { title: data.title, id: data.id, duration: seconds || 0, url: `https://www.youtube.com/watch?v=${data.id}` }; } catch (err) { console.error(`Error fetching YT metadata for ${url}:`, err.message); return null; } finally { if (cookieFile && fs.existsSync(cookieFile)) { try { fs.unlinkSync(cookieFile); } catch (e) {} } } } let cachedPlaylist = []; let lastPlaylistUpdate = 0; function getPlaylist() { // Cache playlist metadata for 1 hour to avoid excessive yt-dlp calls if (Date.now() - lastPlaylistUpdate < 3600000 && cachedPlaylist.length > 0) { return cachedPlaylist; } try { const content = fs.readFileSync(VIDEOS_FILE, 'utf-8'); const videos = JSON.parse(content); const resolved = []; for (const v of videos) { // If duration is already provided in JSON, use it and skip yt-dlp if (v.duration) { // Extract ID from URL if not provided const idMatch = v.url.match(/(?:v=|\/)([a-zA-Z0-9_-]{11})/); const id = v.id || (idMatch ? idMatch[1] : null); if (id) { resolved.push({ title: v.title, id: id, duration: v.duration, url: v.url }); continue; } } const meta = getYouTubeMetadata(v.url); if (meta) resolved.push(meta); } cachedPlaylist = resolved; lastPlaylistUpdate = Date.now(); return resolved; } catch (err) { console.error('Error reading videos.json:', err); return []; } } // Special case for midnight video let midnightVideoMeta = null; function getMidnightVideo() { if (!midnightVideoMeta) { midnightVideoMeta = getYouTubeMetadata(MIDNIGHT_VIDEO); // Hardcoded Fallback if yt-dlp fails due to cookies/bot detection if (!midnightVideoMeta) { midnightVideoMeta = { title: "European Anthem - Special Midnight Broadcast", id: "I88qRwVirTo", duration: 219, url: MIDNIGHT_VIDEO }; } } return midnightVideoMeta; } // Improved News Fetcher (Euronews + Fallback) async function fetchNews() { const feeds = [ 'https://www.euronews.com/rss?level=vertical&name=news', 'https://news.google.com/rss?hl=es-ES&gl=ES&ceid=ES:es' ]; for (const feed of feeds) { try { const res = await fetch(feed, { headers: { 'User-Agent': 'Mozilla/5.0' } }); if (!res.ok) continue; const data = await res.text(); // Extract everything between