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?"