Можно ли принести видео на определенную тег, опубликованную между определенными датами
-
27-10-2019 - |
Вопрос
Мы используем Google-Api-Java-Client искать Видео и мы хотели бы знать, можно ли принести видео на определенную тег (скажем, спорт), опубликованную между определенными датами (начиная со вчерашнего дня до сих пор). Как мне это сделать?
Решение
Я смог сделать это с помощью Google-API-Client 1.6.0-бета (загруженная через Мавен) Я немного изменил пример кода. API немного изменился с момента написания примера кода. Я добавил параметры запроса из Справочное руководство по API YouTube и расширил класс видео, чтобы включить еще пару полей. Если вы посмотрите на Raw 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();
}
}
}