我有一个非常简单的任务 -
Given the URL of a video, I want to extract information of its Related videos (and by information I mean their Title, Tags, Description). I use Java version of the YouTube API and couldn't find out how to do that quickly. Also, this site: https://developers.google.com/youtube/2.0/developers_guide_java provides some information. The code they provide is:
if (videoEntry.getRelatedVideosLink() != null) {
String feedUrl = videoEntry.getRelatedVideosLink().getHref();
VideoFeed videoFeed = service.getFeed(new URL(feedUrl), VideoFeed.class);
printVideoFeed(videoFeed, true);
}
我不知道如何创建仅给出 URL 的 videoEntry..
最佳答案
你可以使用这个:
class YouTubeVideoInfo {
private String channel;
private String url;
private long views;
private int comments;
private int ratings;
private int likes;
private int dislikes;
private String thumbnail;
private String title;
.....
}
public static final String YOUTUBE_GDATA_SERVER = "http://gdata.youtube.com";
public static final String USER_FEED_PREFIX = YOUTUBE_GDATA_SERVER + "/feeds/api/users/";
public static final String UPLOADS_FEED_SUFFIX = "/uploads";
...............
public YouTubeVideoInfo getVideoInfo(YouTubeService service, String channel, String url) {
VideoFeed videoFeed = service.getFeed(
new URL(USER_FEED_PREFIX + channel + UPLOADS_FEED_SUFFIX), VideoFeed.class);
List<VideoEntry> videoEntries = videoFeed.getEntries();
for (VideoEntry videoEntry : videoEntries) {
YouTubeMediaGroup mediaGroup = videoEntry.getMediaGroup();
if (mediaGroup != null && mediaGroup.getPlayer() != null && videoEntry.getTitle() != null) {
if (url.equals(mediaGroup.getPlayer().getUrl())) {
String title = videoEntry.getTitle().getPlainText();
MediaKeywords keywords = mediaGroup.getKeywords();
MediaPlayer mediaPlayer = mediaGroup.getPlayer();
final YtStatistics statistics = videoEntry.getStatistics();
final YouTubeVideoInfo videoInfo = new YouTubeVideoInfo(channel,
mediaPlayer.getUrl(), statistics != null
? statistics.getViewCount() : 0);
if (videoEntry.getComments() != null
&& videoEntry.getComments().getFeedLink() != null)
videoInfo.comments =
videoEntry.getComments().getFeedLink().getCountHint();
final Rating rating = videoEntry.getRating();
if (rating != null)
videoInfo.ratings = rating.getNumRaters();
final YtRating ytRating = videoEntry.getYtRating();
if (ytRating != null) {
videoInfo.likes = ytRating.getNumLikes();
videoInfo.dislikes = ytRating.getNumDislikes();
}
final List<MediaThumbnail> thumbnails = mediaGroup.getThumbnails();
if (!thumbnails.isEmpty())
videoInfo.thumbnail = thumbnails.get(thumbnails.size() / 2).getUrl();
if (videoEntry.getTitle() != null)
videoInfo.title = videoEntry.getTitle().getPlainText();
return videoInfo;
}
}
.... // exception handling
关于java - YouTube API : How to obtain text of Related videos, 给定了视频的 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9897635/