Skip to content

Commit cf7f9b3

Browse files
author
Jim Rogers
committed
Clean up from CR feedback
1 parent 518a2d3 commit cf7f9b3

5 files changed

Lines changed: 76 additions & 88 deletions

File tree

java/README.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ job.
218218
Method: youtubeReporting.jobs.list, youtubeReporting.reports.list<br>
219219
Description: This sample demonstrates how to retrieve reports created by a specific job. It calls the
220220
<code>jobs.list</code> method to retrieve reporting jobs. It then calls the <code>reports.list</code> method with the
221-
<code>jobId</code> parameter set to a specific job id to retrieve reports created by that job. Finally, the sample
221+
<code>jobId</code> parameter set to a specific job ID to retrieve reports created by that job. Finally, the sample
222222
prints out the download URL for each report.
223223

224224
### [Create a broadcast and stream](/java/src/main/java/com/google/api/services/samples/youtube/cmdline/live/CreateBroadcast.java)
@@ -245,24 +245,23 @@ that a channel can use to broadcast live events on YouTube.
245245
### [Get a live chat id](/java/src/main/java/com/google/api/services/samples/youtube/cmdline/live/GetLiveChatId.java)
246246

247247
Methods: youtube.videos.list, youtube.liveBroadcasts.list<br>
248-
Description: This sample retrieves a live chat id from either a <code>videoId</code> parameter
249-
or the current user's live broadcast. The <code>liveChatId</code> is required for other samples
248+
Description: This sample retrieves the live chat ID from either a <code>videoId</code> parameter
249+
or the live broadcast for the authorized user's channel. The <code>liveChatId</code> is required for other samples
250250
that interact with live chat.
251251

252252
### [Insert a live chat message](/java/src/main/java/com/google/api/services/samples/youtube/cmdline/live/InsertLiveChatMessage.java)
253253

254254
Method: youtube.liveChatMessages.insert<br>
255-
Description: This sample inserts a live chat message into the live chat specified by either
256-
a <code>videoId</code> parameter or the current user's live broadcast.
255+
Description: This sample inserts a live chat message into the the specified video or the live broadcast for
256+
the authorized user's channel.
257257

258258
### [Delete a live chat message](/java/src/main/java/com/google/api/services/samples/youtube/cmdline/live/DeleteLiveChatMessage.java)
259259

260260
Method: youtube.liveChatMessages.delete<br>
261-
Description: This sample deletes a live chat message from the live chat specified by either
262-
a <code>videoId</code> parameter or the current user's live broadcast.
261+
Description: This sample deletes the specified live chat message.
263262

264263
### [List live chat messages](/java/src/main/java/com/google/api/services/samples/youtube/cmdline/live/ListLiveChatMessages.java)
265264

266265
Method: youtube.liveChatMessages.list<br>
267-
Description: This sample lists live chat messages from the chat specified by either
268-
a <code>videoId</code> parameter or the current user's live broadcast.
266+
Description: This sample lists live chat messages from the specified video or from the live broadcast for
267+
the authorized user's channel.

java/src/main/java/com/google/api/services/samples/youtube/cmdline/live/DeleteLiveChatMessage.java

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
import com.google.api.services.samples.youtube.cmdline.Auth;
2020
import com.google.api.services.youtube.YouTube;
2121
import com.google.api.services.youtube.YouTubeScopes;
22+
import com.google.common.collect.Lists;
2223
import java.io.IOException;
23-
import java.util.ArrayList;
2424
import java.util.List;
2525

