Skip to content

Commit 7b03687

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

File tree

1 file changed

+269
-0
lines changed

1 file changed

+269
-0
lines changed
Lines changed: 269 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,269 @@
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.CommentThreadSnippet;
30+
import com.google.api.services.youtube.model.V3CommentThreadListResponse;
31+
import com.google.common.collect.Lists;
32+
33+
/**
34+
* This sample creates and manages top-level comments by:
35+
*
36+
* 1. Creating a top-level comments for a video and a channel via "commentThreads.insert" method.
37+
* 2. Retrieving the top-level comments for a video and a channel via "commentThreads.list" method.
38+
* 3. Updating an existing comments via "commentThreads.update" method.
39+
*
40+
* @author Ibrahim Ulukaya
41+
*/
42+
public class CommentThreads {
43+
44+
/**
45+
* Define a global instance of a YouTube object, which will be used to make
46+
* YouTube Data API requests.
47+
*/
48+
private static YouTube youtube;
49+
50+
/**
51+
* Create, list and update top-level channel and video comments.
52+
*
53+
* @param args command line args (not used).
54+
*/
55+
public static void main(String[] args) {
56+
57+
// This OAuth 2.0 access scope allows for full read/write access to the
58+
// authenticated user's account.
59+
List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube");
60+
61+
try {
62+
// Authorize the request.
63+
Credential credential = Auth.authorize(scopes, "commentthreads");
64+
65+
// This object is used to make YouTube Data API requests.
66+
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential)
67+
.setApplicationName("youtube-cmdline-commentthreads-sample").build();
68+
69+
// Prompt the user for the ID of a channel to comment on.
70+
// Retrieve the channel ID that the user is commenting to.
71+
String channelId = getChannelId();
72+
System.out.println("You chose " + channelId + " to subscribe.");
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+
85+
// Insert channel comment by omitting videoId.
86+
// Create a comment snippet with text.
87+
CommentSnippet commentSnippet = new CommentSnippet();
88+
commentSnippet.setTextOriginal(text);
89+
90+
// Create a top-level comment with snippet.
91+
Comment topLevelComment = new Comment();
92+
topLevelComment.setSnippet(commentSnippet);
93+
94+
// Create a comment thread snippet with channelId and top-level
95+
// comment.
96+
CommentThreadSnippet commentThreadSnippet = new CommentThreadSnippet();
97+
commentThreadSnippet.setChannelId(channelId);
98+
commentThreadSnippet.setTopLevelComment(topLevelComment);
99+
100+
// Create a comment thread with snippet.
101+
CommentThread commentThread = new CommentThread();
102+
commentThread.setSnippet(commentThreadSnippet);
103+
104+
// Call the YouTube Data API's commentThreads.insert method to
105+
// create a comment.
106+
CommentThread channelCommentInsertResponse = youtube.commentThreads()
107+
.insert("snippet", commentThread).execute();
108+
// Print information from the API response.
109+
System.out
110+
.println("\n================== Created Channel Comment ==================\n");
111+
CommentSnippet snippet = channelCommentInsertResponse.getSnippet().getTopLevelComment()
112+
.getSnippet();
113+
System.out.println(" - Author: " + snippet.getAuthorDisplayName());
114+
System.out.println(" - Comment: " + snippet.getTextDisplay());
115+
System.out
116+
.println("\n-------------------------------------------------------------\n");
117+
118+
119+
// Insert video comment
120+
commentThreadSnippet.setVideoId(videoId);
121+
// Call the YouTube Data API's commentThreads.insert method to
122+
// create a comment.
123+
CommentThread videoCommentInsertResponse = youtube.commentThreads()
124+
.insert("snippet", commentThread).execute();
125+
// Print information from the API response.
126+
System.out
127+
.println("\n================== Created Video Comment ==================\n");
128+
snippet = videoCommentInsertResponse.getSnippet().getTopLevelComment()
129+
.getSnippet();
130+
System.out.println(" - Author: " + snippet.getAuthorDisplayName());
131+
System.out.println(" - Comment: " + snippet.getTextDisplay());
132+
System.out
133+
.println("\n-------------------------------------------------------------\n");
134+
135+
136+
// Call the YouTube Data API's commentThreads.list method to
137+
// retrieve video comment threads.
138+
V3CommentThreadListResponse videoCommentsListResponse = youtube.commentThreads()
139+
.list("snippet").setVideoId(videoId).setTextFormat("plainText").execute();
140+
List<CommentThread> videoComments = videoCommentsListResponse.getItems();
141+
142+
if (videoComments.isEmpty()) {
143+
System.out.println("Can't get video comments.");
144+
} else {
145+
// Print information from the API response.
146+
System.out
147+
.println("\n================== Returned Video Comments ==================\n");
148+
for (CommentThread videoComment : videoComments) {
149+
snippet = videoComment.getSnippet().getTopLevelComment()
150+
.getSnippet();
151+
System.out.println(" - Author: " + snippet.getAuthorDisplayName());
152+
System.out.println(" - Comment: " + snippet.getTextDisplay());
153+
System.out
154+
.println("\n-------------------------------------------------------------\n");
155+
}
156+
CommentThread firstComment = videoComments.get(0);
157+
firstComment.getSnippet().getTopLevelComment().getSnippet()
158+
.setTextOriginal("updated");
159+
CommentThread videoCommentUpdateResponse = youtube.commentThreads()
160+
.update("snippet", firstComment).execute();
161+
// Print information from the API response.
162+
System.out
163+
.println("\n================== Updated Video Comment ==================\n");
164+
snippet = videoCommentUpdateResponse.getSnippet().getTopLevelComment()
165+
.getSnippet();
166+
System.out.println(" - Author: " + snippet.getAuthorDisplayName());
167+
System.out.println(" - Comment: " + snippet.getTextDisplay());
168+
System.out
169+
.println("\n-------------------------------------------------------------\n");
170+
171+
}
172+
173+
// Call the YouTube Data API's commentThreads.list method to
174+
// retrieve channel comment threads.
175+
V3CommentThreadListResponse channelCommentsListResponse = youtube.commentThreads()
176+
.list("snippet").setChannelId(channelId).setTextFormat("plainText").execute();
177+
List<CommentThread> channelComments = channelCommentsListResponse.getItems();
178+
179+
if (channelComments.isEmpty()) {
180+
System.out.println("Can't get channel comments.");
181+
} else {
182+
// Print information from the API response.
183+
System.out
184+
.println("\n================== Returned Channel Comments ==================\n");
185+
for (CommentThread channelComment : channelComments) {
186+
snippet = channelComment.getSnippet().getTopLevelComment()
187+
.getSnippet();
188+
System.out.println(" - Author: " + snippet.getAuthorDisplayName());
189+
System.out.println(" - Comment: " + snippet.getTextDisplay());
190+
System.out
191+
.println("\n-------------------------------------------------------------\n");
192+
}
193+
CommentThread firstComment = channelComments.get(0);
194+
firstComment.getSnippet().getTopLevelComment().getSnippet()
195+
.setTextOriginal("updated");
196+
CommentThread channelCommentUpdateResponse = youtube.commentThreads()
197+
.update("snippet", firstComment).execute();
198+
// Print information from the API response.
199+
System.out
200+
.println("\n================== Updated Channel Comment ==================\n");
201+
snippet = channelCommentUpdateResponse.getSnippet().getTopLevelComment()
202+
.getSnippet();
203+
System.out.println(" - Author: " + snippet.getAuthorDisplayName());
204+
System.out.println(" - Comment: " + snippet.getTextDisplay());
205+
System.out
206+
.println("\n-------------------------------------------------------------\n");
207+
208+
}
209+
210+
} catch (GoogleJsonResponseException e) {
211+
System.err.println("GoogleJsonResponseException code: " + e.getDetails().getCode()
212+
+ " : " + e.getDetails().getMessage());
213+
e.printStackTrace();
214+
215+
} catch (IOException e) {
216+
System.err.println("IOException: " + e.getMessage());
217+
e.printStackTrace();
218+
} catch (Throwable t) {
219+
System.err.println("Throwable: " + t.getMessage());
220+
t.printStackTrace();
221+
}
222+
}
223+
224+
/*
225+
* Prompt the user to enter a channel ID. Then return the ID.
226+
*/
227+
private static String getChannelId() throws IOException {
228+
229+
String channelId = "";
230+
231+
System.out.print("Please enter a channel id: ");
232+
BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
233+
channelId = bReader.readLine();
234+
235+
return channelId;
236+
}
237+
238+
/*
239+
* Prompt the user to enter a video ID. Then return the ID.
240+
*/
241+
private static String getVideoId() throws IOException {
242+
243+
String videoId = "";
244+
245+
System.out.print("Please enter a video id: ");
246+
BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
247+
videoId = bReader.readLine();
248+
249+
return videoId;
250+
}
251+
252+
/*
253+
* Prompt the user to enter text for a comment. Then return the text.
254+
*/
255+
private static String getText() throws IOException {
256+
257+
String text = "";
258+
259+
System.out.print("Please enter a comment text: ");
260+
BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
261+
text = bReader.readLine();
262+
263+
if (text.length() < 1) {
264+
// If nothing is entered, defaults to "YouTube For Developers."
265+
text = "YouTube For Developers.";
266+
}
267+
return text;
268+
}
269+
}

0 commit comments

Comments
 (0)