如何使用Java检索超过25个youtube注释

时间:2013-08-30 07:14:14

标签: java youtube youtube-api

      YouTubeService service = new YouTubeService("MyApp");
      String videoEntryUrl = "http://gdata.youtube.com/feeds/api/videos/nU9dinwMyHo";
      VideoEntry videoEntry = service.getEntry(new URL(videoEntryUrl), VideoEntry.class);
      String commentUrl = videoEntry.getComments().getFeedLink().getHref();

      CommentFeed commentFeed = service.getFeed(new URL(commentUrl), CommentFeed.class);
      for(CommentEntry comment : commentFeed.getEntries()) {
        System.out.println(comment.getPlainTextContent());

以下是我检索特定视频的YouTube视频的代码。 我可以使用此代码检索大约25条评论,但如何从视频中检索所有评论?

1 个答案:

答案 0 :(得分:0)

我认为它类似于:

// Get a video entry
String str = "http://gdata.youtube.com/feeds/api/videos/" + videoId;
YouTubeQuery youtubeQuery = new YouTubeQuery(new URL(str));
String videoEntryUrl = youtubeQuery.getUrl().toString();
VideoEntry videoEntry = service.getEntry(new URL(videoEntryUrl),VideoEntry.class);

// Get the comments url for this video
if (videoEntry.getComments() != null) {
    String commentUrl = videoEntry.getComments().getFeedLink().getHref();
    System.out.println(commentUrl);

    // Get the comment feed; use a new query
    YouTubeQuery youtubeQuery = new YouTubeQuery(new URL(commentUrl);
    youtubeQuery.setMaxResults(50);


    youtubeQuery.setStartIndex(1);
    String commentUrlFeed = youtubeQuery.getUrl().toString();
    CommentFeed commentFeed = service.getFeed(new URL(commentUrlFeed),CommentFeed.class);
    // The response should contain an url for the next feed, if any, already with an updated start-index.

    for (int i = 0; i < commentFeed.getEntries().size()
            && commentFeed.getEntries().get(i) != null; i++) {

        String author=commentFeed.getEntries().get(i).getAuthors().get(0).getUri().substring(41)
        String commentId=commentFeed.getEntries().get(i).getId().substring(47);
        String comment=commentFeed.getEntries().get(i).getPlainTextContent();
        }
    }
    // Loop thru next comment feed call, if more can be expected.
    // Use updated url from the response or set start-index = start-index + max-results.
 }