Salve ho scritto questo codice
Quando al mattino esce il nuovo video va correttamente in ultimo video pubblicato e la sezione ultima live dice nessuna live presente ma quando al pomeriggio c'è la live la sezione ultima live resta vuota e il ultimo video pubblicato viene sostituito dalla live dive sbaglio? Grazie millecodice:<!DOCTYPE html> <html lang="it"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Barza TV - Breaking News</title> </head> <body> <h1>Barza TV Breaking News</h1> <h2>Ultima Live:</h2> <div id="liveSection"> </div> <h2>Ultimo Video Pubblicato:</h2> <div id="videoSection"> </div> <script> // ID del canale YouTube const channelId = "UC5lq4z_x_22UuH-b7rLmYBw"; // Sostituisci con il tuo ID canale // URL del feed RSS const rssFeedUrl = `https://www.youtube.com/feeds/videos.xml?channel_id=${channelId}&t=${new Date().getTime()}`; async function loadYouTubeContent() { try { // Recupera i dati del feed RSS const response = await fetch(rssFeedUrl); if (!response.ok) throw new Error("Errore nel caricamento del feed RSS."); const text = await response.text(); const parser = new DOMParser(); const xml = parser.parseFromString(text, "application/xml"); const entries = xml.querySelectorAll("entry"); if (entries.length === 0) throw new Error("Nessun video trovato."); let liveVideo = null; let latestVideo = null; // Ordiniamo i video in base alla data (dal più recente) const sortedEntries = Array.from(entries).sort((a, b) => { const dateA = new Date(a.querySelector("published").textContent); const dateB = new Date(b.querySelector("published").textContent); return dateB - dateA; // Ordine decrescente }); // Analizziamo il feed for (const entry of sortedEntries) { const videoUrl = entry.querySelector("link")?.getAttribute("href"); const videoId = new URL(videoUrl).searchParams.get("v"); const liveStatus = entry.querySelector("yt\\:liveBroadcastContent")?.textContent || "none"; const publishedDate = new Date(entry.querySelector("published").textContent); // Se è una live attiva o imminente if (liveStatus === "live" || liveStatus === "upcoming") { // Memorizziamo la live più recente if (!liveVideo || publishedDate > liveVideo.date) { liveVideo = { url: videoUrl, id: videoId, date: publishedDate }; } } // Video pubblicato, escludiamo le live concluse else if (liveStatus === "none" && !videoUrl.includes("/live/")) { // Aggiorna l'ultimo video pubblicato se non è live if (!latestVideo || publishedDate > latestVideo.date) { latestVideo = { url: videoUrl, id: videoId, date: publishedDate }; } } } // Mostra la live più recente const liveSection = document.getElementById("liveSection"); if (liveVideo) { liveSection.innerHTML = ` <a href="${liveVideo.url}" target="_blank"> <iframe width="300" height="170" src="https://www.youtube.com/embed/${liveVideo.id}" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen> </iframe> </a> `; } else { liveSection.innerHTML = "<p>Nessuna live presente.</p>"; } // Mostra l'ultimo video pubblicato (che non è live) const videoSection = document.getElementById("videoSection"); if (latestVideo) { videoSection.innerHTML = ` <a href="${latestVideo.url}" target="_blank"> <iframe width="300" height="170" src="https://www.youtube.com/embed/${latestVideo.id}" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen> </iframe> </a> `; } else { videoSection.innerHTML = "<p>Nessun video trovato.</p>"; } } catch (error) { console.error(error); } } loadYouTubeContent(); </script> </body> </html>

Rispondi quotando



