Query YouTube videos using YouTube Java API
In this example we are going to demonstrate how to query YouTube videos using YouTube Java API. This is particularly useful when you want to embed YouTube support in your application.
In order to query YouTube videos using YouTube Java API you should follows these steps:
First we create two model classes which will be used to hold information about the feeds and the videos. The first one is called YouTubeMedia and contains the media content URL and the media content type. The second one is named YouTubeVideo and contains all the information regarding a specific video (URL, embedded player URL, thumbnails and instances of YoutTubeMedia).
The source code for these is the following:
package com.javacodegeeks.youtube.model; public class YouTubeMedia { private String location; private String type; public YouTubeMedia(String location, String type) { super(); this.location = location; this.type = type; } public String getLocation() { return location; } public void setLocation(String location) { this.location = location; } public String getType() { return type; } public void setType(String type) { this.type = type; } }
package com.javacodegeeks.youtube.model; import java.util.List; public class YouTubeVideo { private List<String> thumbnails; private List<YouTubeMedia> medias; private String webPlayerUrl; private String embeddedWebPlayerUrl; public List<String> getThumbnails() { return thumbnails; } public void setThumbnails(List<String> thumbnails) { this.thumbnails = thumbnails; } public List<YouTubeMedia> getMedias() { return medias; } public void setMedias(List<YouTubeMedia> medias) { this.medias = medias; } public String getWebPlayerUrl() { return webPlayerUrl; } public void setWebPlayerUrl(String webPlayerUrl) { this.webPlayerUrl = webPlayerUrl; } public String getEmbeddedWebPlayerUrl() { return embeddedWebPlayerUrl; } public void setEmbeddedWebPlayerUrl(String embeddedWebPlayerUrl) { this.embeddedWebPlayerUrl = embeddedWebPlayerUrl; } public String retrieveHttpLocation() { if (medias==null || medias.isEmpty()) { return null; } for (YouTubeMedia media : medias) { String location = media.getLocation(); if (location.startsWith("http")) { return location; } } return null; } }
Finally, the YouTubeManager class is presented. It can be used to perform search queries and returns instances of the YouTubeVideo model class with all the relevant information. It also creates the appropriate embedded web player URL. Here is the code for that class:
package com.javacodegeeks.youtube; import java.net.URL; import java.util.LinkedList; import java.util.List; import com.google.gdata.client.youtube.YouTubeQuery; import com.google.gdata.client.youtube.YouTubeService; import com.google.gdata.data.media.mediarss.MediaThumbnail; import com.google.gdata.data.youtube.VideoEntry; import com.google.gdata.data.youtube.VideoFeed; import com.google.gdata.data.youtube.YouTubeMediaContent; import com.google.gdata.data.youtube.YouTubeMediaGroup; import com.javacodegeeks.youtube.model.YouTubeMedia; import com.javacodegeeks.youtube.model.YouTubeVideo; public class YouTubeManager { private static final String YOUTUBE_URL = "http://gdata.youtube.com/feeds/api/videos"; private static final String YOUTUBE_EMBEDDED_URL = "http://www.youtube.com/v/"; private String clientID; public YouTubeManager(String clientID) { this.clientID = clientID; } public List<YouTubeVideo> retrieveVideos(String textQuery, int maxResults, boolean filter, int timeout) throws Exception { YouTubeService service = new YouTubeService(clientID); service.setConnectTimeout(timeout); // millis YouTubeQuery query = new YouTubeQuery(new URL(YOUTUBE_URL)); query.setOrderBy(YouTubeQuery.OrderBy.VIEW_COUNT); query.setFullTextQuery(textQuery); query.setSafeSearch(YouTubeQuery.SafeSearch.NONE); query.setMaxResults(maxResults); VideoFeed videoFeed = service.query(query, VideoFeed.class); List<VideoEntry> videos = videoFeed.getEntries(); return convertVideos(videos); } private List<YouTubeVideo> convertVideos(List<VideoEntry> videos) { List<YouTubeVideo> youtubeVideosList = new LinkedList<YouTubeVideo>(); for (VideoEntry videoEntry : videos) { YouTubeVideo ytv = new YouTubeVideo(); YouTubeMediaGroup mediaGroup = videoEntry.getMediaGroup(); String webPlayerUrl = mediaGroup.getPlayer().getUrl(); ytv.setWebPlayerUrl(webPlayerUrl); String query = "?v="; int index = webPlayerUrl.indexOf(query); String embeddedWebPlayerUrl = webPlayerUrl.substring(index+query.length()); embeddedWebPlayerUrl = YOUTUBE_EMBEDDED_URL + embeddedWebPlayerUrl; ytv.setEmbeddedWebPlayerUrl(embeddedWebPlayerUrl); List<String> thumbnails = new LinkedList<String>(); for (MediaThumbnail mediaThumbnail : mediaGroup.getThumbnails()) { thumbnails.add(mediaThumbnail.getUrl()); } ytv.setThumbnails(thumbnails); List<YouTubeMedia> medias = new LinkedList<YouTubeMedia>(); for (YouTubeMediaContent mediaContent : mediaGroup.getYouTubeContents()) { medias.add(new YouTubeMedia(mediaContent.getUrl(), mediaContent.getType())); } ytv.setMedias(medias); youtubeVideosList.add(ytv); } return youtubeVideosList; } }
In order to test our class, as well provide a show case example, we create the following simple test class:
package com.javacodegeeks.youtube.test; import java.util.List; import com.javacodegeeks.youtube.YouTubeManager; import com.javacodegeeks.youtube.model.YouTubeVideo; public class YouTubeTester { public static void main(String[] args) throws Exception { String clientID = "JavaCodeGeeks"; String textQuery = "java code"; int maxResults = 10; boolean filter = true; int timeout = 2000; YouTubeManager ym = new YouTubeManager(clientID); List<YouTubeVideo> videos = ym.retrieveVideos(textQuery, maxResults, filter, timeout); for (YouTubeVideo youtubeVideo : videos) { System.out.println(youtubeVideo.getWebPlayerUrl()); System.out.println("Thumbnails"); for (String thumbnail : youtubeVideo.getThumbnails()) { System.out.println("t" + thumbnail); } System.out.println(youtubeVideo.getEmbeddedWebPlayerUrl()); System.out.println("************************************"); } } }
This was an example on how to query YouTube videos using YouTube Java API.
Related Article: