Search in sources :

Example 1 with Post

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());
}
Also used : HttpURLConnection(java.net.HttpURLConnection) Blogger(com.google.api.services.blogger.Blogger) ImportResult(org.datatransferproject.spi.transfer.provider.ImportResult) SocialActivityAttachment(org.datatransferproject.types.common.models.social.SocialActivityAttachment) Drive(com.google.api.services.drive.Drive) SocialActivityContainerResource(org.datatransferproject.types.common.models.social.SocialActivityContainerResource) Permission(com.google.api.services.drive.model.Permission) GoogleStaticObjects(org.datatransferproject.datatransfer.google.common.GoogleStaticObjects) Strings(com.google.common.base.Strings) DateTime(com.google.api.client.util.DateTime) ImmutableList(com.google.common.collect.ImmutableList) Post(com.google.api.services.blogger.model.Post) ResultType(org.datatransferproject.spi.transfer.provider.ImportResult.ResultType) BlogList(com.google.api.services.blogger.model.BlogList) File(com.google.api.services.drive.model.File) SocialActivityActor(org.datatransferproject.types.common.models.social.SocialActivityActor) Credential(com.google.api.client.auth.oauth2.Credential) Importer(org.datatransferproject.spi.transfer.provider.Importer) GoogleCredentialFactory(org.datatransferproject.datatransfer.google.common.GoogleCredentialFactory) ImageStreamProvider(org.datatransferproject.transfer.ImageStreamProvider) IdempotentImportExecutor(org.datatransferproject.spi.transfer.idempotentexecutor.IdempotentImportExecutor) Collection(java.util.Collection) SocialActivityType(org.datatransferproject.types.common.models.social.SocialActivityType) IOException(java.io.IOException) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Stream(java.util.stream.Stream) TokensAndUrlAuthData(org.datatransferproject.types.transfer.auth.TokensAndUrlAuthData) InputStreamContent(com.google.api.client.http.InputStreamContent) LocalDate(java.time.LocalDate) SocialActivityAttachmentType(org.datatransferproject.types.common.models.social.SocialActivityAttachmentType) SocialActivityModel(org.datatransferproject.types.common.models.social.SocialActivityModel) InputStream(java.io.InputStream) Post(com.google.api.services.blogger.model.Post) Drive(com.google.api.services.drive.Drive) SocialActivityAttachment(org.datatransferproject.types.common.models.social.SocialActivityAttachment) IOException(java.io.IOException) DateTime(com.google.api.client.util.DateTime)

Aggregations

Credential (com.google.api.client.auth.oauth2.Credential)1 InputStreamContent (com.google.api.client.http.InputStreamContent)1 DateTime (com.google.api.client.util.DateTime)1 Blogger (com.google.api.services.blogger.Blogger)1 BlogList (com.google.api.services.blogger.model.BlogList)1 Post (com.google.api.services.blogger.model.Post)1 Drive (com.google.api.services.drive.Drive)1 File (com.google.api.services.drive.model.File)1 Permission (com.google.api.services.drive.model.Permission)1 Strings (com.google.common.base.Strings)1 ImmutableList (com.google.common.collect.ImmutableList)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 LocalDate (java.time.LocalDate)1 Collection (java.util.Collection)1 UUID (java.util.UUID)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 GoogleCredentialFactory (org.datatransferproject.datatransfer.google.common.GoogleCredentialFactory)1