Search in sources :

Example 21 with YouTube

use of com.google.api.services.youtube.YouTube in project api-samples by youtube.

the class CommentHandling method main.

/**
     * 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.");
        // Prompt the user for the comment text.
        // Retrieve the text that the user is commenting.
        String text = getText();
        System.out.println("You chose " + text + " 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").setVideoId(videoId).setTextFormat("plainText").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");
            }
            CommentThread firstComment = videoComments.get(0);
            // Will use this thread as parent to new reply.
            String parentId = firstComment.getId();
            // Create a comment snippet with text.
            CommentSnippet commentSnippet = new CommentSnippet();
            commentSnippet.setTextOriginal(text);
            commentSnippet.setParentId(parentId);
            // Create a comment with snippet.
            Comment comment = new Comment();
            comment.setSnippet(commentSnippet);
            // Call the YouTube Data API's comments.insert method to reply
            // to a comment.
            // (If the intention is to create a new top-level comment,
            // commentThreads.insert
            // method should be used instead.)
            Comment commentInsertResponse = youtube.comments().insert("snippet", comment).execute();
            // Print information from the API response.
            System.out.println("\n================== Created Comment Reply ==================\n");
            CommentSnippet snippet = commentInsertResponse.getSnippet();
            System.out.println("  - Author: " + snippet.getAuthorDisplayName());
            System.out.println("  - Comment: " + snippet.getTextDisplay());
            System.out.println("\n-------------------------------------------------------------\n");
            // Call the YouTube Data API's comments.list method to retrieve
            // existing comment
            // replies.
            CommentListResponse commentsListResponse = youtube.comments().list("snippet").setParentId(parentId).setTextFormat("plainText").execute();
            List<Comment> comments = commentsListResponse.getItems();
            if (comments.isEmpty()) {
                System.out.println("Can't get comment replies.");
            } else {
                // Print information from the API response.
                System.out.println("\n================== Returned Comment Replies ==================\n");
                for (Comment commentReply : comments) {
                    snippet = commentReply.getSnippet();
                    System.out.println("  - Author: " + snippet.getAuthorDisplayName());
                    System.out.println("  - Comment: " + snippet.getTextDisplay());
                    System.out.println("\n-------------------------------------------------------------\n");
                }
                Comment firstCommentReply = comments.get(0);
                firstCommentReply.getSnippet().setTextOriginal("updated");
                Comment commentUpdateResponse = youtube.comments().update("snippet", firstCommentReply).execute();
                // Print information from the API response.
                System.out.println("\n================== Updated Video Comment ==================\n");
                snippet = commentUpdateResponse.getSnippet();
                System.out.println("  - Author: " + snippet.getAuthorDisplayName());
                System.out.println("  - Comment: " + snippet.getTextDisplay());
                System.out.println("\n-------------------------------------------------------------\n");
                // Call the YouTube Data API's comments.setModerationStatus
                // method to set moderation
                // status of an existing comment.
                youtube.comments().setModerationStatus(firstCommentReply.getId(), "published");
                System.out.println("  -  Changed comment status to published: " + firstCommentReply.getId());
                // Call the YouTube Data API's comments.markAsSpam method to
                // mark an existing comment as spam.
                youtube.comments().markAsSpam(firstCommentReply.getId());
                System.out.println("  -  Marked comment as spam: " + firstCommentReply.getId());
                // Call the YouTube Data API's comments.delete method to
                // delete an existing comment.
                youtube.comments().delete(firstCommentReply.getId());
                System.out.println("  -  Deleted comment as spam: " + firstCommentReply.getId());
            }
        }
    } 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();
    }
}
Also used : CommentSnippet(com.google.api.services.youtube.model.CommentSnippet) Comment(com.google.api.services.youtube.model.Comment) Credential(com.google.api.client.auth.oauth2.Credential) IOException(java.io.IOException) YouTube(com.google.api.services.youtube.YouTube) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) CommentThreadListResponse(com.google.api.services.youtube.model.CommentThreadListResponse) CommentThread(com.google.api.services.youtube.model.CommentThread) CommentListResponse(com.google.api.services.youtube.model.CommentListResponse)

Example 22 with YouTube

use of com.google.api.services.youtube.YouTube in project api-samples by youtube.

the class CommentThreads method main.

/**
     * Create, list and update top-level channel and video comments.
     *
     * @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 channel to comment on.
        // Retrieve the channel ID that the user is commenting to.
        String channelId = getChannelId();
        System.out.println("You chose " + channelId + " to subscribe.");
        // 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.");
        // Prompt the user for the comment text.
        // Retrieve the text that the user is commenting.
        String text = getText();
        System.out.println("You chose " + text + " to subscribe.");
        // Insert channel comment by omitting videoId.
        // Create a comment snippet with text.
        CommentSnippet commentSnippet = new CommentSnippet();
        commentSnippet.setTextOriginal(text);
        // Create a top-level comment with snippet.
        Comment topLevelComment = new Comment();
        topLevelComment.setSnippet(commentSnippet);
        // Create a comment thread snippet with channelId and top-level
        // comment.
        CommentThreadSnippet commentThreadSnippet = new CommentThreadSnippet();
        commentThreadSnippet.setChannelId(channelId);
        commentThreadSnippet.setTopLevelComment(topLevelComment);
        // Create a comment thread with snippet.
        CommentThread commentThread = new CommentThread();
        commentThread.setSnippet(commentThreadSnippet);
        // Call the YouTube Data API's commentThreads.insert method to
        // create a comment.
        CommentThread channelCommentInsertResponse = youtube.commentThreads().insert("snippet", commentThread).execute();
        // Print information from the API response.
        System.out.println("\n================== Created Channel Comment ==================\n");
        CommentSnippet snippet = channelCommentInsertResponse.getSnippet().getTopLevelComment().getSnippet();
        System.out.println("  - Author: " + snippet.getAuthorDisplayName());
        System.out.println("  - Comment: " + snippet.getTextDisplay());
        System.out.println("\n-------------------------------------------------------------\n");
        // Insert video comment
        commentThreadSnippet.setVideoId(videoId);
        // Call the YouTube Data API's commentThreads.insert method to
        // create a comment.
        CommentThread videoCommentInsertResponse = youtube.commentThreads().insert("snippet", commentThread).execute();
        // Print information from the API response.
        System.out.println("\n================== Created Video Comment ==================\n");
        snippet = videoCommentInsertResponse.getSnippet().getTopLevelComment().getSnippet();
        System.out.println("  - Author: " + snippet.getAuthorDisplayName());
        System.out.println("  - Comment: " + snippet.getTextDisplay());
        System.out.println("\n-------------------------------------------------------------\n");
        // Call the YouTube Data API's commentThreads.list method to
        // retrieve video comment threads.
        CommentThreadListResponse videoCommentsListResponse = youtube.commentThreads().list("snippet").setVideoId(videoId).setTextFormat("plainText").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) {
                snippet = videoComment.getSnippet().getTopLevelComment().getSnippet();
                System.out.println("  - Author: " + snippet.getAuthorDisplayName());
                System.out.println("  - Comment: " + snippet.getTextDisplay());
                System.out.println("\n-------------------------------------------------------------\n");
            }
            CommentThread firstComment = videoComments.get(0);
            firstComment.getSnippet().getTopLevelComment().getSnippet().setTextOriginal("updated");
            CommentThread videoCommentUpdateResponse = youtube.commentThreads().update("snippet", firstComment).execute();
            // Print information from the API response.
            System.out.println("\n================== Updated Video Comment ==================\n");
            snippet = videoCommentUpdateResponse.getSnippet().getTopLevelComment().getSnippet();
            System.out.println("  - Author: " + snippet.getAuthorDisplayName());
            System.out.println("  - Comment: " + snippet.getTextDisplay());
            System.out.println("\n-------------------------------------------------------------\n");
        }
        // Call the YouTube Data API's commentThreads.list method to
        // retrieve channel comment threads.
        CommentThreadListResponse channelCommentsListResponse = youtube.commentThreads().list("snippet").setChannelId(channelId).setTextFormat("plainText").execute();
        List<CommentThread> channelComments = channelCommentsListResponse.getItems();
        if (channelComments.isEmpty()) {
            System.out.println("Can't get channel comments.");
        } else {
            // Print information from the API response.
            System.out.println("\n================== Returned Channel Comments ==================\n");
            for (CommentThread channelComment : channelComments) {
                snippet = channelComment.getSnippet().getTopLevelComment().getSnippet();
                System.out.println("  - Author: " + snippet.getAuthorDisplayName());
                System.out.println("  - Comment: " + snippet.getTextDisplay());
                System.out.println("\n-------------------------------------------------------------\n");
            }
            CommentThread firstComment = channelComments.get(0);
            firstComment.getSnippet().getTopLevelComment().getSnippet().setTextOriginal("updated");
            CommentThread channelCommentUpdateResponse = youtube.commentThreads().update("snippet", firstComment).execute();
            // Print information from the API response.
            System.out.println("\n================== Updated Channel Comment ==================\n");
            snippet = channelCommentUpdateResponse.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();
    }
}
Also used : CommentThreadSnippet(com.google.api.services.youtube.model.CommentThreadSnippet) CommentSnippet(com.google.api.services.youtube.model.CommentSnippet) Comment(com.google.api.services.youtube.model.Comment) Credential(com.google.api.client.auth.oauth2.Credential) IOException(java.io.IOException) YouTube(com.google.api.services.youtube.YouTube) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) CommentThread(com.google.api.services.youtube.model.CommentThread) CommentThreadListResponse(com.google.api.services.youtube.model.CommentThreadListResponse)

Aggregations

YouTube (com.google.api.services.youtube.YouTube)22 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)21 Credential (com.google.api.client.auth.oauth2.Credential)20 IOException (java.io.IOException)19 InputStreamContent (com.google.api.client.http.InputStreamContent)4 Video (com.google.api.services.youtube.model.Video)4 ArrayList (java.util.ArrayList)4 MediaHttpUploader (com.google.api.client.googleapis.media.MediaHttpUploader)3 MediaHttpUploaderProgressListener (com.google.api.client.googleapis.media.MediaHttpUploaderProgressListener)3 VideoSnippet (com.google.api.services.youtube.model.VideoSnippet)3 Calendar (java.util.Calendar)3 HttpRequest (com.google.api.client.http.HttpRequest)2 HttpRequestInitializer (com.google.api.client.http.HttpRequestInitializer)2 Channel (com.google.api.services.youtube.model.Channel)2 ChannelListResponse (com.google.api.services.youtube.model.ChannelListResponse)2 Comment (com.google.api.services.youtube.model.Comment)2 CommentSnippet (com.google.api.services.youtube.model.CommentSnippet)2 CommentThread (com.google.api.services.youtube.model.CommentThread)2 CommentThreadListResponse (com.google.api.services.youtube.model.CommentThreadListResponse)2 SearchListResponse (com.google.api.services.youtube.model.SearchListResponse)2