|
| 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 com.google.api.client.auth.oauth2.Credential; |
| 18 | +import com.google.api.client.googleapis.json.GoogleJsonResponseException; |
| 19 | +import com.google.api.client.googleapis.media.MediaHttpUploader; |
| 20 | +import com.google.api.client.googleapis.media.MediaHttpUploaderProgressListener; |
| 21 | +import com.google.api.client.http.InputStreamContent; |
| 22 | +import com.google.api.services.samples.youtube.cmdline.Auth; |
| 23 | +import com.google.api.services.youtube.YouTube; |
| 24 | +import com.google.api.services.youtube.YouTube.Thumbnails.Set; |
| 25 | +import com.google.api.services.youtube.model.ThumbnailSetResponse; |
| 26 | +import com.google.common.collect.Lists; |
| 27 | + |
| 28 | +import java.io.*; |
| 29 | +import java.util.List; |
| 30 | + |
| 31 | +/** |
| 32 | + * This sample uses MediaHttpUploader to upload an image and then calls the |
| 33 | + * API's youtube.thumbnails.set method to set the image as the custom thumbnail |
| 34 | + * for a video. |
| 35 | + * |
| 36 | + * @author Ibrahim Ulukaya |
| 37 | + */ |
| 38 | +public class UploadThumbnail { |
| 39 | + |
| 40 | + /** |
| 41 | + * Define a global instance of a Youtube object, which will be used |
| 42 | + * to make YouTube Data API requests. |
| 43 | + */ |
| 44 | + private static YouTube youtube; |
| 45 | + |
| 46 | + /** |
| 47 | + * Define a global variable that specifies the MIME type of the image |
| 48 | + * being uploaded. |
| 49 | + */ |
| 50 | + private static final String IMAGE_FILE_FORMAT = "image/png"; |
| 51 | + |
| 52 | + /** |
| 53 | + * Prompt the user to specify a video ID and the path for a thumbnail |
| 54 | + * image. Then call the API to set the image as the thumbnail for the video. |
| 55 | + * |
| 56 | + * @param args command line args (not used). |
| 57 | + */ |
| 58 | + public static void main(String[] args) { |
| 59 | + |
| 60 | + // This OAuth 2.0 access scope allows for full read/write access to the |
| 61 | + // authenticated user's account. |
| 62 | + List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube"); |
| 63 | + |
| 64 | + try { |
| 65 | + // Authorize the request. |
| 66 | + Credential credential = Auth.authorize(scopes, "uploadthumbnail"); |
| 67 | + |
| 68 | + // This object is used to make YouTube Data API requests. |
| 69 | + youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).setApplicationName( |
| 70 | + "youtube-cmdline-uploadthumbnail-sample").build(); |
| 71 | + |
| 72 | + // Prompt the user to enter the video ID of the video being updated. |
| 73 | + String videoId = getVideoIdFromUser(); |
| 74 | + System.out.println("You chose " + videoId + " to upload a thumbnail."); |
| 75 | + |
| 76 | + // Prompt the user to specify the location of the thumbnail image. |
| 77 | + File imageFile = getImageFromUser(); |
| 78 | + System.out.println("You chose " + imageFile + " to upload."); |
| 79 | + |
| 80 | + // Create an object that contains the thumbnail image file's |
| 81 | + // contents. |
| 82 | + InputStreamContent mediaContent = new InputStreamContent( |
| 83 | + IMAGE_FILE_FORMAT, new BufferedInputStream(new FileInputStream(imageFile))); |
| 84 | + mediaContent.setLength(imageFile.length()); |
| 85 | + |
| 86 | + // Create an API request that specifies that the mediaContent |
| 87 | + // object is the thumbnail of the specified video. |
| 88 | + Set thumbnailSet = youtube.thumbnails().set(videoId, mediaContent); |
| 89 | + |
| 90 | + // Set the upload type and add an event listener. |
| 91 | + MediaHttpUploader uploader = thumbnailSet.getMediaHttpUploader(); |
| 92 | + |
| 93 | + // Indicate whether direct media upload is enabled. A value of |
| 94 | + // "True" indicates that direct media upload is enabled and that |
| 95 | + // the entire media content will be uploaded in a single request. |
| 96 | + // A value of "False," which is the default, indicates that the |
| 97 | + // request will use the resumable media upload protocol, which |
| 98 | + // supports the ability to resume an upload operation after a |
| 99 | + // network interruption or other transmission failure, saving |
| 100 | + // time and bandwidth in the event of network failures. |
| 101 | + uploader.setDirectUploadEnabled(false); |
| 102 | + |
| 103 | + // Set the upload state for the thumbnail image. |
| 104 | + MediaHttpUploaderProgressListener progressListener = new MediaHttpUploaderProgressListener() { |
| 105 | + @Override |
| 106 | + public void progressChanged(MediaHttpUploader uploader) throws IOException { |
| 107 | + switch (uploader.getUploadState()) { |
| 108 | + // This value is set before the initiation request is |
| 109 | + // sent. |
| 110 | + case INITIATION_STARTED: |
| 111 | + System.out.println("Initiation Started"); |
| 112 | + break; |
| 113 | + // This value is set after the initiation request |
| 114 | + // completes. |
| 115 | + case INITIATION_COMPLETE: |
| 116 | + System.out.println("Initiation Completed"); |
| 117 | + break; |
| 118 | + // This value is set after a media file chunk is |
| 119 | + // uploaded. |
| 120 | + case MEDIA_IN_PROGRESS: |
| 121 | + System.out.println("Upload in progress"); |
| 122 | + System.out.println("Upload percentage: " + uploader.getProgress()); |
| 123 | + break; |
| 124 | + // This value is set after the entire media file has |
| 125 | + // been successfully uploaded. |
| 126 | + case MEDIA_COMPLETE: |
| 127 | + System.out.println("Upload Completed!"); |
| 128 | + break; |
| 129 | + // This value indicates that the upload process has |
| 130 | + // not started yet. |
| 131 | + case NOT_STARTED: |
| 132 | + System.out.println("Upload Not Started!"); |
| 133 | + break; |
| 134 | + } |
| 135 | + } |
| 136 | + }; |
| 137 | + uploader.setProgressListener(progressListener); |
| 138 | + |
| 139 | + // Upload the image and set it as the specified video's thumbnail. |
| 140 | + ThumbnailSetResponse setResponse = thumbnailSet.execute(); |
| 141 | + |
| 142 | + // Print the URL for the updated video's thumbnail image. |
| 143 | + System.out.println("\n================== Uploaded Thumbnail ==================\n"); |
| 144 | + System.out.println(" - Url: " + setResponse.getItems().get(0).getDefault().getUrl()); |
| 145 | + |
| 146 | + } catch (GoogleJsonResponseException e) { |
| 147 | + System.err.println("GoogleJsonResponseException code: " + e.getDetails().getCode() + " : " |
| 148 | + + e.getDetails().getMessage()); |
| 149 | + e.printStackTrace(); |
| 150 | + |
| 151 | + } catch (IOException e) { |
| 152 | + System.err.println("IOException: " + e.getMessage()); |
| 153 | + e.printStackTrace(); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + /* |
| 158 | + * Prompts the user to enter a YouTube video ID and return the user input. |
| 159 | + */ |
| 160 | + private static String getVideoIdFromUser() throws IOException { |
| 161 | + |
| 162 | + String inputVideoId = ""; |
| 163 | + |
| 164 | + System.out.print("Please enter a video Id to update: "); |
| 165 | + BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in)); |
| 166 | + inputVideoId = bReader.readLine(); |
| 167 | + |
| 168 | + if (inputVideoId.length() < 1) { |
| 169 | + // Exit if the user does not specify a video ID. |
| 170 | + System.out.print("Video Id can't be empty!"); |
| 171 | + System.exit(1); |
| 172 | + } |
| 173 | + |
| 174 | + return inputVideoId; |
| 175 | + } |
| 176 | + |
| 177 | + /* |
| 178 | + * Prompt the user to enter the path for the thumbnail image being uploaded. |
| 179 | + */ |
| 180 | + private static File getImageFromUser() throws IOException { |
| 181 | + |
| 182 | + String path = ""; |
| 183 | + |
| 184 | + System.out.print("Please enter the path of the image file to upload: "); |
| 185 | + BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in)); |
| 186 | + path = bReader.readLine(); |
| 187 | + |
| 188 | + if (path.length() < 1) { |
| 189 | + // Exit if the user does not provide a path to the image file. |
| 190 | + System.out.print("Path can not be empty!"); |
| 191 | + System.exit(1); |
| 192 | + } |
| 193 | + |
| 194 | + return new File(path); |
| 195 | + } |
| 196 | +} |
0 commit comments