2626
/**
@@ -52,11 +52,8 @@ public static void main(String[] args) {
5252
}
5353
String messageId = args[0];
5454

55-
// This OAuth 2.0 access scope allows for read-only access to the
56-
// authenticated user's account, but not other types of account access.
57-
List<String> scopes = new ArrayList<String>();
58-
scopes.add(YouTubeScopes.YOUTUBE_FORCE_SSL);
59-
scopes.add(YouTubeScopes.YOUTUBE);
55+
// This OAuth 2.0 access scope allows for write access to the authenticated user's account.
56+
List<String> scopes = Lists.newArrayList(YouTubeScopes.YOUTUBE_FORCE_SSL);
6057

6158
try {
6259
// Authorize the request.
@@ -66,17 +63,6 @@ public static void main(String[] args) {
6663
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential)
6764
.setApplicationName("youtube-cmdline-deletechatmessages-sample").build();
6865

69-
// Get the liveChatId
70-
String liveChatId = GetLiveChatId.getLiveChatId(
71-
youtube,
72-
args.length == 2 ? args[1] : null);
73-
if (liveChatId != null) {
74-
System.out.println("Live chat id: " + liveChatId);
75-
} else {
76-
System.err.println("Unable to find a live chat id");
77-
System.exit(1);
78-
}
79-
8066
// Delete the message from live chat
8167
YouTube.LiveChatMessages.Delete liveChatDelete =
8268
youtube.liveChatMessages().delete(messageId);

java/src/main/java/com/google/api/services/samples/youtube/cmdline/live/GetLiveChatId.java

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ public static void main(String[] args) {
6868
.setApplicationName("youtube-cmdline-getlivechatid-sample").build();
6969

7070
// Get the liveChatId
71-
String liveChatId = getLiveChatId(youtube, args.length == 1 ? args[0] : null);
71+
String liveChatId = args.length == 1
72+
? getLiveChatId(youtube, args[0])
73+
: getLiveChatId(youtube);
7274
if (liveChatId != null) {
7375
System.out.println("Live chat id: " + liveChatId);
7476
} else {
@@ -91,41 +93,48 @@ public static void main(String[] args) {
9193
}
9294

9395
/**
94-
* Retrieves the liveChatId from the videoId, if specified. If not specified, will find the
95-
* liveChatId for the signed in user's live broadcast.
96+
* Retrieves the liveChatId from the authenticated user's live broadcast.
9697
*
9798
* @param youtube The object is used to make YouTube Data API requests.
98-
* @param videoId The videoId associated with the live chat (optional).
9999
* @return A liveChatId, or null if not found.
100100
*/
101-
static String getLiveChatId(YouTube youtube, String videoId) throws IOException {
102-
if (videoId != null && !videoId.isEmpty()) {
103-
// Get liveChatId from the video
104-
YouTube.Videos.List videoList = youtube.videos()
105-
.list("liveStreamingDetails")
106-
.setFields("items/liveStreamingDetails/activeLiveChatId")
107-
.setId(videoId);
108-
VideoListResponse response = videoList.execute();
109-
for (Video v : response.getItems()) {
110-
String liveChatId = v.getLiveStreamingDetails().getActiveLiveChatId();
111-
if (liveChatId != null && !liveChatId.isEmpty()) {
112-
return liveChatId;
113-
}
101+
static String getLiveChatId(YouTube youtube) throws IOException {
102+
// Get signed in user's liveChatId
103+
YouTube.LiveBroadcasts.List broadcastList = youtube
104+
.liveBroadcasts()
105+
.list("snippet")
106+
.setFields("items/snippet/liveChatId")
107+
.setBroadcastType("all")
108+
.setBroadcastStatus("active");
109+
LiveBroadcastListResponse broadcastListResponse = broadcastList.execute();
110+
for (LiveBroadcast b : broadcastListResponse.getItems()) {
111+
String liveChatId = b.getSnippet().getLiveChatId();
112+
if (liveChatId != null && !liveChatId.isEmpty()) {
113+
return liveChatId;
114114
}
115-
} else {
116-
// Get signed in user's liveChatId
117-
YouTube.LiveBroadcasts.List broadcastList = youtube
118-
.liveBroadcasts()
119-
.list("snippet")
120-
.setFields("items/snippet/liveChatId")
121-
.setBroadcastType("all")
122-
.setBroadcastStatus("active");
123-
LiveBroadcastListResponse broadcastListResponse = broadcastList.execute();
124-
for (LiveBroadcast b : broadcastListResponse.getItems()) {
125-
String liveChatId = b.getSnippet().getLiveChatId();
126-
if (liveChatId != null && !liveChatId.isEmpty()) {
127-
return liveChatId;
128-
}
115+
}
116+
117+
return null;
118+
}
119+
120+
/**
121+
* Retrieves the liveChatId from the broadcast associated with a videoId.
122+
*
123+
* @param youtube The object is used to make YouTube Data API requests.
124+
* @param videoId The videoId associated with the live broadcast.
125+
* @return A liveChatId, or null if not found.
126+
*/
127+
static String getLiveChatId(YouTube youtube, String videoId) throws IOException {
128+
// Get liveChatId from the video
129+
YouTube.Videos.List videoList = youtube.videos()
130+
.list("liveStreamingDetails")
131+
.setFields("items/liveStreamingDetails/activeLiveChatId")
132+
.setId(videoId);
133+
VideoListResponse response = videoList.execute();
134+
for (Video v : response.getItems()) {
135+
String liveChatId = v.getLiveStreamingDetails().getActiveLiveChatId();
136+
if (liveChatId != null && !liveChatId.isEmpty()) {
137+
return liveChatId;
129138
}
130139
}
131140

java/src/main/java/com/google/api/services/samples/youtube/cmdline/live/InsertLiveChatMessage.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
import com.google.api.services.youtube.model.LiveChatMessage;
2323
import com.google.api.services.youtube.model.LiveChatMessageSnippet;
2424
import com.google.api.services.youtube.model.LiveChatTextMessageDetails;
25+
import com.google.common.collect.Lists;
2526
import java.io.IOException;
26-
import java.util.ArrayList;
2727
import java.util.List;
2828

2929
/**
@@ -61,11 +61,8 @@ public static void main(String[] args) {
6161
}
6262
String message = args[0];
6363

64-
// This OAuth 2.0 access scope allows for read-only access to the
65-
// authenticated user's account, but not other types of account access.
66-
List<String> scopes = new ArrayList<String>();
67-
scopes.add(YouTubeScopes.YOUTUBE_FORCE_SSL);
68-
scopes.add(YouTubeScopes.YOUTUBE);
64+
// This OAuth 2.0 access scope allows for write access to the authenticated user's account.
65+
List<String> scopes = Lists.newArrayList(YouTubeScopes.YOUTUBE_FORCE_SSL);
6966

7067
try {
7168
// Authorize the request.
@@ -76,9 +73,9 @@ public static void main(String[] args) {
7673
.setApplicationName("youtube-cmdline-insertchatmessage-sample").build();
7774

7875
// Get the liveChatId
79-
String liveChatId = GetLiveChatId.getLiveChatId(
80-
youtube,
81-
args.length == 2 ? args[1] : null);
76+
String liveChatId = args.length == 2
77+
? GetLiveChatId.getLiveChatId(youtube, args[1])
78+
: GetLiveChatId.getLiveChatId(youtube);
8279
if (liveChatId != null) {
8380
System.out.println("Live chat id: " + liveChatId);
8481
} else {
@@ -91,7 +88,6 @@ public static void main(String[] args) {
9188
LiveChatMessageSnippet snippet = new LiveChatMessageSnippet();
9289
snippet.setType("textMessageEvent");
9390
snippet.setLiveChatId(liveChatId);
94-
snippet.setDisplayMessage(message);
9591
LiveChatTextMessageDetails details = new LiveChatTextMessageDetails();
9692
details.setMessageText(message);
9793
snippet.setTextMessageDetails(details);

java/src/main/java/com/google/api/services/samples/youtube/cmdline/live/ListLiveChatMessages.java

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.google.api.services.youtube.model.LiveChatSuperChatDetails;
2727
import com.google.common.collect.Lists;
2828
import java.io.IOException;
29+
import java.util.ArrayList;
2930
import java.util.List;
3031
import java.util.Timer;
3132
import java.util.TimerTask;
@@ -79,9 +80,9 @@ public static void main(String[] args) {
7980
.setApplicationName("youtube-cmdline-listchatmessages-sample").build();
8081

8182
// Get the liveChatId
82-
String liveChatId = GetLiveChatId.getLiveChatId(
83-
youtube,
84-
args.length == 1 ? args[0] : null);
83+
String liveChatId = args.length == 1
84+
? GetLiveChatId.getLiveChatId(youtube, args[0])
85+
: GetLiveChatId.getLiveChatId(youtube);
8586
if (liveChatId != null) {
8687
System.out.println("Live chat id: " + liveChatId);
8788
} else {
@@ -176,25 +177,22 @@ private static String buildOutput(
176177
output.append("SUPERCHAT RECEIVED FROM ");
177178
}
178179
output.append(author.getDisplayName());
179-
if (author.getIsChatOwner() || author.getIsChatModerator() || author.getIsChatSponsor()) {
180+
List<String> roles = new ArrayList<String>();
181+
if (author.getIsChatOwner()) {
182+
roles.add("OWNER");
183+
}
184+
if (author.getIsChatModerator()) {
185+
roles.add("MODERATOR");
186+
}
187+
if (author.getIsChatSponsor()) {
188+
roles.add("SPONSOR");
189+
}
190+
if (roles.size() > 0) {
180191
output.append(" (");
181-
boolean appendComma = false;
182-
if (author.getIsChatOwner()) {
183-
output.append("OWNER");
184-
appendComma = true;
185-
}
186-
if (author.getIsChatModerator()) {
187-
if (appendComma) {
188-
output.append(", ");
189-
}
190-
output.append("MODERATOR");
191-
appendComma = true;
192-
}
193-
if (author.getIsChatSponsor()) {
194-
if (appendComma) {
195-
output.append(", ");
196-
}
197-
output.append("SPONSOR");
192+
String delim = "";
193+
for (String role : roles) {
194+
output.append(delim).append(role);
195+
delim = ", ";
198196
}
199197
output.append(")");
200198
}

0 commit comments

Comments
 (0)