Search in sources :

Example 1 with VideoLocalization

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

the class VideoLocalizations method listVideoLocalizations.

/**
 * Returns a list of localized metadata for a video.
 *
 * @param videoId The id parameter specifies the video ID for the resource
 * that is being updated.
 * @throws IOException
 */
private static void listVideoLocalizations(String videoId) throws IOException {
    // Call the YouTube Data API's videos.list method to retrieve videos.
    VideoListResponse videoListResponse = youtube.videos().list("snippet,localizations").setId(videoId).execute();
    // Since the API request specified a unique video ID, the API
    // response should return exactly one video. If the response does
    // not contain a video, then the specified video ID was not found.
    List<Video> videoList = videoListResponse.getItems();
    if (videoList.isEmpty()) {
        System.out.println("Can't find a video with ID: " + videoId);
        return;
    }
    Video video = videoList.get(0);
    Map<String, VideoLocalization> localizations = video.getLocalizations();
    // Print information from the API response.
    System.out.println("\n================== Video ==================\n");
    System.out.println("  - ID: " + video.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 : Video(com.google.api.services.youtube.model.Video) VideoLocalization(com.google.api.services.youtube.model.VideoLocalization) VideoListResponse(com.google.api.services.youtube.model.VideoListResponse)

Example 2 with VideoLocalization

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

the class VideoLocalizations method setVideoLocalization.

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

Aggregations

Video (com.google.api.services.youtube.model.Video)2 VideoListResponse (com.google.api.services.youtube.model.VideoListResponse)2 VideoLocalization (com.google.api.services.youtube.model.VideoLocalization)2