String videoId = videoSubmission.getVideoId();
// This will retrieve video info from the Uploads feed of the user who owns the video.
// This should always be the freshest data, but it relies on the AuthSub token being valid.
VideoEntry videoEntry = apiManager.getUploadsVideoEntry(videoId);
if (videoEntry == null) {
// Try an unauthenticated request to the specific user's uploads feed next.
AdminConfig admin = adminConfigDao.getAdminConfig();
String clientId = admin.getClientId();
YouTubeApiHelper apiHelper = new YouTubeApiHelper(clientId);
videoEntry = apiHelper.getUploadsVideoEntry(videoSubmission.getYouTubeName(), videoId);
if (videoEntry == null) {
// Fall back on looking for the video in the public feed.
videoEntry = apiHelper.getVideoEntry(videoId);
if (videoEntry == null) {
// The video must have been deleted...
LOG.info(String.format("Unable to find YouTube video id '%s'.", videoId));
videoSubmission.setYouTubeState("NOT_FOUND");
}
}
}
if (videoEntry != null) {
try {
YtPublicationState state = videoEntry.getPublicationState();
String stateValue;
if (state == null) {
// TODO: Find some way to check whether the video is embeddable and/or private, and
// populate that info. Because we're getting the video from the authenticated
// uploads feed (by default), that info isn't easily exposed on the videoEntry
// object. An alternative would be to get an instance from the public video feed
// and check that.
List<YouTubeMediaRating> ratings = videoEntry.getMediaGroup().getYouTubeRatings();
if (ratings.size() == 0) {
stateValue = "OKAY";
} else {
StringBuffer restrictionBuffer = new StringBuffer("RESTRICTED IN: ");
for (YouTubeMediaRating rating : ratings) {
restrictionBuffer.append(rating.getCountries());
}
stateValue = restrictionBuffer.toString();
}
} else {
stateValue = state.getState().toString();
}
if (!stateValue.equals(videoSubmission.getYouTubeState())) {
LOG.info(String.format("YouTube state differs: '%s' (local) vs. '%s' (YT).",
videoSubmission.getYouTubeState(), stateValue));
videoSubmission.setYouTubeState(stateValue);
videoSubmission.setUpdated(now);
}
String title = videoEntry.getTitle().getPlainText();
if (!title.equals(videoSubmission.getVideoTitle())) {
LOG.info(String.format("Title differs: '%s' (local) vs. '%s' (YT).", videoSubmission
.getVideoTitle(), title));
videoSubmission.setVideoTitle(title);
videoSubmission.setUpdated(now);
}
String description = videoEntry.getMediaGroup().getDescription().getPlainTextContent();
if (!description.equals(videoSubmission.getVideoDescription())) {
LOG.info(String.format("Description differs: '%s' (local) vs. '%s' (YT).",
videoSubmission.getVideoDescription(), description));
videoSubmission.setVideoDescription(description);
videoSubmission.setUpdated(now);
}
List<String> tags = videoEntry.getMediaGroup().getKeywords().getKeywords();
String sortedTags = util.sortedJoin(tags, ",");
if (!sortedTags.equals(videoSubmission.getVideoTags())) {
LOG.info(String.format("Tags differs: '%s' (local) vs. '%s' (YT).", videoSubmission
.getVideoTags(), sortedTags));
videoSubmission.setVideoTags(sortedTags);
videoSubmission.setUpdated(now);
}
} catch (NullPointerException e) {
LOG.info(String.format("Couldn't get metadata for video id '%s'. It may not have been"
+ " accepted by YouTube.", videoId));
}
// Unconditionally update view count info, but don't call setUpdated() since this is an
// auto-update.
YtStatistics stats = videoEntry.getStatistics();
if (stats != null) {
videoSubmission.setViewCount(stats.getViewCount());
}
LOG.info(String.format("Finished syncing video id '%s'", videoId));