本文介绍了如何使用分页在YouTube API中的每次执行中获得不同的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过执行YouTube数据API中的搜索功能来获得新的结果.我需要的是在每次执行中获取新的结果作为输出,即没有重复的结果.我知道分页会做到这一点,但我不知道如何避免每次执行时重复结果.

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.

我正在使用Google Apps Script进行服务器端cron作业,因此无论何时执行执行,都需要缓存结果或避免发布已经发布的结果.

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.

这是我的两种类型的代码,一种是从我之前的问题之一中提取的,但是不能完全解决我的问题,因此我要求任何人解决问题. 函数searchByKeyword(nextPageToken){ var results = YouTube.Search.list('id,snippet',{ 问:狗", maxResults:1 pageToken:nextPageToken }); Logger.log(结果) 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

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.

推荐答案

这是分页脚本的脚本.我们需要包括一个数据库,因此请选择表作为您的数据库,由CodeRevolution的Kisded Szabi编码.

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;

}   
    
    
    
    
    
    

这篇关于如何使用分页在YouTube API中的每次执行中获得不同的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 04:03