Skip to content

Commit e1e9901

Browse files
"Added sample: java/src/main/java/com/google/api/services/samples/youtube/cmdline/data/PlaylistLocalizations.java"
1 parent 3acd792 commit e1e9901

File tree

1 file changed

+332
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)