use of com.google.api.services.blogger.model.Post in project data-transfer-project by google.
the class GoogleBloggerImporter method insertActivity.
private void insertActivity(IdempotentImportExecutor idempotentExecutor, SocialActivityActor actor, SocialActivityModel activity, String blogId, TokensAndUrlAuthData authData) throws Exception {
String content = activity.getContent() == null ? "" : activity.getContent();
Collection<SocialActivityAttachment> linkAttachments = activity.getAttachments().stream().filter(attachment -> attachment.getType() == SocialActivityAttachmentType.LINK).collect(Collectors.toList());
Collection<SocialActivityAttachment> imageAttachments = activity.getAttachments().stream().filter(attachment -> attachment.getType() == SocialActivityAttachmentType.IMAGE).collect(Collectors.toList());
// don't know how they were laid out in the originating service.
for (SocialActivityAttachment attachment : linkAttachments) {
content = "<a href=\"" + attachment.getUrl() + "\">" + attachment.getName() + "</a>\n</hr>\n" + content;
}
if (!imageAttachments.isEmpty()) {
// Store any attached images in Drive in a new folder.
Drive driveInterface = getOrCreateDriveService(authData);
String folderId = idempotentExecutor.executeOrThrowException("MainAlbum", "Photo Album", () -> createAlbumFolder(driveInterface));
for (SocialActivityAttachment image : imageAttachments) {
try {
String newImgSrc = idempotentExecutor.executeAndSwallowIOExceptions(image.toString(), "Image", () -> uploadImage(image, driveInterface, folderId));
content += "\n<hr/><img src=\"" + newImgSrc + "\">";
} catch (RuntimeException e) {
throw new IOException("Couldn't import: " + imageAttachments, e);
}
}
}
String title = "";
if (activity.getTitle() != null && !Strings.isNullOrEmpty(activity.getTitle())) {
title = activity.getTitle();
}
Post post = new Post().setTitle("Imported post: " + title).setContent(content);
if (actor != null) {
Post.Author author = new Post.Author();
if (!Strings.isNullOrEmpty(actor.getName())) {
author.setDisplayName(actor.getName());
}
if (!Strings.isNullOrEmpty(actor.getUrl())) {
author.setUrl(actor.getUrl());
}
post.setAuthor(author);
}
if (activity.getPublished() != null) {
post.setPublished(new DateTime(activity.getPublished().toEpochMilli()));
}
idempotentExecutor.executeAndSwallowIOExceptions(title, title, () -> getOrCreateBloggerService(authData).posts().insert(blogId, post).setIsDraft(true).execute().getId());
}
Aggregations