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

Using the Youtube API, can I see if a video is in a playlist without retrieving the entire playlist and looping through all the videos?

$
0
0

I have an array of Youtube videos, and I want to know if they are contained in a particular playlist. I have this code:

  var targetPlaylistId = "some_playlist_id";  var targetPlaylist = YouTube.Playlists.list("snippet", {id: targetPlaylistId});  var targetPlaylistTitle = targetPlaylist.items[0].snippet.title  var targetVideos = YouTube.PlaylistItems.list("snippet", {playlistId: targetPlaylistId, maxResults: 50});  for (var i = 0; i < sourceVideos.length; i++) {    var sourceVideoId = sourceVideos[i].snippet.resourceId.videoId;    var sourceVideoTitle = sourceVideos[i].snippet.title    var isAlreadyAdded = false;    for (var j = 0; j < targetVideos.items.length; j++) {      var targetVideoId = targetVideos.items[j].snippet.resourceId.videoId;      if (sourceVideoId == targetVideoId) {        isAlreadyAdded = true;        break;      }    }    if (!isAlreadyAdded) {      var resource = {        snippet: {          playlistId: targetPlaylistId,          resourceId: {            kind: "youtube#video",            videoId: sourceVideoId          }       }      };      YouTube.PlaylistItems.insert(resource, "snippet");      Logger.log("Added video " + sourceVideoTitle +" with ID " + sourceVideoId +" to playlist " + targetPlaylistTitle);    }  }

This works, but I have to loop over every video in targetPlayList for each one of the sourceVideos, which I don't want to do if targetPlaylist is long.

Is there a way to ask Youtube "is video X already in playlist Y?"


Viewing all articles
Browse latest Browse all 3831

Trending Articles



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