【问题标题】:YouTube API nextPageTokenYouTube API nextPageToken
【发布时间】:2017-11-29 02:11:06
【问题描述】:

我对 javascript 还很陌生,而且我有点坚持使用 youtube API。我能够使用我需要的数据获得结果,遍历每个数据并显示它们,但问题出在 nextPageToken 上。我似乎无法弄清楚为什么在使用 nextPage 令牌的第二个搜索功能之后,我无法丢弃旧的,因此我得到了多个重复的结果。

我正在记录令牌以查看和跟踪它们,但我总是得到以前的令牌重复

<body>

 <div class="youtube-feed container">
  <script id="template" type=test/template>
    <div class="youtube-item">
        <a href="{{link}}" class="link">
            <img src="{{thumb}}" alt="">
        </a>
        <div class="info">
            <h6 class="title">{{title}}</h6>
            <p>{{channel}}</p>
            <p>{{views}}</p>
        </div>
    </div>
  </script>
</div>

<div class="button container">
 <a href="#" id="next">Next Page</a>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

<script>
 (function() {
    var query = 'Random search string',
        apiKey = 'api key here';

    getData();

    function getData() {
        $.get('https://www.googleapis.com/youtube/v3/search', {
            part: 'snippet, id',
            q: query,
            type: 'video',
            maxResults: 10,
            key: apiKey},
        function(data) {
            var nextToken = data.nextPageToken;
            $.each(data.items, function(i, item) {
                var resultsData = {
                    id: item.id.videoId,
                    title: item.snippet.title,
                    desc: item.snippet.description,
                    thumb: item.snippet.thumbnails.medium.url,
                    channel: item.snippet.channelTitle
                };

                $.get('https://www.googleapis.com/youtube/v3/videos', {
                    part: 'statistics',
                    id: resultsData.id,
                    key: apiKey},
                function(data) {
                    $.each(data.items, function(i, item) {
                        var views = item.statistics.viewCount;
                            resultsData.viewCount = views;
                    });
                    renderData(resultsData);
                });
            });
            nextButton(nextToken);
        });
    };

    function renderData(obj) {
        var template = $.trim($('#template').html()),
            dataVals = template.replace(/{{id}}/ig, obj.id)
                                .replace(/{{title}}/ig, obj.title)
                                .replace(/{{thumb}}/ig, obj.thumb)
                                .replace(/{{channel}}/ig, obj.channel)
                                .replace(/{{views}}/ig, obj.viewCount)
                                .replace(/{{link}}/ig, 'https://www.youtube/com/embed/' + obj.id);
        $(dataVals).appendTo('.youtube-feed');
    };

    function nextButton(token) {
        $('#next').on('click', function(e) {
            e.preventDefault();
            nextPage(token);
        });
    };

    function nextPage(token) {
        $.get('https://www.googleapis.com/youtube/v3/search', {
            part: 'snippet, id',
            q: query,
            type: 'video',
            maxResults: 2,
            pageToken: token,
            key: apiKey},
        function(data) {
            var nextToken = data.nextPageToken;
            $.each(data.items, function(i, item) {
                var resultsData = {
                    id: item.id.videoId,
                    title: item.snippet.title,
                    desc: item.snippet.description,
                    thumb: item.snippet.thumbnails.medium.url,
                    channel: item.snippet.channelTitle
                };

                $.get('https://www.googleapis.com/youtube/v3/videos', {
                    part: 'statistics',
                    id: resultsData.id,
                    key: apiKey},
                function(data) {
                    $.each(data.items, function(i, item) {
                        var views = item.statistics.viewCount;
                        resultsData.viewCount = views;
                    });
                    renderData(resultsData);
                });
            });
            nextButton(nextToken);
            console.log(nextToken);
        });
    };
 })();
</script>

</body>

