如何从Youtube视频源获得超过999个结果?

时间:2013-01-28 20:51:04

标签: java video youtube youtube-api feeds

我现在正在撰写关于长尾的单一解释,并希望获取数据来研究它的行为。这就是为什么我想要检索有关YouTube视频的视图的信息。唯一的问题是,像“most_popular”这样的特定主题的一个视频源只有999个条目。有没有办法检索更多数据到特定类别或一般?我将发布我当前的代码(这是尝试检索“sports”类别的数据):

public static void printVideoEntry(VideoEntry videoEntry, boolean detailed) {
      System.out.println("Title: " + videoEntry.getTitle().getPlainText());

      if(videoEntry.isDraft()) {
        System.out.println("Video is not live");
        YtPublicationState pubState = videoEntry.getPublicationState();
        if(pubState.getState() == YtPublicationState.State.PROCESSING) {
          System.out.println("Video is still being processed.");
        }
        else if(pubState.getState() == YtPublicationState.State.REJECTED) {
          System.out.print("Video has been rejected because: ");
          System.out.println(pubState.getDescription());
          System.out.print("For help visit: ");
          System.out.println(pubState.getHelpUrl());
        }
        else if(pubState.getState() == YtPublicationState.State.FAILED) {
          System.out.print("Video failed uploading because: ");
          System.out.println(pubState.getDescription());
          System.out.print("For help visit: ");
          System.out.println(pubState.getHelpUrl());
        }
      }

      if(videoEntry.getEditLink() != null) {
        System.out.println("Video is editable by current user.");
      }

      if(detailed) {



        YtStatistics stats = videoEntry.getStatistics();
        if(stats != null ) {
          System.out.println("View count: " + stats.getViewCount());
        }
        System.out.println();


      }
    }

  public static void printVideoFeed(VideoFeed videoFeed, boolean detailed) {
      for(VideoEntry videoEntry : videoFeed.getEntries() ) {
        printVideoEntry(videoEntry, detailed);
      }
    } 

  public static void printEntireVideoFeed(YouTubeService service, 
          VideoFeed videoFeed, boolean detailed) throws MalformedURLException, 
          IOException, ServiceException {
         do {
           printVideoFeed(videoFeed, detailed);
           if(videoFeed.getNextLink() != null) {
             videoFeed = service.getFeed(new URL(videoFeed.getNextLink().getHref()), 
               VideoFeed.class);
           }
           else {
             videoFeed = null;
           }
         }
         while(videoFeed != null);
        }


  public static void main(String[] args) {

  try {

      YouTubeService service = new YouTubeService("test");

      YouTubeQuery query = 
              new YouTubeQuery(new URL("http://gdata.youtube.com/feeds/api/videos"));
            query.setFullTextQuery("Sports");
            VideoFeed videoFeed = service.query(query, VideoFeed.class);
            printEntireVideoFeed(service, videoFeed, false);
  }
  catch(AuthenticationException e) {
    e.printStackTrace();
  }
  catch(MalformedURLException e) {
    e.printStackTrace();
  }
  catch(ServiceException e) {
    e.printStackTrace();
  }
  catch(IOException e) {
    e.printStackTrace();
  }
}

1 个答案:

答案 0 :(得分:0)

999条目似乎是设计上的限制。请参阅YouTube API v2.0 – Video Feed Types

  

“API会根据搜索视频的请求返回视频供稿。视频供稿最多包含999个条目。”

看起来同样的限制也适用于网站。

您可以尝试添加custom query parameters for the YouTube Data API来限制获得的结果数量,例如:

  • 字幕=真|假
  • 持续时间=短|介质|长
  • 格式= 1 | 5 | 6
  • time = today | this_week | this_month(而不是默认值:all_time)

通过这种方式,您可以使用多种查询来获得您感兴趣的更多结果。

修改:在搜索如何使用持续时间时,我在Stack Overflow上遇到了YouTube GData API - Query for videos with a specific duration,它引用了YouTube API v2.0 – Retrieving a Partial Response。例如,您可以使用此实验性“检索部分响应API”指定最大视图数(可能对获取长尾有用),这只是您可以使用它执行的许多操作之一。

相关问题