void refresh() throws IOException { Disposable disposable = // Observable.just(ytApi.playlistItems() .list(YOUTUBE_PLAYLIST_PART) .setPlaylistId(YOUTUBE_PLAYLIST_ID) .setPageToken(null) .setFields(YOUTUBE_PLAYLIST_FIELDS) .setMaxResults(YOUTUBE_PLAYLIST_MAX_RESULTS) .setKey(API_KEY) // ) .map(AbstractGoogleClientRequest::execute) .map(PlaylistItemListResponse::getItems) .flatMap(playlistItems -> Observable.fromIterable(playlistItems) .map(item -> item.getSnippet().getResourceId().getVideoId())) .toList() .map(ids -> ytApi.videos().list(YOUTUBE_VIDEOS_PART).setFields(YOUTUBE_VIDEOS_FIELDS) // .setKey(API_KEY).setId(TextUtils.join(",", ids)).execute()) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .doOnError( throwable -> Log.e(TAG, "accept() called with: throwable = [" + throwable + "]")) .doOnSuccess( response -> Log.d(TAG, "accept() called with: response = [" + response + "]")) .onErrorReturnItem(new VideoListResponse()) // Bad work around .doOnSuccess(liveData::setValue) .subscribe(); disposables.add(disposable); } }
/** * @return url of thumbnail if the video is fully processed */ public static boolean checkIfProcessed(String videoId, YouTube youtube) { try { YouTube.Videos.List list = youtube.videos().list("processingDetails"); list.setId(videoId); VideoListResponse listResponse = list.execute(); List<Video> videos = listResponse.getItems(); if (videos.size() == 1) { Video video = videos.get(0); String status = video.getProcessingDetails().getProcessingStatus(); Log.e(TAG, String.format("Processing status of [%s] is [%s]", videoId, status)); if (status.equals(SUCCEEDED)) { return true; } } else { // can't find the video Log.e(TAG, String.format("Can't find video with ID [%s]", videoId)); return false; } } catch (IOException e) { Log.e(TAG, "Error fetching video metadata", e); } return false; } }
/** * Returns a list of videos that match the API request parameters. * * Create a request for the method "videos.list". * * This request holds the parameters needed by the youtube server. After setting any optional * parameters, call the {@link List#execute()} method to invoke the remote operation. * * @param part The part parameter specifies a comma-separated list of one or more video resource properties that * the API response will include. If the parameter identifies a property that contains child * properties, the child properties will be included in the response. For example, in a video * resource, the snippet property contains the channelId, title, description, tags, and * categoryId properties. As such, if you set part=snippet, the API response will contain all * of those properties. * @return the request */ public List list(java.lang.String part) throws java.io.IOException { List result = new List(part); initialize(result); return result; }
/** * Given a Youtube videoId, return the relevant Youtube Video object. * @param videoId videoId * @return List of Videos * @throws IOException */ List<Video> getVideoList(String videoId) throws IOException { VideoListResponse videosListResponse = this.youtube.videos().list("snippet,statistics") .setId(videoId) .setKey(config.getApiKey()) .execute(); if (videosListResponse.getItems().size() == 0) { LOGGER.debug("No Youtube videos found for videoId: {}", videoId); return new ArrayList<>(); } return videosListResponse.getItems(); }