I am trying to get new results with the execution of search function within the YouTube data API. What I need is to fetch new results as output in each execution ie no duplication of results. I know the pagination will do that but I don't know how to avoid the duplication of the results in each execution.
I am using Google Apps Script for server-side cron jobs so whenever the execution happens it needs to cache the results or avoid publishing the result already posted.
These are my two types of code, one is extracted from one of my earlier questions but that doesn't solve my issue completely so I request anyone to solve the issues. function searchByKeyword(nextPageToken) { var results = YouTube.Search.list('id,snippet', { q: 'dogs', maxResults: 1, pageToken: nextPageToken }); Logger.log(results) var item = results.items; var res = searchByKeyword( results.nextPageToken ); Logger.log(res) // for (var i = 0; i < results.items.length; i) { { var nextPageToken = results.nextPageToken
// var nextPageToken = '';
while (nextPageToken != null) {
var results
Logger.log('[%s] Title: %s', item.id.videoId, item.snippet.title);
}
}}
The code above prints so many results than my required results which I need to pass in each execution.
/**
* @file getting Videos from Youtube with IDs
*/
/* exported userActionRun */
/**
* User action. Runs the snippet
*/
function userActionRun() {
var data = [];
var res = searchByKeyword_('trailers');
while (res.items.length && data.length < 10) {
data = data.concat(res.items);
res = searchByKeyword_('trailers', res.nextPageToken);
}
Logger.log(data.length);
Logger.log(
'\n%s',
data
.map(function(item, i) {
return Utilities.formatString('%s. %s', i + 1, item.snippet.title);
})
.join('\n')
);
}
/**
* Returns YouTube search result
* @param {string} keyword
* @param {string} nextPageToken
* @returns {object}
*/
function searchByKeyword_(keyword, nextPageToken) {
var q = { q: keyword, maxResults: '1', type: 'video' };
if (nextPageToken) q.pageToken = nextPageToken;
var results = YouTube.Search.list('id,snippet', q);
return results;
}
This code prints results but same results in each execution.