使用 Google Apps 脚本检查播放列表中是否存在 YouTube 视频

Checking if YouTube Video exists in playlist with Google Apps Script

我目前有以下代码可以在我的电子表格中查找新的 YouTube 视频链接(视频 ID)。在添加新视频之前,有什么方法可以搜索已经存在的 YouTube 播放列表并检查其内容吗?我在 PHP 中看到了各种示例,但在 Google Apps 脚本中看到了 none。

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;
    }
}

我相信你的目标如下。

  • 您想通过检查播放列表添加视频 ID。
  • 您想从 Google Spreadsheet 上活动 sheet 的“A”列中检索额外的视频 ID。
  • 当播放列表中存在视频 ID 时,您不想添加它们。
  • 当播放列表中不存在视频 ID 时,您想添加它们。

修改点:

  • 在这种情况下,我认为首先需要从播放列表中检索列表。然后,当播放列表中不存在附加视频ID时,添加视频ID。

当以上几点反映到你的脚本中,就会变成下面这样。

修改后的脚本:

function addVideoToYouTubePlaylist() {
  // 1. Retrieve list from the playlist.
  var playlistId = "PL6bCFcS8yqQxSPjwZ9IXFMfVm6kaNGLfi";
  var list = [];
  var pageToken = "";
  do {
    var res = YouTube.PlaylistItems.list(["snippet"], {playlistId: playlistId, maxResults: 50, pageToken: pageToken});
    if (res.items.length > 0) list = list.concat(res.items);
    pageToken = res.nextPageToken || "";
  } while (pageToken);
  var obj = list.reduce((o, e) => Object.assign(o, {[e.snippet.resourceId.videoId]: e.snippet.resourceId.videoId}), {});

  // 2. Retrieve URLs like `https://www.youtube.com/watch?v=###` from the column "A" of the active sheet.
  var sheet = SpreadsheetApp.getActiveSheet();
  var data = sheet.getDataRange().getValues();

  // 3. Check whether the additional video ID is existing in the playlist and add it.
  data.forEach(([a]) => {
    var videoId = extractVideoID(a);
    if (videoId && !obj[videoId]) {  // <--- Modified
      YouTube.PlaylistItems.insert({
        snippet: {
          playlistId: playlistId,
          resourceId: {
            kind: "youtube#video",
            videoId: videoId
          }
        }
      }, "snippet");
    }
  });
}

function extractVideoID(url){
    var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
    var match = url.match(regExp);
    if (!match) return;  // <--- Added
    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;
    }
}

注:

  • 如果您有很多额外的视频 ID,使用批量请求可能会有用。 Ref

参考文献: