Пример #1
0
  public List<VideoItem> getFavoriteVideo(List<String> favoriteVideoIds) {
    Joiner stringJoiner = Joiner.on(',');
    String videoId = stringJoiner.join(favoriteVideoIds);

    //        Log.d(TAG, "String video ID: " + videoId);

    // Call the YouTube Data API's youtube.videos.list method to
    // retrieve the resources that represent the specified videos
    YouTube.Videos.List listVideosRequest = null;
    try {
      listVideosRequest = youtube.videos().list("snippet, recordingDetails").setId(videoId);
      listVideosRequest.setKey(KEY2);
      VideoListResponse listResponse = listVideosRequest.execute();

      List<Video> videoList = listResponse.getItems();

      List<VideoItem> items = new ArrayList<VideoItem>();
      for (Video video : videoList) {
        VideoItem item = new VideoItem();
        item.setTitle(video.getSnippet().getTitle());
        try {
          item.setPublishedDate(
              new SimpleDateFormat("MM/dd/yyyy")
                  .format(
                      (new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
                          .parse(video.getSnippet().getPublishedAt().toString()))));
        } catch (ParseException e) {
          Log.d(TAG, "Could parse the date: " + e);
        }
        item.setDescription(video.getSnippet().getDescription());
        item.setThumbnailURL(video.getSnippet().getThumbnails().getDefault().getUrl());
        // same as id in search method
        item.setId(video.getId());

        //                item.setViewCount(video.getStatistics().getViewCount().intValue());
        //                Log.d(TAG, "view count: " +
        // video.getStatistics().getViewCount().intValue());

        items.add(item);
      }
      return items;
    } catch (IOException e) {
      Log.d(TAG, "Could not search: " + e);
      return null;
    }
  }
  @SuppressWarnings("deprecation")
  @Override
  public Boolean crawl(RssEntityBean site) throws CrawlerException {
    log.info("start YouTube crawl channel id = " + site.url);
    Boolean updated = false;
    YouTube youtube =
        new YouTube.Builder(
                HTTP_TRANSPORT,
                JSON_FACTORY,
                new HttpRequestInitializer() {
                  public void initialize(HttpRequest request) throws IOException {}
                })
            .setApplicationName("youtube-cmdline-search-sample")
            .build();

    try {
      YouTube.Search.List search = youtube.search().list("id,snippet");
      String apiKey = Conf.getValue("youtube.appkey");
      search.setKey(apiKey);
      search.setType("video");
      search.setOrder("date");
      search.setMaxResults(maxResult);
      search.setChannelId(site.url);

      SearchListResponse searchResponse = search.execute();
      List<SearchResult> searchResultList = searchResponse.getItems();
      log.info("search response video count = " + searchResultList.size());
      for (SearchResult result : searchResultList) {

        SearchResultSnippet snippet = result.getSnippet();
        DateTime publishDate = snippet.getPublishedAt();
        DateTime lastCrawlDate = null;
        if (site.lastCrawl != null) {
          lastCrawlDate = new DateTime(site.lastCrawl);
        }
        if ((lastCrawlDate == null) || (publishDate.getValue() > lastCrawlDate.getValue())) {
          YouTube.Videos.List videolist = youtube.videos().list("id,snippet");
          videolist.setId(result.getId().getVideoId());
          videolist.setKey(apiKey);
          VideoListResponse videoListResponse = videolist.execute();
          List<Video> videos = videoListResponse.getItems();
          if (videos.size() != 1) {
            log.warn("video not found : " + videolist.getId());
            continue;
          }
          Video video = videos.get(0);

          ArticleEntityBean article = new ArticleEntityBean();
          article.link = video.getId();
          article.title = snippet.getTitle();
          article.description = snippet.getDescription();
          article.imageUrl = snippet.getThumbnails().getDefault().getUrl();
          article.url = site.url;
          article.createdAt = snippet.getPublishedAt();
          String fixHour = Conf.getValue("fix_time");
          Date now = new Date();
          Date fixDate =
              new Date(now.getYear(), now.getMonth(), now.getDate(), Integer.parseInt(fixHour), 0);
          if (now.getTime() > fixDate.getTime()) {
            article.publishedAt = tomorrow;
          } else {
            article.publishedAt = today;
          }
          article.site = site.site;
          article.type = site.type;
          article.tags = site.defaultTag;
          log.info("save article video id = " + article.link + " / video title = " + article.title);
          article.save();
          updated = true;
        }
      }
    } catch (IOException e) {
      log.warn("youtube api error : " + e.getMessage());
      throw new CrawlerException(e);
    }
    log.info("end YouTube crawl");
    return updated;
  }