Search in sources :

Example 1 with ChannelLocalization

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

the class ChannelLocalizations method listChannelLocalizations.

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

Example 2 with ChannelLocalization

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

the class ChannelLocalizations method setChannelLocalization.

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

Aggregations

Channel (com.google.api.services.youtube.model.Channel)2 ChannelListResponse (com.google.api.services.youtube.model.ChannelListResponse)2 ChannelLocalization (com.google.api.services.youtube.model.ChannelLocalization)2