Archivo por meses: septiembre 2014

Youtube API – How to copy the WatchLater List

Disclaimer

All the code here has been tested for 5 or 10 minutes… Use at your own risk. This is intended to be only a draft, a demo of the Youtube API possibilities and it is offered without any warranty.

 

OMG, It is written in English…

This post is going to be written in English. I must practice my English and write this will be a good way. So let it go…

 

Why do you want to copy the Watch Later List?

(don’t read this, there is no code here, continue below)  

When I code for pleasure, out of the office, at home or sometimes, on a hotel room, I have a particular way to do it. Some people likes to hear classical music, some prefer video games BSOs, some others totally silence… I like to see youtube music videos. Actually, most of the time, I don’t look at them, I only hear them but I like the way I can made a music selection on youtube. Let’s me explain it…

Before I begin to code, I do a selection of videos. The easy way of doing a selection is to add then to the “watch later” playlist. The selection last for a few days and I made a new one, normally because I want to hear a different kind of music (You know, sometime you prefer POP, sometimes Rock, sometimes games BSO…). But I don’t want to lose the old selection so I need to save it.

In the past you can do it on YouTube, you can re-organize your watch later list and copy selected videos to other list. But this is not more possible.
Last saturday evening I was very upset for this little problem so I google a little until find the youtube info API. And this article is the result. Now I can do that and a few things more…

 

How to do it

