gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:19:50 26 4
gpt4 key购买 nike

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

最佳答案

我能够使用 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();
}
}
}

关于youtube-api - 是否可以获取在特定日期之间发布的特定标签上的视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8847368/

26 4 0