到目前为止,我已经设法在我的Java程序中使用YouTube Data API收集了关于视频的前100条评论。
公共类CommentHandling {
/**
* Define a global instance of a YouTube object, which will be used to make
* YouTube Data API requests.
*/
private static YouTube youtube;
/**
* List, reply to comment threads; list, update, moderate, mark and delete
* replies.
*
* @param args command line args (not used).
*/
public static void main(String[] args) {
// This OAuth 2.0 access scope allows for full read/write access to the
// authenticated user's account and requires requests to use an SSL connection.
List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube.force-ssl");
try {
// Authorize the request.
Credential credential = Auth.authorize(scopes, "commentthreads");
// This object is used to make YouTube Data API requests.
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential)
.setApplicationName("youtube-cmdline-commentthreads-sample").build();
// Prompt the user for the ID of a video to comment on.
// Retrieve the video ID that the user is commenting to.
String videoId = getVideoId();
System.out.println("You chose " + videoId + " to subscribe.");
// All the available methods are used in sequence just for the sake
// of an example.
// Call the YouTube Data API's commentThreads.list method to
// retrieve video comment threads.
CommentThreadListResponse videoCommentsListResponse = youtube.commentThreads()
.list("snippet, replies").setVideoId(videoId)
.setMaxResults(new Long(100)).setTextFormat("html").execute();
List<CommentThread> videoComments = videoCommentsListResponse.getItems();
if (videoComments.isEmpty()) {
System.out.println("Can't get video comments.");
} else {
// Print information from the API response.
System.out
.println("\n================== Returned Video Comments ==================\n");
for (CommentThread videoComment : videoComments) {
CommentSnippet snippet = videoComment.getSnippet().getTopLevelComment()
.getSnippet();
System.out.println(" - Author: " + snippet.getAuthorDisplayName());
System.out.println(" - Comment: " + snippet.getTextDisplay());
System.out
.println("\n-------------------------------------------------------------\n");
}
}
} catch (GoogleJsonResponseException e) {
System.err.println("GoogleJsonResponseException code: " + e.getDetails().getCode()
+ " : " + e.getDetails().getMessage());
e.printStackTrace();
} catch (IOException e) {
System.err.println("IOException: " + e.getMessage());
e.printStackTrace();
} catch (Throwable t) {
System.err.println("Throwable: " + t.getMessage());
t.printStackTrace();
}
}
/*
* Prompt the user to enter a video ID. Then return the ID.
*/
private static String getVideoId() throws IOException {
String videoId = "";
System.out.print("Please enter a video id: ");
BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
videoId = bReader.readLine();
return videoId;
}
但我有兴趣收集视频上的热门评论(通过评论的喜欢或回复)。这些是通过使用“热门评论”过滤器排序时通常在视频第一页上的注释。
任何帮助都将受到高度赞赏。