使用Google Youtube API获取观看次数 [英] Get view count using Google Youtube API

查看:78
本文介绍了使用Google Youtube API获取观看次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取一组视频的观看次数.以下是我的代码的相关部分.

I want to get the view count of set of videos. Following is the relevant part of my code.

  SearchResult singleVideo = iteratorSearchResults.next();
  ResourceId rId = singleVideo.getId();

  // Double checks the kind is video.
  if (rId.getKind().equals("youtube#video")) {
    Thumbnail thumbnail = singleVideo.getSnippet().getThumbnails().get("default");

    System.out.println(" Video Id" + rId.getVideoId());
    System.out.println(" Title: " + singleVideo.getSnippet().getTitle());
    System.out.println(" Thumbnail: " + thumbnail.getUrl());

    YouTube.Videos.List list = youtube.videos().list("statistics");
    list.setId(rId.getVideoId());
    list.setKey("youtube.apikey");            
    Video v = list.execute().getItems().get(0);
    System.out.println("The view count is: "+v.getStatistics().getViewCount());
    System.out.println("\n-------------------------------------------------------------\n");
  }

这在"YouTube.Videos.Lists list = youtube.videos().list("statistics");行中出现以下错误.

This gives the following error in the line "YouTube.Videos.Lists list = youtube.videos().list("statistics");".

error: method list in class YouTube.Videos cannot be applied to given types;

推荐答案

如果这是编译错误,则说明您所包含的库版本可能存在问题.我尝试了 youtube API文档中的示例代码, .

If this is a compilation error then there might be some issue with the library version that you have included. I tried sample code from youtube API docs and it worked for me.

我从示例中删除了一些额外的代码,以展示如何获取单个视频的观看次数:

I have removed some extra code from the sample to show how view counts can be retrieved for a single video:

    import com.google.api.client.googleapis.json.GoogleJsonResponseException;
    import com.google.api.client.http.HttpRequest;
    import com.google.api.client.http.HttpRequestInitializer;
    import com.google.api.services.samples.youtube.cmdline.Auth;
    import com.google.api.services.youtube.YouTube;
    import com.google.api.services.youtube.model.Video;
    import com.google.api.services.youtube.model.VideoListResponse;

    import java.io.IOException;
    import java.math.BigInteger;

public class GeolocationSearch {
    public static void main(String[] args) {

        try {
            YouTube youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, new HttpRequestInitializer() {
                @Override
                public void initialize(HttpRequest request) throws IOException {
                }
            }).setApplicationName("APP_ID").build();

            String apiKey = "API_KEY";
            YouTube.Videos.List listVideosRequest = youtube.videos().list("statistics");
            listVideosRequest.setId("lf_wVfwpfp8"); // add list of video IDs here
            listVideosRequest.setKey(apiKey);
            VideoListResponse listResponse = listVideosRequest.execute();

            Video video = listResponse.getItems().get(0);

            BigInteger viewCount = video.getStatistics().getViewCount();

            System.out.println(" ViewCount: " + viewCount);
            System.out.println("\n-------------------------------------------------------------\n");

        } catch (GoogleJsonResponseException e) {
            System.err.println("There was a service error: " + e.getDetails().getCode() + " : "
                    + e.getDetails().getMessage());
        } catch (IOException e) {
            System.err.println("There was an IO error: " + e.getCause() + " : " + e.getMessage());
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

}

这篇关于使用Google Youtube API获取观看次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