Quantcast
Channel: Active questions tagged youtube-api - Stack Overflow
Viewing all articles
Browse latest Browse all 3831

How to decrease the daily api quota usage for this code?

$
0
0

I wrote this code with the help of chatGPT. It embeds the latest live stream from a youtube channel and if nothing is live it embeds the latest Video from the channel instead.I used the YouTube Data API v3 but the daily quota for the API is used up quickly when attempting to fetch live streams, making it impractical. Is there any way to minimize daily API quota usage, or are there alternative methods for live streaming that might be more effective?

<!DOCTYPE html><html><head><title>YouTube Live Stream</title></head><body><!-- YouTube Live Video Embed Container --><div id="youtube-live-embed"></div><!-- YouTube API Script --><script>    // YouTube API Configuration    var apiKey = 'APIKEY';    var channelId = 'CHANNELID';    var videoPlayer;    var cachedLiveVideoId = null;    var cachedLatestVideoId = null;    var requestInProgress = false; // Flag to prevent concurrent API requests    function onYouTubeIframeAPIReady() {      videoPlayer = new YT.Player('youtube-live-embed', {        height: '450',        width: '700',        playerVars: {'autoplay': 1,'controls': 0,'showinfo': 0,'rel': 0,        },        events: {'onReady': onPlayerReady,'onStateChange': onPlayerStateChange        }      });    }    function onPlayerReady(event) {      checkForLiveStream();    }    function onPlayerStateChange(event) {      if (event.data === YT.PlayerState.ENDED) {        checkForLiveStream();      }    }    function checkForLiveStream() {      if (cachedLiveVideoId) {        videoPlayer.loadVideoById(cachedLiveVideoId);        return;      }      if (requestInProgress) {        return; // Prevent concurrent requests      }      requestInProgress = true;      fetchLiveStream()        .then(videoId => {          if (videoId) {            cachedLiveVideoId = videoId;            videoPlayer.loadVideoById(videoId);          } else {            // No live stream found; play the latest video on the channel            fetchLatestVideo()              .then(videoId => {                if (videoId) {                  cachedLatestVideoId = videoId;                  videoPlayer.loadVideoById(videoId);                } else {                  console.log('No videos found on the channel.');                }              })              .catch(error => console.error('Error fetching latest video:', error))              .finally(() => {                requestInProgress = false;              });          }        })        .catch(error => console.error('Error checking for live stream:', error))        .finally(() => {          requestInProgress = false;        });    }    async function fetchLiveStream() {      try {        const response = await fetch(`https://www.googleapis.com/youtube/v3/search?key=${apiKey}&channelId=${channelId}&eventType=live&type=video`);        const data = await response.json();        return data.items.length > 0 ? data.items[0].id.videoId : null;      } catch (error) {        console.error('Error fetching live stream:', error);        return null;      }    }    async function fetchLatestVideo() {      try {        const response = await fetch(`https://www.googleapis.com/youtube/v3/search?key=${apiKey}&channelId=${channelId}&order=date&maxResults=1&type=video`);        const data = await response.json();        return data.items.length > 0 ? data.items[0].id.videoId : null;      } catch (error) {        console.error('Error fetching latest video:', error);        return null;      }    }</script><!-- YouTube Iframe API Script --><script src="https://www.youtube.com/iframe_api"></script></body></html>

I discovered a form to extend daily quota usage, but I have no idea how much more quota I need (or don't need).


Viewing all articles
Browse latest Browse all 3831

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>