Search in sources :

Example 1 with ResourceId

use of com.google.api.services.youtube.model.ResourceId in project opencast by opencast.

the class YouTubeAPIVersion3ServiceImpl method addPlaylistItem.

@Override
public PlaylistItem addPlaylistItem(final String playlistId, final String videoId) throws IOException {
    // Resource type (video,playlist,channel) needs to be set along with resource id.
    final ResourceId resourceId = new ResourceId();
    resourceId.setKind("youtube#video");
    resourceId.setVideoId(videoId);
    // Set the required snippet properties.
    final PlaylistItemSnippet playlistItemSnippet = new PlaylistItemSnippet();
    playlistItemSnippet.setTitle("First video in the test playlist");
    playlistItemSnippet.setPlaylistId(playlistId);
    playlistItemSnippet.setResourceId(resourceId);
    // Create the playlist item.
    final PlaylistItem playlistItem = new PlaylistItem();
    playlistItem.setSnippet(playlistItemSnippet);
    // The first argument tells the API what to return when a successful insert has been executed.
    final YouTube.PlaylistItems.Insert playlistItemsInsertCommand = youTube.playlistItems().insert("snippet,contentDetails", playlistItem);
    return execute(playlistItemsInsertCommand);
}
Also used : ResourceId(com.google.api.services.youtube.model.ResourceId) PlaylistItemSnippet(com.google.api.services.youtube.model.PlaylistItemSnippet) PlaylistItem(com.google.api.services.youtube.model.PlaylistItem)

Example 2 with ResourceId

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

the class Search method prettyPrint.

/*
     * Prints out all results in the Iterator. For each result, print the
     * title, video ID, and thumbnail.
     *
     * @param iteratorSearchResults Iterator of SearchResults to print
     *
     * @param query Search query (String)
     */
private static void prettyPrint(Iterator<SearchResult> iteratorSearchResults, String query) {
    System.out.println("\n=============================================================");
    System.out.println("   First " + NUMBER_OF_VIDEOS_RETURNED + " videos for search on \"" + query + "\".");
    System.out.println("=============================================================\n");
    if (!iteratorSearchResults.hasNext()) {
        System.out.println(" There aren't any results for your query.");
    }
    while (iteratorSearchResults.hasNext()) {
        SearchResult singleVideo = iteratorSearchResults.next();
        ResourceId rId = singleVideo.getId();
        // item will not contain a video ID.
        if (rId.getKind().equals("youtube#video")) {
            Thumbnail thumbnail = singleVideo.getSnippet().getThumbnails().getDefault();
            System.out.println(" Video Id" + rId.getVideoId());
            System.out.println(" Title: " + singleVideo.getSnippet().getTitle());
            System.out.println(" Thumbnail: " + thumbnail.getUrl());
            System.out.println("\n-------------------------------------------------------------\n");
        }
    }
}
Also used : ResourceId(com.google.api.services.youtube.model.ResourceId) SearchResult(com.google.api.services.youtube.model.SearchResult) Thumbnail(com.google.api.services.youtube.model.Thumbnail)

Example 3 with ResourceId

use of com.google.api.services.youtube.model.ResourceId in project SkyBot by duncte123.

the class SpotifyAudioSourceManager method doThingWithPlaylist.

private List<AudioTrack> doThingWithPlaylist(List<SearchResult> results) throws Exception {
    List<AudioTrack> playList = new ArrayList<>();
    if (results.size() > 0) {
        SearchResult video = results.get(0);
        ResourceId rId = video.getId();
        if (rId.getKind().equals("youtube#video")) {
            Video v = getVideoById(video.getId().getVideoId());
            playList.add(new SpotifyAudioTrack(new AudioTrackInfo(v.getSnippet().getTitle(), v.getSnippet().getChannelId(), toLongDuration(v.getContentDetails().getDuration()), video.getId().getVideoId(), false, "https://youtube.com/watch?v=" + video.getId().getVideoId()), youtubeAudioSourceManager));
        }
    }
    return playList;
}
Also used : ResourceId(com.google.api.services.youtube.model.ResourceId) Video(com.google.api.services.youtube.model.Video) ArrayList(java.util.ArrayList) SearchResult(com.google.api.services.youtube.model.SearchResult)

Example 4 with ResourceId

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

the class AddSubscription method main.

/**
 * Subscribe the user's YouTube account to a user-selected channel.
 *
 * @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, "addsubscription");
        // This object is used to make YouTube Data API requests.
        youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).setApplicationName("youtube-cmdline-addsubscription-sample").build();
        // We get the user selected channel to subscribe.
        // Retrieve the channel ID that the user is subscribing to.
        String channelId = getChannelId();
        System.out.println("You chose " + channelId + " to subscribe.");
        // Create a resourceId that identifies the channel ID.
        ResourceId resourceId = new ResourceId();
        resourceId.setChannelId(channelId);
        resourceId.setKind("youtube#channel");
        // Create a snippet that contains the resourceId.
        SubscriptionSnippet snippet = new SubscriptionSnippet();
        snippet.setResourceId(resourceId);
        // Create a request to add the subscription and send the request.
        // The request identifies subscription metadata to insert as well
        // as information that the API server should return in its response.
        Subscription subscription = new Subscription();
        subscription.setSnippet(snippet);
        YouTube.Subscriptions.Insert subscriptionInsert = youtube.subscriptions().insert("snippet,contentDetails", subscription);
        Subscription returnedSubscription = subscriptionInsert.execute();
        // Print information from the API response.
        System.out.println("\n================== Returned Subscription ==================\n");
        System.out.println("  - Id: " + returnedSubscription.getId());
        System.out.println("  - Title: " + returnedSubscription.getSnippet().getTitle());
    } 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 : Credential(com.google.api.client.auth.oauth2.Credential) IOException(java.io.IOException) YouTube(com.google.api.services.youtube.YouTube) SubscriptionSnippet(com.google.api.services.youtube.model.SubscriptionSnippet) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) ResourceId(com.google.api.services.youtube.model.ResourceId) Subscription(com.google.api.services.youtube.model.Subscription)

Aggregations

ResourceId (com.google.api.services.youtube.model.ResourceId)4 SearchResult (com.google.api.services.youtube.model.SearchResult)2 Credential (com.google.api.client.auth.oauth2.Credential)1 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)1 YouTube (com.google.api.services.youtube.YouTube)1 PlaylistItem (com.google.api.services.youtube.model.PlaylistItem)1 PlaylistItemSnippet (com.google.api.services.youtube.model.PlaylistItemSnippet)1 Subscription (com.google.api.services.youtube.model.Subscription)1 SubscriptionSnippet (com.google.api.services.youtube.model.SubscriptionSnippet)1 Thumbnail (com.google.api.services.youtube.model.Thumbnail)1 Video (com.google.api.services.youtube.model.Video)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1