DIY Youtube Shuffler only loads 200 videos

Viewed 30

So I'm making a DIY Youtube Shuffler because apparently Youtube can't shuffle my playlists properly. I have no experience in HTMl or APIs or anything, but I stole enough code from here to make it work. But, it maxes out at 200 videos even though my playlist is a lot longer than that (around 2500). The code is below. I know this is a limitation with the Youtube API, but is there any workaround?

<html lang="en">
  <head>
    <title>YouTube Shuffle!</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="icon" href="https://www.youtube.com/s/desktop/94d44772/img/favicon_144x144.png" sizes="144x144">
    <style>
      body, html { margin: 0; padding: 0; }
    </style>
  </head>  
  <body>
 <div id="player"></div>
    <script>
        // 2. This code loads the IFrame Player API code asynchronously.
        var tag = document.createElement('script');

        tag.src = "https://www.youtube.com/iframe_api";
        var firstScriptTag = document.getElementsByTagName('script')[0];
        firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

        // 3. This function creates an <iframe> (and YouTube player)
        //    after the API code downloads.
        function onYouTubeIframeAPIReady() {
            var numPl = Math.floor((Math.random() * 50) + 1);
            var player = new YT.Player("player", {
                height: '390',
                width: '640',
                playerVars: {
                    listType:'playlist',
                    list:'PL0mcz16a5pG0evLcpesj7g7CcFpwRhfke',
                    index: numPl,
                    autoplay: 1,
    },
                events: {
                    'onReady': function (event) {
                        //event.target.cuePlaylist({list: "PL0mcz16a5pG0evLcpesj7g7CcFpwRhfke"});
                        //event.target.playVideo();
                        setTimeout(function() {
                            event.target.setShuffle({'shufflePlaylist' : true});
                        }, 1000);
                    }
                }
            });
        }
    </script>
  </body>
</html>
1 Answers

NOTE: The 200 limit YouTube has is - as far as I know - undocumented, but, it exists. If you really need to retrieve all the videos on a playlist, you have to make the requests using YouTube Data API and get every video on the said playlist, then, you have to use more code for create your own video player.

If you're fine with YouTube's generated shuffled playlist, then, change these lines from:

setTimeout(function() {
    event.target.setShuffle({'shufflePlaylist' : true});
}, 1000);

To:

event.target.setShuffle(true);

With this change, the API will return the playlist shuffled - again, only the 200 shuffled videos.


Full code:

// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');

tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

// 3. This function creates an <iframe> (and YouTube player)
//    after the API code downloads.
function onYouTubeIframeAPIReady() {
  var numPl = Math.floor((Math.random() * 50) + 1);
  var player = new YT.Player("player", {
    height: '390',
    width: '640',
    playerVars: {
      listType: 'playlist',
      list: 'PL0mcz16a5pG0evLcpesj7g7CcFpwRhfke',
      index: numPl,
      autoplay: 1,
    },
    muted: 1,
    events: {
      'onReady': function(event) {
        // Change made here: 
        event.target.setShuffle(true);
      }
    }
  });
}
<div id="player"></div>
<!--

First video returned: 
https://youtu.be/UE2vC5v8xLE?list=PL0mcz16a5pG0evLcpesj7g7CcFpwRhfke
posición: 101

Next video: 
https://youtu.be/XCyKJD6uQyg?list=PL0mcz16a5pG0evLcpesj7g7CcFpwRhfke
posición: 110

Next video: 
https://youtu.be/tOKdQ0-2ky4?list=PL0mcz16a5pG0evLcpesj7g7CcFpwRhfke
posición: 26

Next video: 
https://youtu.be/MeljgyLR2D0?list=PL0mcz16a5pG0evLcpesj7g7CcFpwRhfke
posición: 21

↑ You see there the playlist returned the playlist shuffled.

https://youtu.be/a0w-UJkTVFU?list=PL0mcz16a5pG0evLcpesj7g7CcFpwRhfke
> Video no disponible at my country - the player stops and you have to reload the page for reload the shuffled playlist (also, you can search how to skip non-aavilable videos on a YouTube playlist).

https://youtu.be/1FiK9AJHk3k?list=PL0mcz16a5pG0evLcpesj7g7CcFpwRhfke
posición: 19

https://youtu.be/gY_sEitPgpM?list=PL0mcz16a5pG0evLcpesj7g7CcFpwRhfke
posición: 173

https://youtu.be/ZzZ1qmXZBuY?list=PL0mcz16a5pG0evLcpesj7g7CcFpwRhfke
posición: 136

-->

Related