Search in sources :

Example 1 with PlaylistLocalization

use of com.google.api.services.youtube.model.PlaylistLocalization in project api-samples by youtube.

the class PlaylistLocalizations method listPlaylistLocalizations.

/**
     * Returns a list of localized metadata for a playlist.
     *
     * @param playlistId The id parameter specifies the playlist ID for the resource
     * that is being updated.
     * @throws IOException
     */
private static void listPlaylistLocalizations(String playlistId) throws IOException {
    // Call the YouTube Data API's playlists.list method to retrieve playlists.
    PlaylistListResponse playlistListResponse = youtube.playlists().list("snippet,localizations").setId(playlistId).execute();
    // Since the API request specified a unique playlist ID, the API
    // response should return exactly one playlist. If the response does
    // not contain a playlist, then the specified playlist ID was not found.
    List<Playlist> playlistList = playlistListResponse.getItems();
    if (playlistList.isEmpty()) {
        System.out.println("Can't find a playlist with ID: " + playlistId);
        return;
    }
    Playlist playlist = playlistList.get(0);
    Map<String, PlaylistLocalization> localizations = playlist.getLocalizations();
    // Print information from the API response.
    System.out.println("\n================== Playlist ==================\n");
    System.out.println("  - ID: " + playlist.getId());
    for (String language : localizations.keySet()) {
        System.out.println("  - Title(" + language + "): " + localizations.get(language).getTitle());
        System.out.println("  - Description(" + language + "): " + localizations.get(language).getDescription());
    }
    System.out.println("\n-------------------------------------------------------------\n");
}
Also used : Playlist(com.google.api.services.youtube.model.Playlist) PlaylistListResponse(com.google.api.services.youtube.model.PlaylistListResponse) PlaylistLocalization(com.google.api.services.youtube.model.PlaylistLocalization)

Example 2 with PlaylistLocalization

use of com.google.api.services.youtube.model.PlaylistLocalization in project api-samples by youtube.

the class PlaylistLocalizations method setPlaylistLocalization.

/**
     * Updates a playlist's default language and sets its localized metadata.
     *
     * @param playlistId The id parameter specifies the playlist ID for the resource
     * that is being updated.
     * @param defaultLanguage The language of the playlist's default metadata
     * @param language The language of the localized metadata
     * @param title The localized title to be set
     * @param description The localized description to be set
     * @throws IOException
     */
private static void setPlaylistLocalization(String playlistId, String defaultLanguage, String language, String title, String description) throws IOException {
    // Call the YouTube Data API's playlists.list method to retrieve playlists.
    PlaylistListResponse playlistListResponse = youtube.playlists().list("snippet,localizations").setId(playlistId).execute();
    // Since the API request specified a unique playlist ID, the API
    // response should return exactly one playlist. If the response does
    // not contain a playlist, then the specified playlist ID was not found.
    List<Playlist> playlistList = playlistListResponse.getItems();
    if (playlistList.isEmpty()) {
        System.out.println("Can't find a playlist with ID: " + playlistId);
        return;
    }
    Playlist playlist = playlistList.get(0);
    // Modify playlist's default language and localizations properties.
    // Ensure that a value is set for the resource's snippet.defaultLanguage property.
    playlist.getSnippet().setDefaultLanguage(defaultLanguage);
    // Preserve any localizations already associated with the playlist. If the
    // playlist does not have any localizations, create a new array. Append the
    // provided localization to the list of localizations associated with the playlist.
    Map<String, PlaylistLocalization> localizations = playlist.getLocalizations();
    if (localizations == null) {
        localizations = new ArrayMap<String, PlaylistLocalization>();
        playlist.setLocalizations(localizations);
    }
    PlaylistLocalization playlistLocalization = new PlaylistLocalization();
    playlistLocalization.setTitle(title);
    playlistLocalization.setDescription(description);
    localizations.put(language, playlistLocalization);
    // Update the playlist resource by calling the playlists.update() method.
    Playlist playlistResponse = youtube.playlists().update("snippet,localizations", playlist).execute();
    // Print information from the API response.
    System.out.println("\n================== Updated Playlist ==================\n");
    System.out.println("  - ID: " + playlistResponse.getId());
    System.out.println("  - Default Language: " + playlistResponse.getSnippet().getDefaultLanguage());
    System.out.println("  - Title(" + language + "): " + playlistResponse.getLocalizations().get(language).getTitle());
    System.out.println("  - Description(" + language + "): " + playlistResponse.getLocalizations().get(language).getDescription());
    System.out.println("\n-------------------------------------------------------------\n");
}
Also used : Playlist(com.google.api.services.youtube.model.Playlist) PlaylistListResponse(com.google.api.services.youtube.model.PlaylistListResponse) PlaylistLocalization(com.google.api.services.youtube.model.PlaylistLocalization)

Aggregations

Playlist (com.google.api.services.youtube.model.Playlist)2 PlaylistListResponse (com.google.api.services.youtube.model.PlaylistListResponse)2 PlaylistLocalization (com.google.api.services.youtube.model.PlaylistLocalization)2