Skip to content

Instantly share code, notes, and snippets.

@candyan
Created May 15, 2023 04:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save candyan/bea61934b26b8d0cc95833793bf8baeb to your computer and use it in GitHub Desktop.
Save candyan/bea61934b26b8d0cc95833793bf8baeb to your computer and use it in GitHub Desktop.
public void synchronousMediaData(AccountDataEntity accountDataEntity) {
LogUtils.info(log, "[start synchronous youtube data]: account={}", accountDataEntity.getChannel());
if (accountDataEntity.getInfluencerId() == 0) {
throw new RuntimeException("influencer id is 0");
}
InfluencerDataEntity influencerDataEntity = influencerRepository.selectById(accountDataEntity.getInfluencerId());
if (influencerDataEntity == null) {
throw new RuntimeException("influencer is not exist");
}
YoutubeInfo youtube = youtubeService.getYoutubeInfo(accountDataEntity.getLink());
if (Objects.isNull(youtube)) {
throw new RuntimeException("sync youtube user not exist");
}
YTSearchVideoRequest searchVideoRequest = YTSearchVideoRequest.builder()
.link(accountDataEntity.getLink())
.build();
List<YTSearchVideoResponse.VideoItem> last3MonthVideos = new ArrayList<>();
Set<String> hastagsSet = CollectionUtils.isNotEmpty(youtube.getHashTags()) ? youtube.getHashTags() : new HashSet<>();
boolean hasMore = true;
while (hasMore) {
YTSearchVideoResponse searchVideoResponse = youtubeService.getChannelVideos(
searchVideoRequest.getLink(),
searchVideoRequest.getPageToken(),
false
);
if (CollectionUtils.isNotEmpty(searchVideoResponse.getVideoItems())) {
last3MonthVideos.addAll(searchVideoResponse.getVideoItems());
Date beforeDate = DateUtil.getDayStart(DateUtil.decreaseMonths(new Date(), 3));
YTSearchVideoResponse.VideoItem lastMeida = last3MonthVideos.get(last3MonthVideos.size() - 1);
if (lastMeida.getPublishAtDate().before(beforeDate)) {
hasMore = false;
}
if (StringUtils.isNotEmpty(searchVideoResponse.getNextPageToken())) {
searchVideoRequest.setPageToken(searchVideoResponse.getNextPageToken());
searchVideoResponse.getVideoItems().forEach(x -> {
if (CollectionUtils.isNotEmpty(x.getTags())) {
hastagsSet.addAll(x.getTags());
}
});
} else {
hasMore = false;
}
} else {
hasMore = false;
}
}
if (CollectionUtils.isEmpty(last3MonthVideos)) {
YTSearchVideoRequest searchShortVideoRequest = YTSearchVideoRequest.builder()
.link(accountDataEntity.getLink())
.filter("shorts_latest")
.build();
boolean hasMoreShorts = true;
while (hasMoreShorts) {
YTSearchVideoResponse searchVideoResponse = youtubeService.getChannelVideos(
searchShortVideoRequest.getLink(),
searchShortVideoRequest.getPageToken(),
true
);
if (CollectionUtils.isNotEmpty(searchVideoResponse.getVideoItems())) {
last3MonthVideos.addAll(searchVideoResponse.getVideoItems());
Date beforeDate = DateUtil.getDayStart(DateUtil.decreaseMonths(new Date(), 3));
YTSearchVideoResponse.VideoItem lastMeida = last3MonthVideos.get(last3MonthVideos.size() - 1);
if (lastMeida.getPublishAtDate().before(beforeDate)) {
hasMoreShorts = false;
}
if (StringUtils.isNotEmpty(searchVideoResponse.getNextPageToken())) {
searchShortVideoRequest.setPageToken(searchVideoResponse.getNextPageToken());
searchVideoResponse.getVideoItems().forEach(x -> {
if (CollectionUtils.isNotEmpty(x.getTags())) {
hastagsSet.addAll(x.getTags());
}
});
} else {
hasMoreShorts = false;
}
} else {
hasMoreShorts = false;
}
}
}
youtube.setHashTags(hastagsSet);
LogUtils.info(log, "[process synchronous youtube data](updateYoutubeVideos): account={}", accountDataEntity.getChannel());
List<MediaPostESEntity> last3MPosts = null;
boolean isGeneralAccount = Optional.ofNullable(influencerDataEntity.getType()).orElse((byte) 0) == 0;
if (CollectionUtils.isNotEmpty(last3MonthVideos)) {
last3MonthVideos = last3MonthVideos.stream().sorted(
Comparator.comparing(YTSearchVideoResponse.VideoItem::getPublishAtDate).reversed()
).collect(Collectors.toList());
youtube.setLastPublishedAt(last3MonthVideos.get(0).getPublishAtDate());
last3MPosts = mediaPostESService.updateYoutubeVideos(
last3MonthVideos,
accountDataEntity,
youtube,
isGeneralAccount,
false
);
}
LogUtils.info(log, "[process synchronous youtube data](updateMediaAccount): account={}", accountDataEntity.getChannel());
AccountDataEntity updatedADE = updateMediaAccount(
accountDataEntity.getId(),
youtube,
last3MPosts,
isGeneralAccount
);
LogUtils.info(log, "[process synchronous youtube data](updateInfluencer): account={}", accountDataEntity.getChannel());
updateInfluencer(influencerDataEntity, youtube);
LogUtils.info(log, "[process synchronous youtube data](updateAllMediaPostAccountInfo): account={}", accountDataEntity.getChannel());
mediaPostESService.updateAllMediaPostAccountInfo(last3MPosts, updatedADE, influencerDataEntity);
mediaPostESService.saveAll(last3MPosts);
LogUtils.info(log, "[end synchronous youtube data]: account={}", accountDataEntity.getChannel());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment