Search code examples
google-apps-scriptyoutube-data-api

incomplete data from the fetch result using google data API


I want to retrieve the last 50 video data from a channel using appscript and google data API v3. the logger successfully shows 50 data from today to June. but many video data in November are not indexed.

Here's the script I made

var apiKey = 'my API';
var channelId = 'UCAoy6rzhSf4ydcYjJw3WoVg'; 

function fetchRecentVideos() {
  var apiUrl = 'https://www.googleapis.com/youtube/v3/search';

  var videos = [];

  var params = {
    'part': 'snippet',
    'channelId': channelId,
    'type': 'video',
    'maxResults': 50,
    'order': 'date', 
    'key': apiKey
  };

  var response = UrlFetchApp.fetch(apiUrl + '?' + encodeParams(params));
  var result = JSON.parse(response.getContentText());

  for (var i = 0; i < result.items.length; i++) {
    var video = result.items[i].snippet;
    Logger.log('Video Title:'+ video.title);
    Logger.log('Published Date:'+ video.publishedAt);
    Logger.log('--------------------');
  }
}

function encodeParams(params) {
  return Object.keys(params)
    .map(function (key) {
      return encodeURIComponent(key) + '=' + encodeURIComponent(params[key]);
    })
    .join('&');
}

Solution

  • I tried to add publishedAfter to the pharameter, so the output will come out all complete and in the order of the most recent. this doesn't work if using publishedBefore

    var params = {
        'part': 'snippet',
        'channelId': channelId,
        'type': 'video',
        'maxResults': 50,
        'order': 'date',
        'publishedAfter': '2010-01-01T00:00:00Z',
        'key': apiKey
      };