Search in sources :

Example 1 with VideoListResponse

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

the class UpdateVideo method main.

/**
 * Add a keyword tag to a video that the user specifies. Use OAuth 2.0 to
 * authorize the API request.
 *
 * @param args command line args (not used).
 */
public static void main(String[] args) {
    // This OAuth 2.0 access scope allows for full read/write access to the
    // authenticated user's account.
    List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube");
    try {
        // Authorize the request.
        Credential credential = Auth.authorize(scopes, "updatevideo");
        // This object is used to make YouTube Data API requests.
        youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).setApplicationName("youtube-cmdline-updatevideo-sample").build();
        // Prompt the user to enter the video ID of the video being updated.
        String videoId = getVideoIdFromUser();
        System.out.println("You chose " + videoId + " to update.");
        // Prompt the user to enter a keyword tag to add to the video.
        String tag = getTagFromUser();
        System.out.println("You chose " + tag + " as a tag.");
        // Call the YouTube Data API's youtube.videos.list method to
        // retrieve the resource that represents the specified video.
        YouTube.Videos.List listVideosRequest = youtube.videos().list("snippet").setId(videoId);
        VideoListResponse listResponse = listVideosRequest.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 = listResponse.getItems();
        if (videoList.isEmpty()) {
            System.out.println("Can't find a video with ID: " + videoId);
            return;
        }
        // Extract the snippet from the video resource.
        Video video = videoList.get(0);
        VideoSnippet snippet = video.getSnippet();
        // Preserve any tags already associated with the video. If the
        // video does not have any tags, create a new array. Append the
        // provided tag to the list of tags associated with the video.
        List<String> tags = snippet.getTags();
        if (tags == null) {
            tags = new ArrayList<String>(1);
            snippet.setTags(tags);
        }
        tags.add(tag);
        // Update the video resource by calling the videos.update() method.
        YouTube.Videos.Update updateVideosRequest = youtube.videos().update("snippet", video);
        Video videoResponse = updateVideosRequest.execute();
        // Print information from the updated resource.
        System.out.println("\n================== Returned Video ==================\n");
        System.out.println("  - Title: " + videoResponse.getSnippet().getTitle());
        System.out.println("  - Tags: " + videoResponse.getSnippet().getTags());
    } catch (GoogleJsonResponseException e) {
        System.err.println("GoogleJsonResponseException code: " + e.getDetails().getCode() + " : " + e.getDetails().getMessage());
        e.printStackTrace();
    } catch (IOException e) {
        System.err.println("IOException: " + e.getMessage());
        e.printStackTrace();
    } catch (Throwable t) {
        System.err.println("Throwable: " + t.getMessage());
        t.printStackTrace();
    }
}
Also used : VideoSnippet(com.google.api.services.youtube.model.VideoSnippet) Credential(com.google.api.client.auth.oauth2.Credential) IOException(java.io.IOException) YouTube(com.google.api.services.youtube.YouTube) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) Video(com.google.api.services.youtube.model.Video) VideoListResponse(com.google.api.services.youtube.model.VideoListResponse)

Example 2 with VideoListResponse

use of com.google.api.services.youtube.model.VideoListResponse 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 3 with VideoListResponse

use of com.google.api.services.youtube.model.VideoListResponse 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)

Example 4 with VideoListResponse

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

the class GetLiveChatId method getLiveChatId.

/**
 * Retrieves the liveChatId from the broadcast associated with a videoId.
 *
 * @param youtube The object is used to make YouTube Data API requests.
 * @param videoId The videoId associated with the live broadcast.
 * @return A liveChatId, or null if not found.
 */
static String getLiveChatId(YouTube youtube, String videoId) throws IOException {
    // Get liveChatId from the video
    YouTube.Videos.List videoList = youtube.videos().list("liveStreamingDetails").setFields("items/liveStreamingDetails/activeLiveChatId").setId(videoId);
    VideoListResponse response = videoList.execute();
    for (Video v : response.getItems()) {
        String liveChatId = v.getLiveStreamingDetails().getActiveLiveChatId();
        if (liveChatId != null && !liveChatId.isEmpty()) {
            return liveChatId;
        }
    }
    return null;
}
Also used : Video(com.google.api.services.youtube.model.Video) VideoListResponse(com.google.api.services.youtube.model.VideoListResponse)

Example 5 with VideoListResponse

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

the class GeolocationSearch method main.

/**
 * Initialize a YouTube object to search for videos on YouTube. Then
 * display the name and thumbnail image of each video in the result set.
 *
 * @param args command line args.
 */
