Skip to content

Commit

Permalink
Update getVideoData method to iteratively get video data
Browse files Browse the repository at this point in the history
YouTube API does not allow getting more than 50 videos at once, so we need to iteratively request data if the provided list with ids has more than 50 items.
  • Loading branch information
leingenm committed Aug 28, 2024
1 parent 125adef commit 138ef8b
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/main/java/com/ypm/service/VideoServiceImp.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,27 @@ public void deleteVideo(String accessToken, String videoId) throws IOException {

@Override
public List<VideoDto> getVideoData(List<String> videoIds) throws IOException {
var items = youTubeClient
.videos()
.list(List.of(Part.SNIPPET.toString(), Part.CONTENT_DETAILS.toString()))
.setId(videoIds)
.setAccessToken(tokenService.getAccessToken())
.execute()
.getItems();
final int maxResults = 50;

// Remove duplicates from the incoming list with ids
videoIds = videoIds.stream().distinct().toList();
var videoDtoList = new ArrayList<VideoDto>();

for (int i = 0; i < videoIds.size(); i += maxResults) {
var subList = videoIds.subList(i, Math.min(i + maxResults, videoIds.size()));

var itemsSub = youTubeClient
.videos()
.list(List.of(Part.SNIPPET.toString(), Part.CONTENT_DETAILS.toString()))
.setId(subList)
.setOauthToken(tokenService.getAccessToken());

var videoListResponse = itemsSub.execute();
var videos = videoListResponse.getItems();
videoDtoList.addAll(VideoMapper.mapToVideoDto(videos));
}

return VideoMapper.mapToVideoDto(items);
return videoDtoList;
}

@Override
Expand Down

0 comments on commit 138ef8b

Please sign in to comment.