Skip to content

Commit b0661d9

Browse files
"Added sample: java/src/main/java/com/google/api/services/samples/youtube/cmdline/data/UpdateVideo.java"
1 parent 8eb9623 commit b0661d9

File tree

1 file changed

+162
-0
lines changed
  • java/src/main/java/com/google/api/services/samples/youtube/cmdline/data

1 file changed

+162
-0
lines changed
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
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.services.samples.youtube.cmdline.Auth;
20+
import com.google.api.services.youtube.YouTube;
21+
import com.google.api.services.youtube.model.Video;
22+
import com.google.api.services.youtube.model.VideoListResponse;
23+
import com.google.api.services.youtube.model.VideoSnippet;
24+
import com.google.common.collect.Lists;
25+
26+
import java.io.BufferedReader;
27+
import java.io.IOException;
28+
import java.io.InputStreamReader;
29+
import java.util.ArrayList;
30+
import java.util.List;
31+
32+
/**
33+
* Update a video by adding a keyword tag to its metadata. The demo uses the
34+
* YouTube Data API (v3) and OAuth 2.0 for authorization.
35+
*
36+
* @author Ibrahim Ulukaya
37+
*/
38+
public class UpdateVideo {
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+
* Add a keyword tag to a video that the user specifies. Use OAuth 2.0 to
48+
* authorize the API request.
49+
*
50+
* @param args command line args (not used).
51+
*/
52+
public static void main(String[] args) {
53+
54+
// This OAuth 2.0 access scope allows for full read/write access to the
55+
// authenticated user's account.
56+
List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube");
57+
58+
try {
59+
// Authorize the request.
60+
Credential credential = Auth.authorize(scopes, "updatevideo");
61+
62+
// This object is used to make YouTube Data API requests.
63+
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential)
64+
.setApplicationName("youtube-cmdline-updatevideo-sample").build();
65+
66+
// Prompt the user to enter the video ID of the video being updated.
67+
String videoId = getVideoIdFromUser();
68+
System.out.println("You chose " + videoId + " to update.");
69+
70+
// Prompt the user to enter a keyword tag to add to the video.
71+
String tag = getTagFromUser();
72+
System.out.println("You chose " + tag + " as a tag.");
73+
74+
// Call the YouTube Data API's youtube.videos.list method to
75+
// retrieve the resource that represents the specified video.
76+
YouTube.Videos.List listVideosRequest = youtube.videos().list("snippet").setId(videoId);
77+
VideoListResponse listResponse = listVideosRequest.execute();
78+
79+
// Since the API request specified a unique video ID, the API
80+
// response should return exactly one video. If the response does
81+
// not contain a video, then the specified video ID was not found.
82+
List<Video> videoList = listResponse.getItems();
83+
if (videoList.isEmpty()) {
84+
System.out.println("Can't find a video with ID: " + videoId);
85+
return;
86+
}
87+
88+
// Extract the snippet from the video resource.
89+
Video video = videoList.get(0);
90+
VideoSnippet snippet = video.getSnippet();
91+
92+
// Preserve any tags already associated with the video. If the
93+
// video does not have any tags, create a new array. Append the
94+
// provided tag to the list of tags associated with the video.
95+
List<String> tags = snippet.getTags();
96+
if (tags == null) {
97+
tags = new ArrayList<String>(1);
98+
snippet.setTags(tags);
99+
}
100+
tags.add(tag);
101+
102+
// Update the video resource by calling the videos.update() method.
103+
YouTube.Videos.Update updateVideosRequest = youtube.videos().update("snippet", video);
104+
Video videoResponse = updateVideosRequest.execute();
105+
106+
// Print information from the updated resource.
107+
System.out.println("\n================== Returned Video ==================\n");
108+
System.out.println(" - Title: " + videoResponse.getSnippet().getTitle());
109+
System.out.println(" - Tags: " + videoResponse.getSnippet().getTags());
110+
111+
} catch (GoogleJsonResponseException e) {
112+
System.err.println("GoogleJsonResponseException code: " + e.getDetails().getCode() + " : "
113+
+ e.getDetails().getMessage());
114+
e.printStackTrace();
115+
} catch (IOException e) {
116+
System.err.println("IOException: " + e.getMessage());
117+
e.printStackTrace();
118+
} catch (Throwable t) {
119+
System.err.println("Throwable: " + t.getMessage());
120+
t.printStackTrace();
121+
}
122+
}
123+
124+
/*
125+
* Prompt the user to enter a keyword tag.
126+
*/
127+
private static String getTagFromUser() throws IOException {
128+
129+
String keyword = "";
130+
131+
System.out.print("Please enter a tag for your video: ");
132+
BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
133+
keyword = bReader.readLine();
134+
135+
if (keyword.length() < 1) {
136+
// If the user doesn't enter a tag, use the default value "New Tag."
137+
keyword = "New Tag";
138+
}
139+
return keyword;
140+
}
141+
142+
/*
143+
* Prompt the user to enter a video ID.
144+
*/
145+
private static String getVideoIdFromUser() throws IOException {
146+
147+
String videoId = "";
148+
149+
System.out.print("Please enter a video Id to update: ");
150+
BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
151+
videoId = bReader.readLine();
152+
153+
if (videoId.length() < 1) {
154+
// Exit if the user doesn't provide a value.
155+
System.out.print("Video Id can't be empty!");
156+
System.exit(1);
157+
}
158+
159+
return videoId;
160+
}
161+
162+
}

0 commit comments

Comments
 (0)