2
votes

I have a pretty straight forward task -

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);
}

I don't know how to create a videoEntry given only the URL..

2

2 Answers

2
votes

You can use this:

        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
0
votes

I have got a method to do what I intended to. I did not require name of the Channel (username that uploaded the video). All I need is a URL. YouTube has an ID corresponding to each video. For example, for the video: http://www.youtube.com/watch?v=f3q3JkNUPmI , the ID is "f3q3JkNUPmI" (without the quotes). So, all you need to do is to create a String containing the feed link that you want to get. This can be created as:

String Video_Related_Feed="https://gdata.youtube.com/feeds/api/videos/f3q3JkNUPmI/related?v=2" (don't replace https with http -- it does not work) Obviously, one can automate this .. I'm just giving the hard-coded example. Now get the actual Feed based on this address:

VideoFeed videoFeed = service.getFeed(new URL(Video_Related_Feed), VideoFeed.class); This feed contains VideoEntry of the Related videos (corresponding to our original URL), each of which can be accessed by a for loop: for (VideoEntry ve : videoFeed.getEntries()) { insert your code here }

References: https://developers.google.com/youtube/2.0/reference andhttps://developers.google.com/youtube/2.0/developers_guide_java (sorry, I had to write last link like that because I couldn't post more than 2 URLs).

I hope this helps someone.