I currently have the code below to look for new YouTube video links (video IDs) in my spreadsheet. Is there any way I can search through the YouTube playlist that already exists and check its contents before adding a new video to that? I've seen various examples in PHP but none in Google Apps Script.
function addVideoToYouTubePlaylist() {
// Read the source videos from Google Sheet
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange().getValues();
var playlistId = "PL6bCFcS8yqQxSPjwZ9IXFMfVm6kaNGLfi";
// iterate through all rows in the sheet
for (var d=1,l=data.length; d<l; d++) {
// Add the video to the existing playlist
YouTube.PlaylistItems.insert({
snippet: {
playlistId: playlistId,
resourceId: {
kind: "youtube#video",
videoId: extractVideoID(data[d][0])
}
}
}, "snippet");
sheet.deleteRow(d+1);
// wait for a second to avoid hitting the rate limit
Utilities.sleep(1000);
}
}
function extractVideoID(url){
var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
var match = url.match(regExp);
if ( match && match[7].length == 11 ){
return match[7];
} else {
console.log("Could not extract video ID.");
var trimmedVideoID = url.replace("https://youtu.be/", "");
trimmedVideoID = trimmedVideoID.replace('https://www.youtube.com/watch?v=', "");
trimmedVideoID = trimmedVideoID.replace('https://youtube.com/watch?v=', "");
trimmedVideoID = trimmedVideoID.replace("&feature=youtu.be", "");
trimmedVideoID = trimmedVideoID.replace("&feature=share", "");
console.log(trimmedVideoID);
return trimmedVideoID;
}
}