问题
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.
回答1:
Here is the script for the pagination script .we need to include a DB so choose sheet as your DB coded by Kisded Szabi from CodeRevolution.
function doGet() {
return HtmlService.createHtmlOutputFromFile('v5')
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
}
function userActionRun(myForm) {
var ss = SpreadsheetApp.openById('');
var CLIENT_ID = ""
var CLIENT_SECRET = ""
var sheet = ss.getSheetByName('Sheet1');
var data = [];
var nextPageData = sheet.getRange(sheet.getLastRow(), 1, 1,3).getValues();
if(nextPageData[0][0] !== undefined)
{
var res = searchByKeyword_(values1, nextPageData[0][0]);
}
else
{
var res = searchByKeyword_(values1);
}
for(var i in res.items)
{
var item = res.items[i];
getFullDescr_(item.id.videoId);
}
if(typeof res.nextPageToken !== 'undefined')
{
sheet.getRange(sheet.getLastRow(), 1, 1,3).setValues([[res.nextPageToken, '1', '2']]);
}
data = data.concat(res.items);
Logger.log("We got this after initial call: " + data.length);
var maximumItems = 1;
while (typeof res.nextPageToken !== 'undefined' && res.items.length) {
sheet.getRange(sheet.getLastRow(), 1, 1,3).setValues([[res.nextPageToken, '1', '2']]);
if(data.length >= maximumItems)
{
Logger.log("Max number reached, bye! " + data.length);
break;
}
res = searchByKeyword_(values1, res.nextPageToken);
for(var i in res.items)
{
var item = res.items[i];
getFullDescr_(item.id.videoId);
}
data = data.concat(res.items);
}
for (i = 0; i < data.length; i++) {
Logger.log("Our result: " + data[i].snippet.title);
}
}
function getFullDescr_(videoId)
{
var results = YouTube.Videos.list("id,snippet",{'id': videoId }); // here passing that id for a full description //
for(var i in results.items) {
var item = results.items[i];
Logger.log('%s Description: %s',item.snippet.title, item.snippet.description);
}
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;
}
来源:https://stackoverflow.com/questions/54821144/how-to-get-different-results-in-each-execution-in-youtube-api-using-pagination