Java 如何使用YouTube数据API在YouTube视频上获取最喜欢的评论或回复?
到目前为止,我已经在Java程序中使用YouTube数据API收集了视频的前100条评论,如下所示 公共类评论处理{Java 如何使用YouTube数据API在YouTube视频上获取最喜欢的评论或回复?,java,video,youtube,youtube-data-api,Java,Video,Youtube,Youtube Data Api,到目前为止,我已经在Java程序中使用YouTube数据API收集了视频的前100条评论,如下所示 公共类评论处理{ /** * 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,
/**
* 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;
}
/**
*定义YouTube对象的全局实例,该实例将用于
*YouTube数据API请求。
*/
私有静态YouTube YouTube;
/**
*列表,回复评论线程;列表,更新,缓和,标记和删除
*答复。
*
*@param args命令行args(未使用)。
*/
公共静态void main(字符串[]args){
//此OAuth 2.0访问范围允许对
//已验证用户的帐户,并要求请求使用SSL连接。
列表范围=列表。newArrayList(“https://www.googleapis.com/auth/youtube.force-ssl");
试一试{
//批准请求。
凭证=授权(作用域,“评论线程”);
//此对象用于发出YouTube数据API请求。
youtube=new youtube.Builder(Auth.HTTP_传输、Auth.JSON_工厂、凭证)
.setApplicationName(“youtube cmdline commentthreads示例”).build();
//提示用户输入要评论的视频的ID。
//检索用户评论的视频ID。
字符串videoId=getVideoId();
System.out.println(“您选择“+videoId+”订阅”);
//所有可用的方法都是按顺序使用的
//举个例子。
//调用YouTube数据API的commentThreads.list方法
//检索视频评论线程。
CommentThreadListResponse videoCommentsListResponse=youtube.commentThreads()
.list(“片段,回复”).setVideoId(videoId)
.setMaxResults(新长(100)).setTextFormat(“html”).execute();
List videoComments=VideoCommentsResponse.getItems();
if(videoComments.isEmpty()){
System.out.println(“无法获取视频评论”);
}否则{
//打印API响应中的信息。
系统输出
.println(“\n================================================================\n”);
对于(评论线程videoComment:videoComments){
CommentSnippet snippet=videoComment.getSnippet().GetToLevel注释()
.getSnippet();
System.out.println(“-Author:+snippet.getAuthorDisplayName());
System.out.println(“-Comment:+snippet.getTextDisplay());
系统输出
.println(“\n--------------------------------------------\n”);
}
}
}捕获(GoogleJsonResponseException e){
System.err.println(“GoogleJsonResponseException代码:”+e.getDetails().getCode()
+“:”+e.getDetails().getMessage());
e、 printStackTrace();
}捕获(IOE异常){
System.err.println(“IOException:+e.getMessage());
e、 printStackTrace();
}捕获(可丢弃的t){
System.err.println(“Throwable:+t.getMessage());
t、 printStackTrace();
}
}
/*
*提示用户输入视频ID,然后返回ID。
*/
私有静态字符串getVideoId()引发IOException{
字符串videoId=“”;
System.out.print(“请输入视频id:”);
BufferedReader bReader=新的BufferedReader(新的InputStreamReader(System.in));
videoId=bReader.readLine();
返回videoId;
}
但我对收集视频中的顶级评论(通过喜欢或回复评论)感兴趣。这些评论通常在视频的第一页上,使用“顶级评论”过滤器进行排序
任何帮助都将不胜感激。表示您希望获得顶级评论