Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save filipeleonelbatista/52c9104944b8a2600bf8d7f5a0aaae2a to your computer and use it in GitHub Desktop.
Save filipeleonelbatista/52c9104944b8a2600bf8d7f5a0aaae2a to your computer and use it in GitHub Desktop.
Component Last Video Posted On My Channel Using RSS Feed From Yourube. Comments in PT-BR
import { useEffect, useState } from "react";
import axios from "axios";
import xml2js from 'xml-js';
export default function LastVideo() {
const channelID = "<seu_id_de_canal_aqui>";
const youtubeRSS = `https://www.youtube.com/feeds/videos.xml?channel_id=${channelID}`;
const [lastVideoId, setLastVideoId] = useState("<id_video_padrao_ou_aspas_simples>");
useEffect(() => {
const executeAsync = async () => {
try {
const { data } = await axios.get(youtubeRSS);
const jsonData = xml2js.xml2json(data, { compact: true, spaces: 4 });
setLastVideoId(videoData.feed.entry[0]["yt:videoId"]._text);
} catch (error) {
console.error("Erro ao buscar dados do feed XML do YouTube:", error);
}
}
executeAsync();
}, [])
return (
<iframe
src={`https://www.youtube.com/embed/${lastVideoId}`}
title="YouTube video player"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowFullScreen
style={{
border: 0,
width: '100%'
}}
></iframe>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment