Skip to content

Commit

Permalink
Merge pull request #110 from jimrogerz/live_chat
Browse files Browse the repository at this point in the history
Add samples for live chat
  • Loading branch information
mchambers committed Mar 28, 2017
2 parents 0b62f83 + cf7f9b3 commit 48b4230
Show file tree
Hide file tree
Showing 6 changed files with 578 additions and 5 deletions.
37 changes: 33 additions & 4 deletions java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ client ID and client secret. You can create an ID/secret pair at:

To build this code sample from the command line, type:

mvn compile
<code>mvn compile</code>

To run the code sample from the command line, enter the following:
To run a code sample from the command line, enter the following:

mvn exec:java -Dexec.mainClass="FULL_CLASS_NAME"
<code>mvn exec:java -Dexec.mainClass="FULL_CLASS_NAME"</code>

For samples that require arguments, also specify -Dexec.args, e.g.:

<code>mvn exec:java -Dexec.mainClass="FULL_CLASS_NAME" -Dexec.args="arg1 arg2"</code>

For more instructions about how to set up Maven and/or your IDE to run
YouTube API samples, see this video:
Expand Down Expand Up @@ -214,7 +218,7 @@ job.
Method: youtubeReporting.jobs.list, youtubeReporting.reports.list<br>
Description: This sample demonstrates how to retrieve reports created by a specific job. It calls the
<code>jobs.list</code> method to retrieve reporting jobs. It then calls the <code>reports.list</code> method with the
<code>jobId</code> parameter set to a specific job id to retrieve reports created by that job. Finally, the sample
<code>jobId</code> parameter set to a specific job ID to retrieve reports created by that job. Finally, the sample
prints out the download URL for each report.

### [Create a broadcast and stream](/java/src/main/java/com/google/api/services/samples/youtube/cmdline/live/CreateBroadcast.java)
Expand All @@ -236,3 +240,28 @@ also specify a value for the <code>--broadcast-status</code> option to only retr
Method: youtube.liveStreams.list<br>
Description: This sample calls the API's <code>liveStreams.list</code> method to retrieve a list of video stream settings
that a channel can use to broadcast live events on YouTube.


### [Get a live chat id](/java/src/main/java/com/google/api/services/samples/youtube/cmdline/live/GetLiveChatId.java)

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

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

Method: youtube.liveChatMessages.insert<br>
Description: This sample inserts a live chat message into the the specified video or the live broadcast for
the authorized user's channel.

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

Method: youtube.liveChatMessages.delete<br>
Description: This sample deletes the specified live chat message.

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

Method: youtube.liveChatMessages.list<br>
Description: This sample lists live chat messages from the specified video or from the live broadcast for
the authorized user's channel.
2 changes: 1 addition & 1 deletion java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<url>http://maven.apache.org</url>

<properties>
<project.youtube.version>v3-rev179-1.22.0</project.youtube.version>
<project.youtube.version>v3-rev182-1.22.0</project.youtube.version>
<project.youtube.analytics.version>v1-rev63-1.22.0</project.youtube.analytics.version>
<project.youtube.reporting.version>v1-rev10-1.22.0</project.youtube.reporting.version>
<project.http.version>1.20.0</project.http.version>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (c) 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

package com.google.api.services.samples.youtube.cmdline.live;

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.services.samples.youtube.cmdline.Auth;
import com.google.api.services.youtube.YouTube;
import com.google.api.services.youtube.YouTubeScopes;
import com.google.common.collect.Lists;
import java.io.IOException;
import java.util.List;

