0
votes

I'm building a script that should do the following:

  1. parse music data (artist-track as strings) from a spreadsheet
  2. search each track on YT using the .Search class of the data API
  3. get video ID of the best (most "relevant") result for each track searched and store it in a spreadsheet column
  4. create a playlist in my channel
  5. add each video from the search to the playlist

I'm having issues with step 5.

I have only intermediate knowledge of JS and the Google Apps Script library.

Here's my function:

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

The last for loop seems to be the problem: I get an error that just says "Required" without any other info...

Any help would be much appreciated!

1

1 Answers

1
votes

You just need to give the proper format to the PlaylistItem. Check this example:

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

Check if this works for you.