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); } }
System.out.println( " Video Id" + rId.getVideoId() ); System.out.println( " Title: " + singleVideo.getSnippet().getTitle() ); System.out.println( " Description: " + singleVideo.getSnippet().getDescription() );
private List<YoutubeInfo> convertTo( Iterator<SearchResult> iteratorSearchResults ) { List<YoutubeInfo> results = new ArrayList<YoutubeInfo>(); if ( !iteratorSearchResults.hasNext() ) { return results; } while ( iteratorSearchResults.hasNext() ) { SearchResult singleVideo = iteratorSearchResults.next(); ResourceId rId = singleVideo.getId(); // Confirm that the result represents a video. Otherwise, the // item will not contain a video ID. if ( rId.getKind().equals( "youtube#video" ) ) { YoutubeInfo yi = new YoutubeInfo(); yi.thumbnail = singleVideo.getSnippet().getThumbnails().getDefault().getUrl(); yi.id = rId.getVideoId(); yi.title = singleVideo.getSnippet().getTitle(); yi.description = singleVideo.getSnippet().getDescription(); results.add( yi ); } } return results; }
public List<SimpleResult> getResults(String query, int numresults) { List<SimpleResult> urls = new ArrayList<>(); search.setQ(query); search.setMaxResults((long) numresults); SearchListResponse searchResponse; try { searchResponse = search.execute(); List<SearchResult> searchResultList = searchResponse.getItems(); searchResultList.forEach((sr) -> urls.add(new SimpleResult(sr.getId().getVideoId(), sr.getSnippet().getTitle()))); } catch (GoogleJsonResponseException e) { if (e.getMessage().contains("quotaExceeded") || e.getMessage().contains("keyInvalid")) { if (setupNextKey()) { return getResults(query, numresults); } } else { Launcher.logToDiscord(e, "youtube-search-error", "<--", "code", e.getDetails().getCode(), "message", e.getDetails().getMessage()); } } catch (IOException ex) { DiscordBot.LOGGER.error("YTSearch failure: " + ex.toString()); return null; } return urls; }
videoIds.add(item.getSnippet().getResourceId().getVideoId());
/** * Given a feed and an after and before date, fetch all relevant user videos * and place them into the datumQueue for post-processing. * @param feed ActivityListResponse * @param afterDate DateTime * @param beforeDate DateTime * @throws IOException IOException * @throws InterruptedException InterruptedException */ void processActivityFeed(ActivityListResponse feed, DateTime afterDate, DateTime beforeDate) throws IOException, InterruptedException { for (com.google.api.services.youtube.model.Activity activity : feed.getItems()) { try { List<Video> videos = new ArrayList<>(); if (activity.getContentDetails().getUpload() != null) { videos.addAll(getVideoList(activity.getContentDetails().getUpload().getVideoId())); } if (activity.getContentDetails().getPlaylistItem() != null && activity.getContentDetails().getPlaylistItem().getResourceId() != null) { videos.addAll(getVideoList(activity.getContentDetails().getPlaylistItem().getResourceId().getVideoId())); } processVideos(videos, afterDate, beforeDate, activity, feed); } catch (Exception ex) { LOGGER.error("Error while trying to process activity: {}, {}", activity, ex); } } }