/**
* Delets a message from a live broadcast, using OAuth 2.0 to authorize API requests.
*
* @author Jim Rogers
*/
public class DeleteLiveChatMessage {

/**
* Define a global instance of a Youtube object, which will be used
* to make YouTube Data API requests.
*/
private static YouTube youtube;

/**
* Deletes a message from a live broadcast.
*
* @param args The message id to delete (required) followed by the videoId (optional). If the
* videoId is given, live chat messages will be retrieved from the chat associated with this
* video. If the videoId is not specified, the signed in user's current live broadcast will be
* used instead.
*/
public static void main(String[] args) {
// Get the message id to delete
if (args.length == 0) {
System.err.println("No message id specified");
System.exit(1);
}
String messageId = args[0];

// This OAuth 2.0 access scope allows for write access to the authenticated user's account.
List<String> scopes = Lists.newArrayList(YouTubeScopes.YOUTUBE_FORCE_SSL);

try {
// Authorize the request.
Credential credential = Auth.authorize(scopes, "deletelivechatmessage");

// This object is used to make YouTube Data API requests.
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential)
.setApplicationName("youtube-cmdline-deletechatmessages-sample").build();

// Delete the message from live chat
YouTube.LiveChatMessages.Delete liveChatDelete =
youtube.liveChatMessages().delete(messageId);
liveChatDelete.execute();
System.out.println("Deleted message id " + messageId);
} 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();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* Copyright (c) 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

package com.google.api.services.samples.youtube.cmdline.live;

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.services.samples.youtube.cmdline.Auth;
import com.google.api.services.youtube.YouTube;
import com.google.api.services.youtube.YouTubeScopes;
import com.google.api.services.youtube.model.LiveBroadcast;
import com.google.api.services.youtube.model.LiveBroadcastListResponse;
import com.google.api.services.youtube.model.Video;
import com.google.api.services.youtube.model.VideoListResponse;
import com.google.common.collect.Lists;
import java.io.IOException;
import java.util.List;

/**
* Gets a live chat id from a video id or current signed in user.
*
* The videoId is often included in the video's url, e.g.:
* https://www.youtube.com/watch?v=L5Xc93_ZL60
* ^ videoId
* The video URL may be found in the browser address bar, or by right-clicking a video and selecting
* Copy video URL from the context menu.
*
* @author Jim Rogers
*/
public class GetLiveChatId {

/**
* Define a global instance of a Youtube object, which will be used
* to make YouTube Data API requests.
*/
private static YouTube youtube;

/**
* Poll live chat messages and SuperChat details from a live broadcast.
*
* @param args videoId (optional). If the videoId is given, live chat messages will be retrieved
* from the chat associated with this video. If the videoId is not specified, the signed in
* user's current live broadcast will be used instead.
*/
public static void main(String[] args) {

// This OAuth 2.0 access scope allows for read-only access to the
// authenticated user's account, but not other types of account access.
List<String> scopes = Lists.newArrayList(YouTubeScopes.YOUTUBE_READONLY);

try {
// Authorize the request.
Credential credential = Auth.authorize(scopes, "getlivechatid");

// This object is used to make YouTube Data API requests.
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential)
.setApplicationName("youtube-cmdline-getlivechatid-sample").build();

// Get the liveChatId
String liveChatId = args.length == 1
? getLiveChatId(youtube, args[0])
: getLiveChatId(youtube);
if (liveChatId != null) {
System.out.println("Live chat id: " + liveChatId);
} else {
System.err.println("Unable to find a live chat id");
System.exit(1);
}
} 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();
}
}

/**
* Retrieves the liveChatId from the authenticated user's live broadcast.
*
* @param youtube The object is used to make YouTube Data API requests.
* @return A liveChatId, or null if not found.
*/
static String getLiveChatId(YouTube youtube) throws IOException {
// Get signed in user's liveChatId
YouTube.LiveBroadcasts.List broadcastList = youtube
.liveBroadcasts()
.list("snippet")
.setFields("items/snippet/liveChatId")
.setBroadcastType("all")
.setBroadcastStatus("active");
LiveBroadcastListResponse broadcastListResponse = broadcastList.execute();
for (LiveBroadcast b : broadcastListResponse.getItems()) {
String liveChatId = b.getSnippet().getLiveChatId();
if (liveChatId != null && !liveChatId.isEmpty()) {
return liveChatId;
}
}

return null;
}

/**
* Retrieves the liveChatId from the broadcast associated with a videoId.
*
* @param youtube The object is used to make YouTube Data API requests.
* @param videoId The videoId associated with the live broadcast.
* @return A liveChatId, or null if not found.
*/
static String getLiveChatId(YouTube youtube, String videoId) throws IOException {
// Get liveChatId from the video
YouTube.Videos.List videoList = youtube.videos()
.list("liveStreamingDetails")
.setFields("items/liveStreamingDetails/activeLiveChatId")
.setId(videoId);
VideoListResponse response = videoList.execute();
for (Video v : response.getItems()) {
String liveChatId = v.getLiveStreamingDetails().getActiveLiveChatId();
if (liveChatId != null && !liveChatId.isEmpty()) {
return liveChatId;
}
}

return null;
}
}
Loading

0 comments on commit 48b4230

Please sign in to comment.