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



我们使用google api java客户端来查找视频,我们想知道是否可以在特定日期(从昨天到现在)之间获取发布的特定标签(比如体育)上的视频。我该怎么做?

我能够使用googleapi客户端1.6.0-beta(通过Maven下载)来完成这项工作。我对示例代码做了一些修改。自从编写示例代码以来,API略有变化。我添加了YouTube API参考指南中的查询参数,并扩展了视频类以包括更多的字段。如果你查看查询返回的原始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();
        }                    
    }
}

相关内容

  • 没有找到相关文章

最新更新