如何使用Java检索超过25条YouTube评论


      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 条评论,但如何从视频中检索所有评论?

我认为它是这样的:

// 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.
 }

相关内容

  • 没有找到相关文章

最新更新