【问题讨论】:

    标签: javascript jquery html youtube-api youtube-javascript-api


    【解决方案1】:

    您需要在添加新的单击事件处理程序之前删除您的单击事件处理程序,否则它们会加起来,请参阅this post

    $('#next').off('click').on('click', function(e) {
        e.preventDefault();
        nextPage(token);
    });
    

    这是一个完整的例子,它也处理最后一页(当没有pageToken 字段时):

    <body>
    
      <div class="youtube-feed container">
        <script id="template" type=test/template>
          <div class="youtube-item">
            <a href="{{link}}" class="link">
              <img src="{{thumb}}" alt="">
            </a>
            <div class="info">
              <h6 class="title">{{title}}</h6>
              <p>{{channel}}</p>
              <p>{{views}}</p>
            </div>
          </div>
        </script>
      </div>
    
      <div class="button container">
        <a href="#" id="next">Next Page</a>
      </div>
    
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    
      <script>
        (function() {
          var query = 'TANNOY dvs 6',
            apiKey = 'YOUR_API_KEY';
    
          var reachLastPage = false;
    
          getData();
    
          function getData() {
            $.get('https://www.googleapis.com/youtube/v3/search', {
                part: 'snippet, id',
                q: query,
                type: 'video',
                maxResults: 10,
                key: apiKey
              },
              function(data) {
    
                $.each(data.items, function(i, item) {
                  var resultsData = {
                    id: item.id.videoId,
                    title: item.snippet.title,
                    desc: item.snippet.description,
                    thumb: item.snippet.thumbnails.medium.url,
                    channel: item.snippet.channelTitle
                  };
    
                  $.get('https://www.googleapis.com/youtube/v3/videos', {
                      part: 'statistics',
                      id: resultsData.id,
                      key: apiKey
                    },
                    function(data) {
                      $.each(data.items, function(i, item) {
                        var views = item.statistics.viewCount;
                        resultsData.viewCount = views;
                      });
                      renderData(resultsData);
                    });
                });
    
                if (data.nextPageToken) {
                  console.log("go to token : " + data.nextPageToken);
                  nextButton(data.nextPageToken);
                } else {
                  console.log("no page left");
                  reachLastPage = true;
                }
              });
          };
    
          function renderData(obj) {
            var template = $.trim($('#template').html()),
              dataVals = template.replace(/{{id}}/ig, obj.id)
              .replace(/{{title}}/ig, obj.title)
              .replace(/{{thumb}}/ig, obj.thumb)
              .replace(/{{channel}}/ig, obj.channel)
              .replace(/{{views}}/ig, obj.viewCount)
              .replace(/{{link}}/ig, 'https://www.youtube/com/embed/' + obj.id);
            $(dataVals).appendTo('.youtube-feed');
          };
    
          function nextButton(token) {
            $('#next').off('click').on('click', function(e) {
              e.preventDefault();
              if (!reachLastPage) {
                nextPage(token);
              } else {
                console.log("we already have reached last page!");
              }
            });
          };
    
          function nextPage(token) {
            $.get('https://www.googleapis.com/youtube/v3/search', {
                part: 'snippet, id',
                q: query,
                type: 'video',
                maxResults: 2,
                pageToken: token,
                key: apiKey
              },
              function(data) {
    
                $.each(data.items, function(i, item) {
                  var resultsData = {
                    id: item.id.videoId,
                    title: item.snippet.title,
                    desc: item.snippet.description,
                    thumb: item.snippet.thumbnails.medium.url,
                    channel: item.snippet.channelTitle
                  };
    
                  $.get('https://www.googleapis.com/youtube/v3/videos', {
                      part: 'statistics',
                      id: resultsData.id,
                      key: apiKey
                    },
                    function(data) {
                      $.each(data.items, function(i, item) {
                        var views = item.statistics.viewCount;
                        resultsData.viewCount = views;
                      });
                      renderData(resultsData);
                    });
                });
                if (data.nextPageToken) {
                  console.log("go to token : " + data.nextPageToken);
                  nextButton(data.nextPageToken);
                } else {
                  console.log("no page left");
                  reachLastPage = true;
                }
              });
          };
        })();
      </script>
    
    </body>

    【讨论】:

    • .off() 方法是我之前不知道的解决方案。谢谢!它现在可以工作了。
    猜你喜欢
    • 2018-08-01
    • 2019-08-05
    • 2021-10-16
    • 1970-01-01
    • 2021-07-20
    • 2019-05-13
    • 2018-12-13
    • 1970-01-01
    • 2018-08-28
    相关资源
    最近更新 更多