📄 youtubeclient.java
字号:
/* Copyright (c) 2006 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package sample.youtube;import com.google.gdata.client.Query;import com.google.gdata.client.Service;import com.google.gdata.client.youtube.YouTubeQuery;import com.google.gdata.client.youtube.YouTubeService;import com.google.gdata.data.Category;import com.google.gdata.data.Entry;import com.google.gdata.data.Feed;import com.google.gdata.data.TextContent;import com.google.gdata.data.extensions.Comments;import com.google.gdata.data.extensions.FeedLink;import com.google.gdata.data.media.mediarss.MediaKeywords;import com.google.gdata.data.media.mediarss.MediaPlayer;import com.google.gdata.data.media.mediarss.MediaThumbnail;import com.google.gdata.data.youtube.FeedLinkEntry;import com.google.gdata.data.youtube.PlaylistEntry;import com.google.gdata.data.youtube.PlaylistFeed;import com.google.gdata.data.youtube.PlaylistLinkEntry;import com.google.gdata.data.youtube.PlaylistLinkFeed;import com.google.gdata.data.youtube.SubscriptionEntry;import com.google.gdata.data.youtube.SubscriptionFeed;import com.google.gdata.data.youtube.UserProfileEntry;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.google.gdata.data.youtube.YouTubeNamespace;import com.google.gdata.util.ServiceException;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.URL;import java.util.List;/** * Demonstrates basic Youtube Data API operations using the Java client library: * <ol> * <li> Retrieving standard Youtube feeds * <li> Searching a feed * <li> Searching a feed using categories and keywords * <li> Retrieving a user's uploaded videos * <li> Retrieve a user's favorite videos * <li> Retrieve responses for a video * <li> Retrieve comments for a video * <li> Retrieve a list of a user's playlists * <li> Retrieve a playlist * <li> Retrieve a list of a user's subscriptions * <li> Retrieve a user's profile * </ol> */public class YouTubeClient { /** * The name of the server hosting the YouTube GDATA feeds */ public static final String YOUTUBE_GDATA_SERVER = "http://gdata.youtube.com"; /** * The prefix common to all standard feeds */ public static final String STANDARD_FEED_PREFIX = YOUTUBE_GDATA_SERVER + "/feeds/standardfeeds/"; /** * The URL of the "Most Recent" feed */ public static final String MOST_RECENT_FEED = STANDARD_FEED_PREFIX + "most_recent"; /** * The URL of the "Top Rated" feed */ public static final String TOP_RATED_FEED = STANDARD_FEED_PREFIX + "top_rated"; /** * The URL of the "Most Viewed" feed */ public static final String MOST_VIEWED_FEED = STANDARD_FEED_PREFIX + "most_viewed"; /** * The URL of the "Recently Featured" feed */ public static final String RECENTLY_FEATURED_FEED = STANDARD_FEED_PREFIX + "recently_featured"; /** * The URL of the "Watch On Mobile" feed */ public static final String WATCH_ON_MOBILE_FEED = STANDARD_FEED_PREFIX + "watch_on_mobile"; /** * The URL of the "Videos" feed */ public static final String VIDEOS_FEED = YOUTUBE_GDATA_SERVER + "/feeds/videos"; /** * The prefix of the User Feeds */ public static final String USER_FEED_PREFIX = YOUTUBE_GDATA_SERVER + "/feeds/users/"; /** * The URL suffix of the test user's uploads feed */ public static final String UPLOADS_FEED_SUFFIX = "/uploads"; /** * The URL suffix of the test user's favorites feed */ public static final String FAVORITES_FEED_SUFFIX = "/favorites"; /** * The URL suffix of the test user's subscriptions feed */ public static final String SUBSCRIPTIONS_FEED_SUFFIX = "/subscriptions"; /** * The URL suffix of the test user's playlists feed */ public static final String PLAYLISTS_FEED_SUFFIX = "/playlists"; /** * The default user if the user does not enter one. */ private static String defaultTestUser = "YTdebates"; /** * */ private YouTubeClient() { } /** * Prints a list of all standard feeds. * * @param service a YouTubeService object. * @throws ServiceException * If the service is unable to handle the request. * @throws IOException error sending request or reading the feed. */ private static void printStandardFeeds(YouTubeService service) throws IOException, ServiceException { printVideoFeed(service, MOST_VIEWED_FEED, false); printVideoFeed(service, TOP_RATED_FEED, false); printVideoFeed(service, RECENTLY_FEATURED_FEED, false); printVideoFeed(service, WATCH_ON_MOBILE_FEED, false); } /** * Prints a String, a newline, and a number of '-' characters equal to the * String's length. * * @param feedTitle - the title to print underlined */ private static void printUnderlined(String feedTitle) { System.out.println(feedTitle); for (int i = 0; i < feedTitle.length(); ++i) { System.out.print("-"); } System.out.println("\n"); } /** * Prints a VideoEntry, optionally showing its responses and comment feeds. * * @param prefix a string to be shown before each entry * @param videoEntry the VideoEntry to be printed * @param showCommentsAndResponses true if the comments and responses feeds * should be printed * @throws ServiceException * If the service is unable to handle the * request. * @throws IOException error sending request or reading the feed. */ private static void printVideoEntry(String prefix, VideoEntry videoEntry, boolean showCommentsAndResponses) throws IOException, ServiceException { System.out.println(prefix); if (videoEntry.getTitle() != null) { System.out.printf("Title: %s\n", videoEntry.getTitle().getPlainText()); } if (videoEntry.getSummary() != null) { System.out.printf("Summary: %s\n", videoEntry.getSummary().getPlainText()); } YouTubeMediaGroup mediaGroup = videoEntry.getMediaGroup(); if(mediaGroup != null) { MediaPlayer mediaPlayer = mediaGroup.getPlayer(); System.out.println("Web Player URL: " + mediaPlayer.getUrl()); MediaKeywords keywords = mediaGroup.getKeywords(); System.out.print("Keywords: "); for(String keyword : keywords.getKeywords()) { System.out.print(keyword + ","); } System.out.println(); System.out.println("\tThumbnails:"); for(MediaThumbnail mediaThumbnail : mediaGroup.getThumbnails()) { System.out.println("\t\tThumbnail URL: " + mediaThumbnail.getUrl()); System.out.println("\t\tThumbnail Time Index: " + mediaThumbnail.getTime()); System.out.println(); } System.out.println("\tMedia:"); for(YouTubeMediaContent mediaContent : mediaGroup.getYouTubeContents()) { System.out.println("\t\tMedia Location: "+mediaContent.getUrl()); System.out.println("\t\tMedia Type: "+mediaContent.getType()); System.out.println("\t\tDuration: " + mediaContent.getDuration()); System.out.println(); } System.out.println(); } if (showCommentsAndResponses) { printResponsesFeed(videoEntry); System.out.println(""); printCommentsFeed(videoEntry); System.out.println(""); System.out.println(""); } } /** * Prints the responses feed of a VideoEntry. * * @param videoEntry the VideoEntry whose responses are to be printed * @throws ServiceException * If the service is unable to handle the request. * @throws IOException error sending request or reading the feed. */ private static void printResponsesFeed(VideoEntry videoEntry) throws IOException, ServiceException { if (videoEntry.getVideoResponsesLink() != null) { String videoResponsesFeedUrl = videoEntry.getVideoResponsesLink().getHref(); System.out.println(); printVideoFeed((YouTubeService) videoEntry.getService(), videoResponsesFeedUrl, false); } } /** * Prints the comments feed of a VideoEntry. * * @param videoEntry the VideoEntry whose comments are to be printed * @throws ServiceException * If the service is unable to handle the request. * @throws IOException error sending request or reading the feed. */ private static void printCommentsFeed(VideoEntry videoEntry) throws IOException, ServiceException { Comments comments = videoEntry.getComments(); if (comments != null && comments.getFeedLink() != null) { System.out.println("\tComments:"); printFeed(videoEntry.getService(), comments.getFeedLink().getHref(), "Comment"); } } /** * Prints a basic feed, such as the comments or responses feed, retrieved from * a feedUrl. * * @param service a YouTubeService object * @param feedUrl the url of the feed * @param prefix a prefix string to be printed in front of each entry field * @throws ServiceException * If the service is unable to handle the request. * @throws IOException error sending request or reading the feed. */ private static void printFeed(Service service, String feedUrl, String prefix) throws IOException, ServiceException { Feed feed = service.getFeed(new URL(feedUrl), Feed.class); for (Entry e : feed.getEntries()) { printEntry(e, prefix); } } /** * Prints a basic Entry, usually from a comments or responses feed. * * @param entry the entry to be printed * @param prefix a prefix to be printed before each entry attribute */ private static void printEntry(Entry entry, String prefix) { System.out.println("\t\t" + prefix + " Title: " + entry.getTitle().getPlainText()); if (entry.getContent() != null) { TextContent content = (TextContent) entry.getContent(); System.out.println("\t\t" + prefix + " Content: " + content.getContent().getPlainText()); } System.out.println("\t\tURL: " + entry.getHtmlLink().getHref()); } /** * Prints a PlaylistEntry by retrieving the Description of the PlayList, * followed by the Titles and URLs of each entry in the feed. * * @param prefix a string to be printed before each entry * @param playlistLinkEntry the PlaylistEntry to be printed * @param showPlaylistContents if true, show the list of videos in the * playlist * @throws ServiceException * If the service is unable to handle the * request. * @throws IOException error sending request or reading the feed. */ private static void printPlaylistEntry(String prefix, PlaylistLinkEntry playlistLinkEntry, boolean showPlaylistContents) throws IOException, ServiceException { System.out.println(prefix); System.out.printf("Description: %s\n", playlistLinkEntry.getDescription()); if (showPlaylistContents) { FeedLink feedLink = playlistLinkEntry.getFeedLink(); printPlaylist(playlistLinkEntry.getService(), feedLink.getHref()); } } /** * Prints a playlist feed as a series of Titles and URLs. * * @param service a YouTubeService object * @param playlistUrl the url of the playlist * @throws ServiceException * If the service is unable to handle the request. * @throws IOException error sending request or reading the feed. */ private static void printPlaylist(Service service, String playlistUrl) throws IOException, ServiceException { PlaylistFeed playlistFeed = service.getFeed(new URL(playlistUrl), PlaylistFeed.class); if (playlistFeed != null) { for (PlaylistEntry e : playlistFeed.getEntries()) { System.out.println("\tPlaylist Entry: " + e.getTitle().getPlainText()); System.out.println("\tPlaylist URL: " + e.getHtmlLink().getHref()); } } } /** * Prints a FeedLinkEntry as a Title and URL String. * * @param feedLinkEntry the FeedLinkEntry to be printed */ private static void printFeedLinkEntry(FeedLinkEntry feedLinkEntry) { if (feedLinkEntry.getTitle() != null) { System.out.printf("Title: %s\n", feedLinkEntry.getTitle().getPlainText()); } System.out.println("FeedLink: " + feedLinkEntry.getFeedLink().getHref()); } /** * Prints a SubscriptionEntry, which is a FeedLink entry. * * @param subEntry the SubscriptionEntry to be printed */ private static void printSubscriptionEntry(SubscriptionEntry subEntry) { printFeedLinkEntry(subEntry); } /** * Reads a line of text from the standard input. * @throws IOException if unable to read a line from the standard input * @return a line of text read from the standard input */ private static String readLine() throws IOException { BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(System.in)); return bufferedReader.readLine(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -