Question

We use google-api-java-client to lookup videos and we would like to know if it would be possible to fetch videos on a certain tag (say sports) published between certain dates (starting from yesterday till now). How do I do this?

Was it helpful?

Solution

I was able to do this using the google-api-client 1.6.0-beta (downloaded via Maven). I modified the example code a little. The API had changed slightly since the example code was written. I added query parameters from the YouTube API Reference Guide and extended the Video class to include a couple more fields. If you look at the raw JSON returned from the query you will see you could add several others fields including thumbnails, duration, aspect ratio, comment count etc. I hope this helps.

import com.google.api.client.googleapis.GoogleHeaders;
import com.google.api.client.googleapis.json.JsonCParser;
import com.google.api.client.http.*;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.client.util.Key;
import java.io.IOException;
import java.util.List;

public class YouTubeSample {

    public static class VideoFeed {
        @Key
        List<Video> items;
    }

    public static class Video {
        @Key
        String title;
        @Key
        String description;
        @Key
        Player player;
        @Key
        String uploaded;
        @Key
        String category;
        @Key
        String[] tags;
    }

    public static class Player {
        @Key("default")
        String defaultUrl;
    }

    public static class YouTubeUrl extends GenericUrl {
        @Key
        final String alt = "jsonc";
        @Key
        String author;
        @Key("max-results")
        Integer maxResults;
        @Key
        String category;        
        @Key
        String time;        

        YouTubeUrl(String url) {
            super(url);
        }
    }

    public static void main(String[] args) throws IOException {
        // set up the HTTP request factory
        HttpTransport transport = new NetHttpTransport();
        final JsonFactory jsonFactory = new JacksonFactory();
        HttpRequestFactory factory = transport.createRequestFactory(new HttpRequestInitializer() {

            @Override
            public void initialize(HttpRequest request) {
                // set the parser
                JsonCParser parser = new JsonCParser(jsonFactory);
                request.addParser(parser);
                // set up the Google headers
                GoogleHeaders headers = new GoogleHeaders();
                headers.setApplicationName("Google-YouTubeSample/1.0");
                headers.gdataVersion = "2";
                request.setHeaders(headers);
            }
        });
        // build the YouTube URL
        YouTubeUrl url = new YouTubeUrl("https://gdata.youtube.com/feeds/api/videos");
        url.maxResults = 10;
        url.category = "sports";        
        // Time options: today, this_week, this_month, all_time        
        url.time = "today";


        // build the HTTP GET request
        HttpRequest request = factory.buildGetRequest(url);
        // execute the request and the parse video feed
        VideoFeed feed = request.execute().parseAs(VideoFeed.class);

        // Useful for viewing raw JSON results
        //System.out.println(request.execute().parseAsString());

        for (Video video : feed.items) {
            System.out.println();
            System.out.println("Video title: " + video.title);
            System.out.println("Description: " + video.description);
            System.out.println("Play URL: " + video.player.defaultUrl);
            System.out.println("Uploaded: " + video.uploaded);
            System.out.println("Category: " + video.category);
            System.out.print("Tags: ");
            for(String tag: video.tags){
                System.out.print(tag + " ");
            }
            System.out.println();
        }                    
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top