(if you don’t want to read more, you would prefer go to the github repository directly: https://github.com/sbgermanm/youtubeV2)

1. We need a google api client ID

1.1 Go to google api console: https://console.developers.google.com/

1.2 Create a new project

Crear proyecto

1.3 Activate youtube data api

enable api2

1.4 Create new client ID. Here you can take the data for the file client_secrets.json

create client id

client id created
1.5 Fill in the consent screen

consent screen
1.6 You are done

 

Let’s do it

Now that you can use the youtube api, let’s go make an application:

2. Resources

To understand how the API works, there are three importants helps from google:
2.1 The introduction to how the API works: https://developers.google.com/youtube/v3/getting-started
2.2 The API reference: https://developers.google.com/youtube/v3/docs/, where you can see the methods, the resource json structure (to figure out where you can get the data), the diferents resouce propoerties and some samples.
2.3 The API explorer: https://developers.google.com/apis-explorer/#p/youtube/v3/ a very useful resource to try the API. Here you can use all the api, try with different parameters and see the json request and response. It’s very important to know that you can limit the response of the api on several ways. You can read it in the first resource and you can interactively try it in this resource to make sure that you get what you want and only that. For example, you can easily compose the “field” parameter of the API.

3. The Code

As a line of code is better than 1 hundred words of comments, let’s code:

3.1 The pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>es.sebas</groupId>
    <artifactId>youtube2</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>jar</packaging>

    <repositories>
        <repository>
            <id>google-api-services</id>
            <url>http://google-api-client-libraries.appspot.com/mavenrepo</url>
        </repository>
    </repositories>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.youtube.version>v3-rev91-1.17.0-rc</project.youtube.version>
        <project.http.version>1.19.0</project.http.version>
        <project.oauth.version>1.19.0</project.oauth.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    </properties>
    <dependencies>

        <!-- YouTube Data V3 support -->
        <dependency>
            <groupId>com.google.apis</groupId>
            <artifactId>google-api-services-youtube</artifactId>
            <version>${project.youtube.version}</version>
        </dependency>

        <dependency>
            <groupId>com.google.http-client</groupId>
            <artifactId>google-http-client-jackson2</artifactId>
            <version>${project.http.version}</version>
        </dependency>

        <dependency>
            <groupId>com.google.oauth-client</groupId>
            <artifactId>google-oauth-client-jetty</artifactId>
            <version>${project.oauth.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- Forces Maven to use Java 1.6 -->
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <compilerArgument></compilerArgument>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <name>youtube2</name>
</project>

 

3.2 The methods used in this function
3.2.1 Authorize

public static Credential authorize() throws IOException {

        List<String> scopes = Arrays.asList("https://www.googleapis.com/auth/youtube");

        Reader reader = new InputStreamReader(PlayListTools.class.getResourceAsStream("/client_secrets.json"));
        // Load client secrets.
        GoogleClientSecrets clientSecrets
                = GoogleClientSecrets.load(
                        JSON_FACTORY,
                        reader);

        FileDataStoreFactory fileDataStoreFactory = new FileDataStoreFactory(new File(System.getProperty("user.home") + "/" + CREDENTIALS_DIRECTORY));
        DataStore<StoredCredential> datastore = fileDataStoreFactory.getDataStore("youtubesebas");

        // Set up authorization code flow.
        GoogleAuthorizationCodeFlow flow
                = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT,
                        JSON_FACTORY,
                        clientSecrets,
                        scopes)
                .setCredentialDataStore(datastore)
                .build();

        // Build the local server and bind it to port 9000
        LocalServerReceiver localReceiver = new LocalServerReceiver.Builder().setPort(8080).build();

        // Authorize.
        return new AuthorizationCodeInstalledApp(flow, localReceiver).authorize("user");
}

and the credentials for the authorization client_secrets.json (put here the data you get on section 1.4)

{
 "installed": {
 "client_id": "Enter your client id here",
 "client_secret": "Enter your client secret here"
 }
}

 

3.2.2 Recover all watch later items

public static List<PlaylistItem> getAllWatchLaterItems(Credential credential) {

        String wlPlaylistId = getWlID(credential);
        System.out.println("WatchLaterId : " + wlPlaylistId);
        return getAllPlayListItems(credential, wlPlaylistId);
}

public static List<PlaylistItem> getAllPlayListItems(Credential credential, String playListID) {
        // List to store all PlaylistItem items associated with the uploadPlaylistId.
        ArrayList<PlaylistItem> playlistItemList = new ArrayList<PlaylistItem>();

        /*
         * Now that the user is authenticated, the app makes a channel list request to get the
         * authenticated user's channel. Returned with that data is the playlist id for the uploaded
         * videos. https://developers.google.com/youtube/v3/docs/channels/list
         */
        YouTube youtube = new YouTube.Builder(new NetHttpTransport(), new JacksonFactory(), credential)
                .setApplicationName("youtube-sebas")
                .build();

        /*
         * Now that we have the playlist id for your uploads, we will request the playlistItems
         * associated with that playlist id, so we can get information on each video uploaded. This
         * is the template for the list call. We call it multiple times in the do while loop below
         * (only changing the nextToken to get all the videos).
         * https://developers.google.com/youtube/v3/docs/playlistitems/list
         */
        YouTube.PlaylistItems.List playlistItemRequest;
        try {
            playlistItemRequest = youtube.playlistItems().list("id,contentDetails,snippet");
            playlistItemRequest.setPlaylistId(playListID);

            // This limits the results to only the data we need and makes things more efficient.
            playlistItemRequest.setFields(
                    "items(id,contentDetails/videoId,snippet/title),nextPageToken,pageInfo");

            String nextToken = "";

            // Loops over all search page results returned for the uploadPlaylistId.
            do {
                playlistItemRequest.setPageToken(nextToken);
                PlaylistItemListResponse playlistItemResult = playlistItemRequest.execute();

                playlistItemList.addAll(playlistItemResult.getItems());

                nextToken = playlistItemResult.getNextPageToken();
            } while (nextToken != null);

        } catch (IOException ex) {
            Logger.getLogger(PlayListTools.class.getName()).log(Level.SEVERE, null, ex);
        }

        return playlistItemList;
}

 

3.2.3 Insert a new playList

public static String insertPlaylist(String playlistTitle, String playlistDesc, Credential credential) throws IOException {

        /*
         * We need to first create the parts of the Playlist before the playlist itself.  Here we are
         * creating the PlaylistSnippet and adding the required data.
         */
        PlaylistSnippet playlistSnippet = new PlaylistSnippet();
        playlistSnippet.setTitle(playlistTitle);
        playlistSnippet.setDescription(playlistDesc);

        // Here we set the privacy status (required).
        PlaylistStatus playlistStatus = new PlaylistStatus();
        playlistStatus.setPrivacyStatus("private");

        /*
         * Now that we have all the required objects, we can create the Playlist itself and assign the
         * snippet and status objects from above.
         */
        Playlist youTubePlaylist = new Playlist();
        youTubePlaylist.setSnippet(playlistSnippet);
        youTubePlaylist.setStatus(playlistStatus);

        /*
         * This is the object that will actually do the insert request and return the result.  The
         * first argument tells the API what to return when a successful insert has been executed.  In
         * this case, we want the snippet and contentDetails info.  The second argument is the playlist
         * we wish to insert.
         */
        // YouTube object used to make all API requests.
        YouTube youtube = new YouTube.Builder(new NetHttpTransport(), new JacksonFactory(), credential)
                .setApplicationName("youtube-sebas")
                .build();

        YouTube.Playlists.Insert playlistInsertCommand
                = youtube.playlists().insert("snippet,status", youTubePlaylist);
        Playlist playlistInserted = playlistInsertCommand.execute();

        // Pretty print results.
        System.out.println("New Playlist name: " + playlistInserted.getSnippet().getTitle());
        System.out.println(" - Privacy: " + playlistInserted.getStatus().getPrivacyStatus());
        System.out.println(" - Description: " + playlistInserted.getSnippet().getDescription());
        System.out.println(" - Channel: " + playlistInserted.getSnippet().getChannelId() + "n");
        return playlistInserted.getId();

}

 

3.2.4 Insert the previously recovered items (the watch later items)

public static void insertPlaylistItems(Credential credential, String playlistId, List<PlaylistItem> playListItems) {

        for (PlaylistItem playlistItem : playListItems) {
            try {
                System.out.println("Inserting Video, ID=" + playlistItem.getContentDetails().getVideoId() + " with title: " + playlistItem.getSnippet().getTitle());
                insertPlaylistItem(credential, playlistId, playlistItem.getContentDetails().getVideoId(), playlistItem.getSnippet().getTitle());
            } catch (IOException ex) {
                Logger.getLogger(PlayListTools.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

}

private static String insertPlaylistItem(Credential credential, String playlistId, String videoId, String title) throws IOException {

        /*
         * The Resource type (video,playlist,channel) needs to be set along with the resource id. In
         * this case, we are setting the resource to a video id, since that makes sense for this
         * playlist.
         */
        ResourceId resourceId = new ResourceId();
        resourceId.setKind(RESOURCE_KIND_VIDEO);
        resourceId.setVideoId(videoId);

        /*
         * Here we set all the information required for the snippet section.  We also assign the
         * resource id from above to the snippet object.
         */
        PlaylistItemSnippet playlistItemSnippet = new PlaylistItemSnippet();
        playlistItemSnippet.setTitle(title);
        playlistItemSnippet.setPlaylistId(playlistId);
        playlistItemSnippet.setResourceId(resourceId);

        /*
         * Now that we have all the required objects, we can create the PlaylistItem itself and assign
         * the snippet object from above.
         */
        PlaylistItem playlistItem = new PlaylistItem();
        playlistItem.setSnippet(playlistItemSnippet);

        /*
         * This is the object that will actually do the insert request and return the result.  The
         * first argument tells the API what to return when a successful insert has been executed.  In
         * this case, we want the snippet and contentDetails info.  The second argument is the
         * playlistitem we wish to insert.
         */
        YouTube youtube = new YouTube.Builder(new NetHttpTransport(), new JacksonFactory(), credential)
                .setApplicationName("youtube-sebas")
                .build();
        YouTube.PlaylistItems.Insert playlistItemsInsertCommand
                = youtube.playlistItems().insert("snippet,contentDetails", playlistItem);
        PlaylistItem returnedPlaylistItem = playlistItemsInsertCommand.execute();

        printPlayListItem(returnedPlaylistItem);

        return returnedPlaylistItem.getId();

}

 

3.2.5 Some globals…

 /**
 * Global instance of the HTTP transport.
 */
 private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();

 /**
 * Global instance of the JSON factory.
 */
 private static final JsonFactory JSON_FACTORY = new JacksonFactory();

/**
* Some constants...
*/
 private static final String RESOURCE_KIND_VIDEO = "youtube#video";
 private static final String CREDENTIALS_DIRECTORY = ".oauth-credentials";

 

3.2.6 The method that do the copy

public static void main(String[] args) {

        // Authorization.
        Credential credential = null;
        try {
            credential = PlayListTools.authorize();
        } catch (IOException ex) {
            Logger.getLogger(WacthLaterCopy.class.getName()).log(Level.SEVERE, null, ex);
        }

        List<PlaylistItem> wlPlayListItems = PlayListTools.getAllWatchLaterItems(credential);

        String playlistTitle = "Temporal Playlist " + Calendar.getInstance().getTime();
        String playlistDesc = "A private temporal playlist created with the YouTube API v3";
        String playlistId;
        try {
            playlistId = PlayListTools.insertPlaylist(playlistTitle, playlistDesc, credential);

            // If a valid playlist was created, adds a new playlistitem with a video to that playlist.
            PlayListTools.insertPlaylistItems(credential, playlistId, wlPlayListItems);
        } catch (IOException ex) {
            Logger.getLogger(WacthLaterCopy.class.getName()).log(Level.SEVERE, null, ex);
        }
}

 

3.2.7 The maven comand line to automate this

mvn exec:java -Dexec.mainClass=”es.sebas.youtube.WatChLaterCopy”

 

To be continued

Now we have the copy of the Watch Later List. In next post I will show :

  • How to Purge the Watch Later List
  • How to create a VideoLan List of the videos in our playLists

Write you later…