Skip to content

Commit dadfbf2

Browse files
"Added sample: java/src/main/java/com/google/api/services/samples/youtube/cmdline/data/CommentHandling.java"
1 parent 7b03687 commit dadfbf2

1 file changed

Lines changed: 235 additions & 0 deletions

File tree

Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
/*
2+
* Copyright (c) 2013 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package com.google.api.services.samples.youtube.cmdline.data;
16+
17+
import java.io.BufferedReader;
18+
import java.io.IOException;
19+
import java.io.InputStreamReader;
20+
import java.util.List;
21+
22+
import com.google.api.client.auth.oauth2.Credential;
23+
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
24+
import com.google.api.services.samples.youtube.cmdline.Auth;
25+
import com.google.api.services.youtube.YouTube;
26+
import com.google.api.services.youtube.model.Comment;
27+
import com.google.api.services.youtube.model.CommentSnippet;
28+
import com.google.api.services.youtube.model.CommentThread;
29+
import com.google.api.services.youtube.model.V3CommentListResponse;
30+
import com.google.api.services.youtube.model.V3CommentThreadListResponse;
31+
import com.google.common.collect.Lists;
32+
33+
/**
34+
* This sample creates and manages comments by:
35+
*
36+
* 1. Retrieving the top-level comments for a video via "commentThreads.list" method.
37+
* 2. Replying to a comment thread via "comments.insert" method.
38+
* 3. Retrieving comment replies via "comments.list" method.
39+
* 4. Updating an existing comment via "comments.update" method.
40+
* 5. Sets moderation status of an existing comment via "comments.setModerationStatus" method.
41+
* 6. Marking a comment as spam via "comments.markAsSpam" method.
42+
* 7. Deleting an existing comment via "comments.delete" method.
43+
*
44+
* @author Ibrahim Ulukaya
45+
*/
46+
public class CommentHandling {
47+
48+
/**
49+
* Define a global instance of a YouTube object, which will be used to make
50+
* YouTube Data API requests.
51+
*/
52+
private static YouTube youtube;
53+
54+
/**
55+
* List, reply to comment threads; list, update, moderate, mark and delete
56+
* replies.
57+
*
58+
* @param args command line args (not used).
59+
*/
60+
public static void main(String[] args) {
61+
62+
// This OAuth 2.0 access scope allows for full read/write access to the
63+
// authenticated user's account.
64+
List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube");
65+
66+
try {
67+
// Authorize the request.
68+
Credential credential = Auth.authorize(scopes, "commentthreads");
69+
70+
// This object is used to make YouTube Data API requests.
71+
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential)
72+
.setApplicationName("youtube-cmdline-commentthreads-sample").build();
73+
74+
// Prompt the user for the ID of a video to comment on.
75+
// Retrieve the video ID that the user is commenting to.
76+
String videoId = getVideoId();
77+
System.out.println("You chose " + videoId + " to subscribe.");
78+
79+
// Prompt the user for the comment text.
80+
// Retrieve the text that the user is commenting.
81+
String text = getText();
82+
System.out.println("You chose " + text + " to subscribe.");
83+
84+
// All the available methods are used in sequence just for the sake
85+
// of an example.
86+
87+
// Call the YouTube Data API's commentThreads.list method to
88+
// retrieve video comment threads.
89+
V3CommentThreadListResponse videoCommentsListResponse = youtube.commentThreads()
90+
.list("snippet").setVideoId(videoId).setTextFormat("plainText").execute();
91+
List<CommentThread> videoComments = videoCommentsListResponse.getItems();
92+
93+
if (videoComments.isEmpty()) {
94+
System.out.println("Can't get video comments.");
95+
} else {
96+
// Print information from the API response.
97+
System.out
98+
.println("\n================== Returned Video Comments ==================\n");
99+
for (CommentThread videoComment : videoComments) {
100+
CommentSnippet snippet = videoComment.getSnippet().getTopLevelComment()
101+
.getSnippet();
102+
System.out.println(" - Author: " + snippet.getAuthorDisplayName());
103+
System.out.println(" - Comment: " + snippet.getTextDisplay());
104+
System.out
105+
.println("\n-------------------------------------------------------------\n");
106+
}
107+
CommentThread firstComment = videoComments.get(0);
108+
109+
// Will use this thread as parent to new reply.
110+
String parentId = firstComment.getId();
111+
112+
// Create a comment snippet with text.
113+
CommentSnippet commentSnippet = new CommentSnippet();
114+
commentSnippet.setTextOriginal(text);
115+
commentSnippet.setParentId(parentId);
116+
117+
// Create a comment with snippet.
118+
Comment comment = new Comment();
119+
comment.setSnippet(commentSnippet);
120+
121+
// Call the YouTube Data API's comments.insert method to reply
122+
// to a comment.
123+
// (If the intention is to create a new top-level comment,
124+
// commentThreads.insert
125+
// method should be used instead.)
126+
Comment commentInsertResponse = youtube.comments().insert("snippet", comment)
127+
.execute();
128+
129+
// Print information from the API response.
130+
System.out
131+
.println("\n================== Created Comment Reply ==================\n");
132+
CommentSnippet snippet = commentInsertResponse.getSnippet();
133+
System.out.println(" - Author: " + snippet.getAuthorDisplayName());
134+
System.out.println(" - Comment: " + snippet.getTextDisplay());
135+
System.out
136+
.println("\n-------------------------------------------------------------\n");
137+
138+
// Call the YouTube Data API's comments.list method to retrieve
139+
// existing comment
140+
// replies.
141+
V3CommentListResponse commentsListResponse = youtube.comments().list("snippet")
142+
.setParentId(parentId).setTextFormat("plainText").execute();
143+
List<Comment> comments = commentsListResponse.getItems();
144+
145+
if (comments.isEmpty()) {
146+
System.out.println("Can't get comment replies.");
147+
} else {
148+
// Print information from the API response.
149+
System.out
150+
.println("\n================== Returned Comment Replies ==================\n");
151+
for (Comment commentReply : comments) {
152+
snippet = commentReply.getSnippet();
153+
System.out.println(" - Author: " + snippet.getAuthorDisplayName());
154+
System.out.println(" - Comment: " + snippet.getTextDisplay());
155+
System.out
156+
.println("\n-------------------------------------------------------------\n");
157+
}
158+
Comment firstCommentReply = comments.get(0);
159+
firstCommentReply.getSnippet().setTextOriginal("updated");
160+
Comment commentUpdateResponse = youtube.comments()
161+
.update("snippet", firstCommentReply).execute();
162+
// Print information from the API response.
163+
System.out
164+
.println("\n================== Updated Video Comment ==================\n");
165+
snippet = commentUpdateResponse.getSnippet();
166+
System.out.println(" - Author: " + snippet.getAuthorDisplayName());
167+
System.out.println(" - Comment: " + snippet.getTextDisplay());
168+
System.out
169+
.println("\n-------------------------------------------------------------\n");
170+
171+
// Call the YouTube Data API's comments.setModerationStatus
172+
// method to set moderation
173+
// status of an existing comment.
174+
youtube.comments().setModerationStatus(firstCommentReply.getId(), "published");
175+
System.out.println(" - Changed comment status to published: "
176+
+ firstCommentReply.getId());
177+
178+
// Call the YouTube Data API's comments.markAsSpam method to
179+
// mark an existing comment as spam.
180+
youtube.comments().markAsSpam(firstCommentReply.getId());
181+
System.out.println(" - Marked comment as spam: " + firstCommentReply.getId());
182+
183+
// Call the YouTube Data API's comments.delete method to
184+
// delete an existing comment.
185+
youtube.comments().delete(firstCommentReply.getId());
186+
System.out
187+
.println(" - Deleted comment as spam: " + firstCommentReply.getId());
188+
}
189+
}
190+
} catch (GoogleJsonResponseException e) {
191+
System.err.println("GoogleJsonResponseException code: " + e.getDetails().getCode()
192+
+ " : " + e.getDetails().getMessage());
193+
e.printStackTrace();
194+
195+
} catch (IOException e) {
196+
System.err.println("IOException: " + e.getMessage());
197+
e.printStackTrace();
198+
} catch (Throwable t) {
199+
System.err.println("Throwable: " + t.getMessage());
200+
t.printStackTrace();
201+
}
202+
}
203+
204+
/*
205+
* Prompt the user to enter a video ID. Then return the ID.
206+
*/
207+
private static String getVideoId() throws IOException {
208+
209+
String videoId = "";
210+
211+
System.out.print("Please enter a video id: ");
212+
BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
213+
videoId = bReader.readLine();
214+
215+
return videoId;
216+
}
217+
218+
/*
219+
* Prompt the user to enter text for a comment. Then return the text.
220+
*/
221+
private static String getText() throws IOException {
222+
223+
String text = "";
224+
225+
System.out.print("Please enter a comment text: ");
226+
BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
227+
text = bReader.readLine();
228+
229+
if (text.length() < 1) {
230+
// If nothing is entered, defaults to "YouTube For Developers."
231+
text = "YouTube For Developers.";
232+
}
233+
return text;
234+
}
235+
}

0 commit comments

Comments
 (0)