Skip to content

Commit 361a1ef

Browse files
"Added sample: java/src/main/java/com/google/api/services/samples/youtube/cmdline/data/ChannelSectionLocalizations.java"
1 parent 870ac82 commit 361a1ef

File tree

1 file changed

+326
-0
lines changed

1 file changed

+326
-0
lines changed
Lines changed: 326 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,326 @@
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.ChannelSection;
23+
import com.google.api.services.youtube.model.ChannelSectionListResponse;
24+
import com.google.api.services.youtube.model.ChannelSectionLocalization;
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 section by:
35+
*
36+
* 1. Updating language of the default metadata and setting localized metadata
37+
* for a channel section via "channelSections.update" method.
38+
* 2. Getting the localized metadata for a channel section in a selected language using the
39+
* "channelSections.list" method and setting the "hl" parameter.
40+
* 3. Listing the localized metadata for a channel section using the "channelSections.list" method
41+
* and including "localizations" in the "part" parameter.
42+
*
43+
* @author Ibrahim Ulukaya
44+
*/
45+
public class ChannelSectionLocalizations {
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 section.
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+
setChannelSectionLocalization(getId("channel section"),
82+
getDefaultLanguage(), getLanguage(), getMetadata("title"));
83+
break;
84+
case GET:
85+
getChannelSectionLocalization(getId("channel section"), getLanguage());
86+
break;
87+
case LIST:
88+
listChannelSectionLocalizations(getId("channel section"));
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 section's default language and sets its localized metadata.
107+
*
108+
* @param channelSectionId The id parameter specifies the channel section ID for the resource
109+
* that is being updated.
110+
* @param defaultLanguage The language of the channel section's default metadata
111+
* @param language The language of the localized metadata
112+
* @param title The localized title to be set
113+
* @throws IOException
114+
*/
115+
private static void setChannelSectionLocalization(String channelSectionId,
116+
String defaultLanguage, String language, String title)
117+
throws IOException {
118+
// Call the YouTube Data API's channelSections.list method to retrieve channel sections.
119+
ChannelSectionListResponse channelSectionListResponse = youtube.channelSections().
120+
list("snippet,localizations").setId(channelSectionId).execute();
121+
122+
// Since the API request specified a unique channel section ID, the API
123+
// response should return exactly one channel section. If the response does
124+
// not contain a channel section, then the specified channel section ID was not found.
125+
List<ChannelSection> channelSectionList = channelSectionListResponse.getItems();
126+
if (channelSectionList.isEmpty()) {
127+
System.out.println("Can't find a channel section with ID: " + channelSectionId);
128+
return;
129+
}
130+
ChannelSection channelSection = channelSectionList.get(0);
131+
132+
// Modify channel section's default language and localizations properties.
133+
// Ensure that a value is set for the resource's snippet.defaultLanguage property.
134+
channelSection.getSnippet().setDefaultLanguage(defaultLanguage);
135+
136+
// Preserve any localizations already associated with the channel section. If the
137+
// channel section does not have any localizations, create a new array. Append the
138+
// provided localization to the list of localizations associated with the channel section.
139+
Map<String, ChannelSectionLocalization> localizations = channelSection.getLocalizations();
140+
if (localizations == null) {
141+
localizations = new ArrayMap<String, ChannelSectionLocalization>();
142+
channelSection.setLocalizations(localizations);
143+
}
144+
ChannelSectionLocalization channelSectionLocalization = new ChannelSectionLocalization();
145+
channelSectionLocalization.setTitle(title);
146+
localizations.put(language, channelSectionLocalization);
147+
148+
// Update the channel section resource by calling the channelSections.update() method.
149+
ChannelSection channelSectionResponse = youtube.channelSections()
150+
.update("snippet,localizations", channelSection).execute();
151+
152+
// Print information from the API response.
153+
System.out.println("\n================== Updated Channel Section ==================\n");
154+
System.out.println(" - ID: " + channelSectionResponse.getId());
155+
System.out.println(" - Default Language: " +
156+
channelSectionResponse.getSnippet().getDefaultLanguage());
157+
System.out.println(" - Title(" + language + "): " +
158+
channelSectionResponse.getLocalizations().get(language).getTitle());
159+
System.out.println("\n-------------------------------------------------------------\n");
160+
}
161+
162+
/**
163+
* Returns localized metadata for a channel section in a selected language.
164+
* If the localized text is not available in the requested language,
165+
* this method will return text in the default language.
166+
*
167+
* @param channelSectionId The id parameter specifies the channel section ID for the resource
168+
* that is being updated.
169+
* @param language The language of the localized metadata
170+
* @throws IOException
171+
*/
172+
private static void getChannelSectionLocalization(String channelSectionId, String language)
173+
throws IOException {
174+
// Call the YouTube Data API's channelSections.list method to retrieve channel sections.
175+
ChannelSectionListResponse channelSectionListResponse = youtube.channelSections().
176+
list("snippet").setId(channelSectionId).set("hl", language).execute();
177+
178+
// Since the API request specified a unique channel ID, the API
179+
// response should return exactly one channel. If the response does
180+
// not contain a channel, then the specified channel ID was not found.
181+
List<ChannelSection> channelSectionList = channelSectionListResponse.getItems();
182+
if (channelSectionList.isEmpty()) {
183+
System.out.println("Can't find a channel section with ID: " + channelSectionId);
184+
return;
185+
}
186+
ChannelSection channelSection = channelSectionList.get(0);
187+
188+
// Print information from the API response.
189+
System.out.println("\n================== Channel Section==================\n");
190+
System.out.println(" - ID: " + channelSection.getId());
191+
System.out.println(" - Title(" + language + "): " +
192+
channelSection.getLocalizations().get(language).getTitle());
193+
System.out.println("\n-------------------------------------------------------------\n");
194+
}
195+
196+
/**
197+
* Returns a list of localized metadata for a channel section.
198+
*
199+
* @param channelSectionId The id parameter specifies the channel section ID for the resource
200+
* that is being updated.
201+
* @throws IOException
202+
*/
203+
private static void listChannelSectionLocalizations(String channelSectionId) throws IOException {
204+
// Call the YouTube Data API's channelSections.list method to retrieve channel sections.
205+
ChannelSectionListResponse channelSectionListResponse = youtube.channelSections().
206+
list("snippet,localizations").setId(channelSectionId).execute();
207+
208+
// Since the API request specified a unique channel section ID, the API
209+
// response should return exactly one channel section. If the response does
210+
// not contain a channel section, then the specified channel section ID was not found.
211+
List<ChannelSection> channelSectionList = channelSectionListResponse.getItems();
212+
if (channelSectionList.isEmpty()) {
213+
System.out.println("Can't find a channel section with ID: " + channelSectionId);
214+
return;
215+
}
216+
ChannelSection channelSection = channelSectionList.get(0);
217+
Map<String, ChannelSectionLocalization> localizations = channelSection.getLocalizations();
218+
219+
// Print information from the API response.
220+
System.out.println("\n================== Channel ==================\n");
221+
System.out.println(" - ID: " + channelSection.getId());
222+
for (String language : localizations.keySet()) {
223+
System.out.println(" - Title(" + language + "): " +
224+
localizations.get(language).getTitle());
225+
}
226+
System.out.println("\n-------------------------------------------------------------\n");
227+
}
228+
229+
/*
230+
* Prompt the user to enter a resource ID. Then return the ID.
231+
*/
232+
private static String getId(String resource) throws IOException {
233+
234+
String id = "";
235+
236+
System.out.print("Please enter a " + resource + " id: ");
237+
BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
238+
id = bReader.readLine();
239+
240+
System.out.println("You chose " + id + " for localizations.");
241+
return id;
242+
}
243+
244+
/*
245+
* Prompt the user to enter the localized metadata. Then return the metadata.
246+
*/
247+
private static String getMetadata(String type) throws IOException {
248+
249+
String metadata = "";
250+
251+
System.out.print("Please enter a localized " + type + ": ");
252+
BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
253+
metadata = bReader.readLine();
254+
255+
if (metadata.length() < 1) {
256+
// If nothing is entered, defaults to type.
257+
metadata = type + "(localized)";
258+
}
259+
260+
System.out.println("You chose " + metadata + " as localized "+ type + ".");
261+
return metadata;
262+
}
263+
264+
/*
265+
* Prompt the user to enter the language for the resource's default metadata.
266+
* Then return the language.
267+
*/
268+
private static String getDefaultLanguage() throws IOException {
269+
270+
String defaultlanguage = "";
271+
272+
System.out.print("Please enter the language for the resource's default metadata: ");
273+
BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
274+
defaultlanguage = bReader.readLine();
275+
276+
if (defaultlanguage.length() < 1) {
277+
// If nothing is entered, defaults to "en".
278+
defaultlanguage = "en";
279+
}
280+
281+
System.out.println("You chose " + defaultlanguage +
282+
" as the language for the resource's default metadata.");
283+
return defaultlanguage;
284+
}
285+
286+
/*
287+
* Prompt the user to enter a language for the localized metadata. Then return the language.
288+
*/
289+
private static String getLanguage() throws IOException {
290+
291+
String language = "";
292+
293+
System.out.print("Please enter the localized metadata language: ");
294+
BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
295+
language = bReader.readLine();
296+
297+
if (language.length() < 1) {
298+
// If nothing is entered, defaults to "de".
299+
language = "de";
300+
}
301+
302+
System.out.println("You chose " + language + " as the localized metadata language.");
303+
return language;
304+
}
305+
306+
/*
307+
* Prompt the user to enter an action. Then return the action.
308+
*/
309+
private static String getActionFromUser() throws IOException {
310+
311+
String action = "";
312+
313+
System.out.print("Please choose action to be accomplished: ");
314+
System.out.print("Options are: 'set', 'get' and 'list' ");
315+
BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
316+
action = bReader.readLine();
317+
318+
return action;
319+
}
320+
321+
public enum Action {
322+
SET,
323+
GET,
324+
LIST
325+
}
326+
}

0 commit comments

Comments
 (0)