|
| 1 | +/* |
| 2 | + * Copyright (c) 2017 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.live; |
| 16 | + |
| 17 | +import com.google.api.client.auth.oauth2.Credential; |
| 18 | +import com.google.api.client.googleapis.json.GoogleJsonResponseException; |
| 19 | +import com.google.api.services.samples.youtube.cmdline.Auth; |
| 20 | +import com.google.api.services.youtube.YouTube; |
| 21 | +import com.google.api.services.youtube.YouTubeScopes; |
| 22 | +import com.google.api.services.youtube.model.LiveBroadcast; |
| 23 | +import com.google.api.services.youtube.model.LiveBroadcastListResponse; |
| 24 | +import com.google.api.services.youtube.model.Video; |
| 25 | +import com.google.api.services.youtube.model.VideoListResponse; |
| 26 | +import com.google.common.collect.Lists; |
| 27 | +import java.io.IOException; |
| 28 | +import java.util.List; |
| 29 | + |
| 30 | +/** |
| 31 | + * Gets a live chat id from a video id or current signed in user. |
| 32 | + * |
| 33 | + * The videoId is often included in the video's url, e.g.: |
| 34 | + * https://www.youtube.com/watch?v=L5Xc93_ZL60 |
| 35 | + * ^ videoId |
| 36 | + * The video URL may be found in the browser address bar, or by right-clicking a video and selecting |
| 37 | + * Copy video URL from the context menu. |
| 38 | + * |
| 39 | + * @author Jim Rogers |
| 40 | + */ |
| 41 | +public class GetLiveChatId { |
| 42 | + |
| 43 | + /** |
| 44 | + * Define a global instance of a Youtube object, which will be used |
| 45 | + * to make YouTube Data API requests. |
| 46 | + */ |
| 47 | + private static YouTube youtube; |
| 48 | + |
| 49 | + /** |
| 50 | + * Poll live chat messages and SuperChat details from a live broadcast. |
| 51 | + * |
| 52 | + * @param args videoId (optional). If the videoId is given, live chat messages will be retrieved |
| 53 | + * from the chat associated with this video. If the videoId is not specified, the signed in |
| 54 | + * user's current live broadcast will be used instead. |
| 55 | + */ |
| 56 | + public static void main(String[] args) { |
| 57 | + |
| 58 | + // This OAuth 2.0 access scope allows for read-only access to the |
| 59 | + // authenticated user's account, but not other types of account access. |
| 60 | + List<String> scopes = Lists.newArrayList(YouTubeScopes.YOUTUBE_READONLY); |
| 61 | + |
| 62 | + try { |
| 63 | + // Authorize the request. |
| 64 | + Credential credential = Auth.authorize(scopes, "getlivechatid"); |
| 65 | + |
| 66 | + // This object is used to make YouTube Data API requests. |
| 67 | + youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential) |
| 68 | + .setApplicationName("youtube-cmdline-getlivechatid-sample").build(); |
| 69 | + |
| 70 | + // Get the liveChatId |
| 71 | + String liveChatId = args.length == 1 |
| 72 | + ? getLiveChatId(youtube, args[0]) |
| 73 | + : getLiveChatId(youtube); |
| 74 | + if (liveChatId != null) { |
| 75 | + System.out.println("Live chat id: " + liveChatId); |
| 76 | + } else { |
| 77 | + System.err.println("Unable to find a live chat id"); |
| 78 | + System.exit(1); |
| 79 | + } |
| 80 | + } catch (GoogleJsonResponseException e) { |
| 81 | + System.err |
| 82 | + .println("GoogleJsonResponseException code: " + e.getDetails().getCode() + " : " |
| 83 | + + e.getDetails().getMessage()); |
| 84 | + e.printStackTrace(); |
| 85 | + |
| 86 | + } catch (IOException e) { |
| 87 | + System.err.println("IOException: " + e.getMessage()); |
| 88 | + e.printStackTrace(); |
| 89 | + } catch (Throwable t) { |
| 90 | + System.err.println("Throwable: " + t.getMessage()); |
| 91 | + t.printStackTrace(); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Retrieves the liveChatId from the authenticated user's live broadcast. |
| 97 | + * |
| 98 | + * @param youtube The object is used to make YouTube Data API requests. |
| 99 | + * @return A liveChatId, or null if not found. |
| 100 | + */ |
| 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; |
| 114 | + } |
| 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; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + return null; |
| 142 | + } |
| 143 | +} |
0 commit comments