是否可以在特定日期之间发布的某个标签上获取视频

时间:2012-01-13 07:43:19

标签: youtube-api google-api-java-client

我们将google-api-java-client用于lookup个视频,我们想知道是否可以在特定日期(从昨天开始)发布的特定标签(例如体育)上获取视频现在)。我该怎么做?

1 个答案:

答案 0 :(得分:2)

我可以使用google-api-client 1.6.0-beta(通过Maven下载)来完成此操作。我稍微修改了示例代码。自编写示例代码以来,API略有改变。我从YouTube API Reference Guide添加了查询参数,并扩展了Video类以包含更多字段。如果你查看从查询返回的原始JSON,你会看到你可以添加其他几个字段,包括缩略图,持续时间,宽高比,评论数等。我希望这会有所帮助。

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();
        }                    
    }
}