List all YouTube videos in a Playlist with Google AppScript

The following function helps you to retrieve information of all videos present in a YouTube playlist. Visit YouTube Playlist API documentation for more details about the parameters and API usage.

function youtube_playlist() {

  const PLAYLIST_ID = "PL4o29bINVT4EG_y-k5jGoOu3-Am8Nvi10";
  const MAX_RESULTS = 50;

  let res = YouTube.PlaylistItems.list('snippet',{
    playlistId: PLAYLIST_ID,
    maxResults: MAX_RESULTS,
  });
  
  let items = res.items, videos = [];

  while('nextPageToken' in res) {
    res = YouTube.PlaylistItems.list('snippet',{
      playlistId: PLAYLIST_ID,
      maxResults: MAX_RESULTS,
      pageToken: res.nextPageToken,
    });
    items.concat(res.items);
  }

// Iterate the responses and store only the relevant information
  items.map(item => {
    videos.push({
      title: item.snippet.title,
      videoId: item.snippet.resourceId.videoId,
    })
  })
  console.log(videos)
}

/*
Prints
[ { title: 'Maroon 5 - Memories (Official Video)', videoId: 'SlPhMPnQ58k' },
{ title: 'Justin Bieber - Peaches ft. Daniel Caesar, Giveon',     videoId: 'tQ0yjYUFKAE' },
{ title: 'Tiësto - The Business (Official Music Video)', videoId: 'nCg3ufihKyU' },
...
]

*/
Exit mobile version