使用 Apps 脚本将播放列表添加到 YouTube 频道

Add Playlist to YouTube Channel using Apps Script

我正在构建一个应该执行以下操作的脚本:

  1. 从电子表格中解析音乐数据(艺术家曲目作为字符串)
  2. 使用数据 API
  3. 的 .Search class 在 YT 上搜索每个曲目
  4. 获取搜索到的每个曲目的最佳(最多 "relevant")结果的视频 ID,并将其存储在电子表格列中
  5. 在我的频道中创建一个播放列表
  6. 将搜索到的每个视频添加到播放列表

我在执行第 5 步时遇到问题。

我只有 JS 和 Google Apps 脚本库的中级知识。

这是我的函数:

// GLOBAL VARIABLES
var ss = SpreadsheetApp.getActiveSpreadsheet();
var source = ss.getSheets()[0];
var sourceDataRange = source.getDataRange();
var sourceValues = sourceDataRange.getValues();
var sourceRows = sourceDataRange.getNumRows();
var sourceColumns = sourceDataRange.getNumColumns();
var now = new Date();
    var nowYear = now.getYear();
    var nowMonth = now.getMonth() + 1;
    var nowDay = now.getDate();
var templateURL = 'https://www.youtube.com/watch?v=';
var vidId = [];

// FUNCTION DEFINITION

function youtubeSearchAndCreate() {
  youtubeSearch();
  createAndInsert();
}

function youtubeSearch() {

  // create new sheet from import copy
  var newSheetName = 'log_' + nowYear + '_' + nowMonth + '_' + nowDay; // names new sheet dynamically with today's date
  var newSheet = ss.insertSheet(newSheetName, 1);

  ss.getDataRange().offset(0, 0, sourceRows, sourceColumns).setValues(sourceValues);

  var artistsCol = 3;
  var artists = newSheet.getRange(2, artistsCol, sourceRows).getValues();
  var tracksCol = 4;
  var tracks = newSheet.getRange(2, tracksCol, sourceRows).getValues();
  var resultsCol = 1 + sourceColumns;

  newSheet.getRange(1, resultsCol).setValue('video_URL'); // set new search results column title

  var searchQueries = []; // search queries container array
  // var vidId = []; 
  var resultItems = [];

  for (var i=0; i<=sourceRows-2; i++) {

    searchQueries[i] = artists[i] + " - " + tracks[i]; // search query array generation

    var results = YouTube.Search.list('id', {q: searchQueries[i], maxResults: 1, type: 'video'}); // YouTube API searching for the most relevant video
    resultItems.push(results.items[0]);
    vidId.push(resultItems[i].id.videoId);
    newSheet.getRange(i+2, resultsCol).setValue(templateURL + vidId[i]); // insert each video URL into search results column
  }
}

function createAndInsert() {
  // create a new playlist and name it accordingly
  var newPlaylist = YouTube.Playlists.insert(
    {
      snippet: {
        title: nowYear + '-' + nowMonth + '-' + nowDay + ' TripleJ Hitlist',
        description: 'A playlist created with the YouTube API from ' + sourceRows - 1 + ' songs in the TripleJ Hitlist.'
      },
      status: {
        privacyStatus: 'private'
      }
    },
    'snippet,status'
  );

  // insert videos in the playlist
  var videosInserted = [];
  for (var i=0; i<vidId.length; i++) {
    // insert videos in the playlist according to their video ID gathered by the search script
    var insertVideo = YouTube.PlaylistItems.insert(
      {
        snippet: {
          playlistId: newPlaylist.id,
          resourceId: vidId[i]
        }
      },
      'snippet'
    );
    videosInserted.push(insertVideo.snippet);
  }
  Logger.log(videosInserted);
}

最后一个 for 循环似乎是问题所在:我得到一个错误,只显示 "Required",没有任何其他信息...

如有任何帮助,我们将不胜感激!

您只需为 PlaylistItem 提供正确的格式。检查这个例子:

var details = {
    videoId: vidId[i],
    kind: 'youtube#video'
  }

 var part= 'snippet';
    var resource = {
      snippet: {
        playlistId: newPlaylist.id,
        resourceId: details
     }
    };

    var insertVideo = YouTube.PlaylistItems.insert(resource, part);

检查这是否适合您。