【问题标题】:Youtube API - How to limit results for pagination?Youtube API - 如何限制分页结果?
【发布时间】:2010-03-15 16:38:24
【问题描述】:

我想抓取用户的上传内容(即:BBC)并将输出限制为每页 10 个。

虽然我可以使用以下网址: http://gdata.youtube.com/feeds/api/users/bbc/uploads/?start-index=1&max-results=10

上面的工作正常。

我想改用查询方法:

Zend 框架文档: http://framework.zend.com/manual/en/zend.gdata.youtube.html

声明我可以检索用户上传的视频,但理想情况下我想使用查询方法来限制分页的结果。

查询方法在 Zend 框架文档上(与之前标题“按元数据搜索视频”相同的页面),类似于:

$yt = new Zend_Gdata_YouTube(); $query = $yt->newVideoQuery(); $query->setTime('today'); $query->setMaxResults(10); $videoFeed = $yt->getUserUploads( NULL, $query );

print '<ol>'; foreach($videoFeed as $video): print '<li>' . $video->title . '</li>'; endforeach; print '</ol>';

问题是我不能做 $query->setUser('bbc')。

我尝试了 setAuthor 但这会返回完全不同的结果。

理想情况下,我想使用查询方法以分页方式获取结果。

如何使用 $query 方法设置分页限制?

谢谢。

【问题讨论】:

    标签: zend-framework youtube-api zend-gdata


    【解决方案1】:

    我决定只使用用户上传的提要作为使分页起作用的一种方式。 http://gdata.youtube.com/feeds/api/users/bbc/uploads/?start-index=1&max-results=10

    如果有一种方法可以使用查询/搜索方法来完成类似的工作将会很有趣。

    【讨论】:

      【解决方案2】:

      我基本上以与 worchyld 相同的方式解决了这个问题,但略有不同:

          $username = 'ignite';
          $limit = 30;  // Youtube will throw an exception if > 50
          $offset = 1;  // First video is 1 (silly non-programmers!)
          $videoFeed = null;
          $uploadCount = 0;
          try {
              $yt = new Zend_Gdata_YouTube();
              $yt->setMajorProtocolVersion(2);
              $userProfile = $yt->getUserProfile($username);
              $uploadCount = $userProfile->getFeedLink('http://gdata.youtube.com/schemas/2007#user.uploads')->countHint;
      
              // The following code is a dirty hack to get pagination with the YouTube API without always starting from the first result
              // The following code snippet was copied from Zend_Gdata_YouTube->getUserUploads();
              $url = Zend_Gdata_YouTube::USER_URI .'/'. $username .'/'. Zend_Gdata_YouTube::UPLOADS_URI_SUFFIX;
              $location = new Zend_Gdata_YouTube_VideoQuery($url);
              $location->setStartIndex($offset);
              $location->setMaxResults($limit);
              $videoFeed = $yt->getVideoFeed($location);
          } catch (Exception $e) {
              // Exception handling goes here!
              return;
          }
      

      Zend YouTube API 看起来很傻,因为包含的 getUserUploads 方法在实际获取提要之前从不返回 VideoQuery 实例,虽然您可以将位置对象作为第二个参数传递,但这是一种“非此即彼”的情况 - 它'将仅使用用户名参数来构造基本 uri 或仅使用位置,您必须自己构造整个东西(如上)。

      【讨论】:

        猜你喜欢
        • 2020-03-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-10
        • 1970-01-01
        • 2017-03-27
        • 2015-08-11
        相关资源
        最近更新 更多