public static void main(String[] args) {
    // Read the developer key from the properties file.
    Properties properties = new Properties();
    try {
        InputStream in = GeolocationSearch.class.getResourceAsStream("/" + PROPERTIES_FILENAME);
        properties.load(in);
    } catch (IOException e) {
        System.err.println("There was an error reading " + PROPERTIES_FILENAME + ": " + e.getCause() + " : " + e.getMessage());
        System.exit(1);
    }
    try {
        // This object is used to make YouTube Data API requests. The last
        // argument is required, but since we don't need anything
        // initialized when the HttpRequest is initialized, we override
        // the interface and provide a no-op function.
        youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, new HttpRequestInitializer() {

            @Override
            public void initialize(HttpRequest request) throws IOException {
            }
        }).setApplicationName("youtube-cmdline-geolocationsearch-sample").build();
        // Prompt the user to enter a query term.
        String queryTerm = getInputQuery();
        // Prompt the user to enter location coordinates.
        String location = getInputLocation();
        // Prompt the user to enter a location radius.
        String locationRadius = getInputLocationRadius();
        // Define the API request for retrieving search results.
        YouTube.Search.List search = youtube.search().list("id,snippet");
        // Set your developer key from the {{ Google Cloud Console }} for
        // non-authenticated requests. See:
        // {{ https://cloud.google.com/console }}
        String apiKey = properties.getProperty("youtube.apikey");
        search.setKey(apiKey);
        search.setQ(queryTerm);
        search.setLocation(location);
        search.setLocationRadius(locationRadius);
        // Restrict the search results to only include videos. See:
        // https://developers.google.com/youtube/v3/docs/search/list#type
        search.setType("video");
        // As a best practice, only retrieve the fields that the
        // application uses.
        search.setFields("items(id/videoId)");
        search.setMaxResults(NUMBER_OF_VIDEOS_RETURNED);
        // Call the API and print results.
        SearchListResponse searchResponse = search.execute();
        List<SearchResult> searchResultList = searchResponse.getItems();
        List<String> videoIds = new ArrayList<String>();
        if (searchResultList != null) {
            // Merge video IDs
            for (SearchResult searchResult : searchResultList) {
                videoIds.add(searchResult.getId().getVideoId());
            }
            Joiner stringJoiner = Joiner.on(',');
            String videoId = stringJoiner.join(videoIds);
            // Call the YouTube Data API's youtube.videos.list method to
            // retrieve the resources that represent the specified videos.
            YouTube.Videos.List listVideosRequest = youtube.videos().list("snippet, recordingDetails").setId(videoId);
            VideoListResponse listResponse = listVideosRequest.execute();
            List<Video> videoList = listResponse.getItems();
            if (videoList != null) {
                prettyPrint(videoList.iterator(), queryTerm);
            }
        }
    } catch (GoogleJsonResponseException e) {
        System.err.println("There was a service error: " + e.getDetails().getCode() + " : " + e.getDetails().getMessage());
    } catch (IOException e) {
        System.err.println("There was an IO error: " + e.getCause() + " : " + e.getMessage());
    } catch (Throwable t) {
        t.printStackTrace();
    }
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) SearchListResponse(com.google.api.services.youtube.model.SearchListResponse) Joiner(com.google.api.client.util.Joiner) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) SearchResult(com.google.api.services.youtube.model.SearchResult) IOException(java.io.IOException) Properties(java.util.Properties) YouTube(com.google.api.services.youtube.YouTube) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) Video(com.google.api.services.youtube.model.Video) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer) VideoListResponse(com.google.api.services.youtube.model.VideoListResponse)

Aggregations

VideoListResponse (com.google.api.services.youtube.model.VideoListResponse)7 Video (com.google.api.services.youtube.model.Video)6 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)2 YouTube (com.google.api.services.youtube.YouTube)2 VideoLocalization (com.google.api.services.youtube.model.VideoLocalization)2 IOException (java.io.IOException)2 Credential (com.google.api.client.auth.oauth2.Credential)1 HttpRequest (com.google.api.client.http.HttpRequest)1 HttpRequestInitializer (com.google.api.client.http.HttpRequestInitializer)1 Joiner (com.google.api.client.util.Joiner)1 SearchListResponse (com.google.api.services.youtube.model.SearchListResponse)1 SearchResult (com.google.api.services.youtube.model.SearchResult)1 VideoSnippet (com.google.api.services.youtube.model.VideoSnippet)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1