Skip to content

Commit 2ed8fa7

Browse files
"Added sample: java/src/main/java/com/google/api/services/samples/youtube/cmdline/data/ChannelLocalizations.java"
1 parent e19c807 commit 2ed8fa7

File tree

1 file changed

+327
-0
lines changed

1 file changed

+327
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,327 @@
1+
/*
2+
* Copyright (c) 2015 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.util.ArrayMap;
20+
import com.google.api.services.samples.youtube.cmdline.Auth;
21+
import com.google.api.services.youtube.YouTube;
22+
import com.google.api.services.youtube.model.Channel;
23+
import com.google.api.services.youtube.model.ChannelListResponse;
24+
import com.google.api.services.youtube.model.ChannelLocalization;
25+
import com.google.common.collect.Lists;
26+
27+
import java.io.BufferedReader;
28+
import java.io.IOException;
29+
import java.io.InputStreamReader;
30+
import java.util.List;
31+
import java.util.Map;
32+
33+
/**
34+
* This sample sets and retrieves localized metadata for a channel by:
35+
*
36+
* 1. Updating language of the default metadata and setting localized metadata
37+
* for a channel via "channels.update" method.
38+
* 2. Getting the localized metadata for a channel in a selected language using the
39+
* "channels.list" method and setting the "hl" parameter.
40+
* 3. Listing the localized metadata for a channel using "channels.list" method and
41+
* including "localizations" in the "part" parameter.
42+
*
43+
* @author Ibrahim Ulukaya
44+
*/
45+
public class ChannelLocalizations {
46+
47+
/**
48+
* Define a global instance of a YouTube object, which will be used to make
49+
* YouTube Data API requests.
50+
*/
51+
private static YouTube youtube;
52+
53+
54+
/**
55+
* Set and retrieve localized metadata for a channel.
56+
*
57+
* @param args command line args (not used).
58+
*/
59+
public static void main(String[] args) {
60+
61+
// This OAuth 2.0 access scope allows for full read/write access to the
62+
// authenticated user's account.
63+
List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube");
64+
65+
try {
66+
// Authorize the request.
67+
Credential credential = Auth.authorize(scopes, "localizations");
68+
69+
// This object is used to make YouTube Data API requests.
70+
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential)
71+
.setApplicationName("youtube-cmdline-localizations-sample").build();
72+
73+
// Prompt the user to specify the action of the be achieved.
74+
String actionString = getActionFromUser();
75+
System.out.println("You chose " + actionString + ".");
76+
//Map the user input to the enum values.
77+
Action action = Action.valueOf(actionString.toUpperCase());
78+
79+
switch (action) {
80+
case SET:
81+
setChannelLocalization(getId("channel"), getDefaultLanguage(),
82+
getLanguage(), getMetadata("description"));
83+
break;
84+
case GET:
85+
getChannelLocalization(getId("channel"), getLanguage());
86+
break;
87+
case LIST:
88+
listChannelLocalizations(getId("channel"));
89+
break;
90+
}
91+
} catch (GoogleJsonResponseException e) {
92+
System.err.println("GoogleJsonResponseException code: " + e.getDetails().getCode()
93+
+ " : " + e.getDetails().getMessage());
94+
e.printStackTrace();
95+
96+
} catch (IOException e) {
97+
System.err.println("IOException: " + e.getMessage());
98+
e.printStackTrace();
99+
} catch (Throwable t) {
100+
System.err.println("Throwable: " + t.getMessage());
101+
t.printStackTrace();
102+
}
103+
}
104+
105+
/**
106+
* Updates a channel's default language and sets its localized metadata.
107+
*
108+
* @param channelId The id parameter specifies the channel ID for the resource
109+
* that is being updated.
110+
* @param defaultLanguage The language of the channel's default metadata
111+
* @param language The language of the localized metadata
112+
* @param description The localized description to be set
113+
* @throws IOException
114+
*/
115+
private static void setChannelLocalization(String channelId, String defaultLanguage,
116+
String language, String description) throws IOException {
117+
// Call the YouTube Data API's channels.list method to retrieve channels.
118+
ChannelListResponse channelListResponse = youtube.channels().
119+
list("brandingSettings,localizations").setId(channelId).execute();
120+
121+
// Since the API request specified a unique channel ID, the API
122+
// response should return exactly one channel. If the response does
123+
// not contain a channel, then the specified channel ID was not found.
124+
List<Channel> channelList = channelListResponse.getItems();
125+
if (channelList.isEmpty()) {
126+
System.out.println("Can't find a channel with ID: " + channelId);
127+
return;
128+
}
129+
Channel channel = channelList.get(0);
130+
131+
// Modify channel's default language and localizations properties.
132+
// Ensure that a value is set for the resource's snippet.defaultLanguage property.
133+
// To set the snippet.defaultLanguage property for a channel resource,
134+
// you actually need to update the brandingSettings.channel.defaultLanguage property.
135+
channel.getBrandingSettings().getChannel().setDefaultLanguage(defaultLanguage);
136+
137+
// Preserve any localizations already associated with the channel. If the
138+
// channel does not have any localizations, create a new array. Append the
139+
// provided localization to the list of localizations associated with the channel.
140+
Map<String, ChannelLocalization> localizations = channel.getLocalizations();
141+
if (localizations == null) {
142+
localizations = new ArrayMap<String, ChannelLocalization>();
143+
channel.setLocalizations(localizations);
144+
}
145+
ChannelLocalization channelLocalization = new ChannelLocalization();
146+
channelLocalization.setDescription(description);
147+
localizations.put(language, channelLocalization);
148+
149+
// Update the channel resource by calling the channels.update() method.
150+
Channel channelResponse = youtube.channels()
151+
.update("brandingSettings,localizations", channel).execute();
152+
153+
// Print information from the API response.
154+
System.out.println("\n================== Updated Channel ==================\n");
155+
System.out.println(" - ID: " + channelResponse.getId());
156+
System.out.println(" - Default Language: " +
157+
channelResponse.getSnippet().getDefaultLanguage());
158+
System.out.println(" - Description(" + language + "): " +
159+
channelResponse.getLocalizations().get(language).getDescription());
160+
System.out.println("\n-------------------------------------------------------------\n");
161+
}
162+
163+
/**
164+
* Returns localized metadata for a channel in a selected language.
165+
* If the localized text is not available in the requested language,
166+
* this method will return text in the default language.
167+
*
168+
* @param channelId The id parameter specifies the channel ID for the resource
169+
* that is being updated.
170+
* @param language The language of the localized metadata
171+
* @throws IOException
172+
*/
173+
private static void getChannelLocalization(String channelId, String language)
174+
throws IOException {
175+
// Call the YouTube Data API's channels.list method to retrieve channels.
176+
ChannelListResponse channelListResponse = youtube.channels().
177+
list("snippet").setId(channelId).set("hl", language).execute();
178+
179+
// Since the API request specified a unique channel ID, the API
180+
// response should return exactly one channel. If the response does
181+
// not contain a channel, then the specified channel ID was not found.
182+
List<Channel> channelList = channelListResponse.getItems();
183+
if (channelList.isEmpty()) {
184+
System.out.println("Can't find a channel with ID: " + channelId);
185+
return;
186+
}
187+
Channel channel = channelList.get(0);
188+
189+
// Print information from the API response.
190+
System.out.println("\n================== Channel ==================\n");
191+
System.out.println(" - ID: " + channel.getId());
192+
System.out.println(" - Description(" + language + "): " +
193+
channel.getLocalizations().get(language).getDescription());
194+
System.out.println("\n-------------------------------------------------------------\n");
195+
}
196+
197+
/**
198+
* Returns a list of localized metadata for a channel.
199+
*
200+
* @param channelId The id parameter specifies the channel ID for the resource
201+
* that is being updated.
202+
* @throws IOException
203+
*/
204+
private static void listChannelLocalizations(String channelId) throws IOException {
205+
// Call the YouTube Data API's channels.list method to retrieve channels.
206+
ChannelListResponse channelListResponse = youtube.channels().
207+
list("snippet,localizations").setId(channelId).execute();
208+
209+
// Since the API request specified a unique channel ID, the API
210+
// response should return exactly one channel. If the response does
211+
// not contain a channel, then the specified channel ID was not found.
212+
List<Channel> channelList = channelListResponse.getItems();
213+
if (channelList.isEmpty()) {
214+
System.out.println("Can't find a channel with ID: " + channelId);
215+
return;
216+
}
217+
Channel channel = channelList.get(0);
218+
Map<String, ChannelLocalization> localizations = channel.getLocalizations();
219+
220+
// Print information from the API response.
221+
System.out.println("\n================== Channel ==================\n");
222+
System.out.println(" - ID: " + channel.getId());
223+
for (String language : localizations.keySet()) {
224+
System.out.println(" - Description(" + language + "): " +
225+
localizations.get(language).getDescription());
226+
}
227+
System.out.println("\n-------------------------------------------------------------\n");
228+
}
229+
230+
/*
231+
* Prompt the user to enter a resource ID. Then return the ID.
232+
*/
233+
private static String getId(String resource) throws IOException {
234+
235+
String id = "";
236+
237+
System.out.print("Please enter a " + resource + " id: ");
238+
BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
239+
id = bReader.readLine();
240+
241+
System.out.println("You chose " + id + " for localizations.");
242+
return id;
243+
}
244+
245+
/*
246+
* Prompt the user to enter the localized metadata. Then return the metadata.
247+
*/
248+
private static String getMetadata(String type) throws IOException {
249+
250+
String metadata = "";
251+
252+
System.out.print("Please enter a localized " + type + ": ");
253+
BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
254+
metadata = bReader.readLine();
255+
256+
if (metadata.length() < 1) {
257+
// If nothing is entered, defaults to type.
258+
metadata = type + "(localized)";
259+
}
260+
261+
System.out.println("You chose " + metadata + " as localized "+ type + ".");
262+
return metadata;
263+
}
264+
265+
/*
266+
* Prompt the user to enter the language for the resource's default metadata.
267+
* Then return the language.
268+
*/
269+
private static String getDefaultLanguage() throws IOException {
270+
271+
String defaultlanguage = "";
272+
273+
System.out.print("Please enter the language for the resource's default metadata: ");
274+
BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
275+
defaultlanguage = bReader.readLine();
276+
277+
if (defaultlanguage.length() < 1) {
278+
// If nothing is entered, defaults to "en".
279+
defaultlanguage = "en";
280+
}
281+
282+
System.out.println("You chose " + defaultlanguage +
283+
" as the language for the resource's default metadata.");
284+
return defaultlanguage;
285+
}
286+
287+
/*
288+
* Prompt the user to enter a language for the localized metadata. Then return the language.
289+
*/
290+
private static String getLanguage() throws IOException {
291+
292+
String language = "";
293+
294+
System.out.print("Please enter the localized metadata language: ");
295+
BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
296+
language = bReader.readLine();
297+
298+
if (language.length() < 1) {
299+
// If nothing is entered, defaults to "de".
300+
language = "de";
301+
}
302+
303+
System.out.println("You chose " + language + " as the localized metadata language.");
304+
return language;
305+
}
306+
307+
/*
308+
* Prompt the user to enter an action. Then return the action.
309+
*/
310+
private static String getActionFromUser() throws IOException {
311+
312+
String action = "";
313+
314+
System.out.print("Please choose action to be accomplished: ");
315+
System.out.print("Options are: 'set', 'get' and 'list' ");
316+
BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
317+
action = bReader.readLine();
318+
319+
return action;
320+
}
321+
322+
public enum Action {
323+
SET,
324+
GET,
325+
LIST
326+
}
327+
}

0 commit comments

Comments
 (0)