Search in sources :

Example 1 with SocialActivityModel

use of org.datatransferproject.types.common.models.social.SocialActivityModel 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)

Example 2 with SocialActivityModel

use of org.datatransferproject.types.common.models.social.SocialActivityModel in project data-transfer-project by google.

the class GooglePlusExporter method export.

@Override
public ExportResult<SocialActivityContainerResource> export(UUID jobId, TokensAndUrlAuthData authData, Optional<ExportInformation> exportInformation) throws IOException {
    Plus plus = getOrCreatePeopleService(authData);
    Plus.Activities.List listActivities = plus.activities().list("me", "public");
    if (exportInformation.isPresent()) {
        StringPaginationToken pageToken = (StringPaginationToken) exportInformation.get().getPaginationData();
        listActivities.setPageToken(pageToken.getToken());
    }
    ActivityFeed activityFeed = listActivities.execute();
    List<Activity> activities = activityFeed.getItems();
    ContinuationData continuationData = null;
    SocialActivityContainerResource results = null;
    if (activities != null && !activities.isEmpty()) {
        List<SocialActivityModel> activityModels = new ArrayList<>();
        Activity.Actor actor = activities.get(0).getActor();
        SocialActivityActor parsedActor = new SocialActivityActor(actor.getUrl(), actor.getDisplayName(), actor.getUrl());
        if (!Strings.isNullOrEmpty(activityFeed.getNextPageToken())) {
            continuationData = new ContinuationData(new StringPaginationToken(activityFeed.getNextPageToken()));
        }
        for (Activity activity : activities) {
            try {
                activityModels.add(postToActivityModel(activity));
            } catch (RuntimeException e) {
                throw new IOException("Problem exporting: " + activity, e);
            }
        }
        results = new SocialActivityContainerResource(jobId.toString(), parsedActor, activityModels);
    }
    return new ExportResult<>(continuationData == null ? ResultType.END : ResultType.CONTINUE, results, continuationData);
}
Also used : SocialActivityContainerResource(org.datatransferproject.types.common.models.social.SocialActivityContainerResource) ArrayList(java.util.ArrayList) Activity(com.google.api.services.plus.model.Activity) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) IOException(java.io.IOException) ActivityFeed(com.google.api.services.plus.model.ActivityFeed) SocialActivityModel(org.datatransferproject.types.common.models.social.SocialActivityModel) Plus(com.google.api.services.plus.Plus) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken) SocialActivityActor(org.datatransferproject.types.common.models.social.SocialActivityActor) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult)

Example 3 with SocialActivityModel

use of org.datatransferproject.types.common.models.social.SocialActivityModel in project data-transfer-project by google.

the class GooglePlusExporter method postToActivityModel.

private SocialActivityModel postToActivityModel(Activity activity) {
    String contentString = activity.getObject().getOriginalContent();
    List<SocialActivityAttachment> activityAttachments = new ArrayList<>();
    switch(activity.getVerb()) {
        case "post":
            for (Attachments attachment : activity.getObject().getAttachments()) {
                if (attachment.getObjectType().equals("article")) {
                    activityAttachments.add(new SocialActivityAttachment(SocialActivityAttachmentType.LINK, attachment.getUrl(), attachment.getDisplayName(), attachment.getContent()));
                } else if (attachment.getObjectType().equals("photo")) {
                    activityAttachments.add(new SocialActivityAttachment(SocialActivityAttachmentType.IMAGE, attachment.getFullImage().getUrl(), attachment.getDisplayName(), attachment.getContent()));
                } else if (attachment.getObjectType().equals("album")) {
                    // all the images.
                    for (Thumbnails image : attachment.getThumbnails()) {
                        activityAttachments.add(new SocialActivityAttachment(SocialActivityAttachmentType.IMAGE, // This is just a thumbnail image
                        image.getImage().getUrl(), image.getDescription(), // but instead a hosted page of the image.
                        "Original G+ Image: " + image.getUrl() + " from album: " + attachment.getUrl()));
                    }
                } else {
                    throw new IllegalArgumentException("Don't know how to export attachment " + attachment.getObjectType());
                }
            }
            return new SocialActivityModel(activity.getId(), Instant.ofEpochMilli(activity.getPublished().getValue()), SocialActivityType.POST, activityAttachments, null, activity.getTitle(), contentString, activity.getUrl());
        case "checkin":
            Place location = activity.getLocation();
            return new SocialActivityModel(activity.getId(), Instant.ofEpochMilli(activity.getPublished().getValue()), SocialActivityType.CHECKIN, null, new SocialActivityLocation(location.getDisplayName(), location.getPosition().getLongitude(), location.getPosition().getLatitude()), activity.getPlaceName(), contentString, null);
        default:
            throw new IllegalArgumentException("Don't know how to export " + activity);
    }
}
Also used : SocialActivityLocation(org.datatransferproject.types.common.models.social.SocialActivityLocation) ArrayList(java.util.ArrayList) SocialActivityModel(org.datatransferproject.types.common.models.social.SocialActivityModel) SocialActivityAttachment(org.datatransferproject.types.common.models.social.SocialActivityAttachment) Thumbnails(com.google.api.services.plus.model.Activity.PlusObject.Attachments.Thumbnails) Attachments(com.google.api.services.plus.model.Activity.PlusObject.Attachments) Place(com.google.api.services.plus.model.Place)

Example 4 with SocialActivityModel

use of org.datatransferproject.types.common.models.social.SocialActivityModel in project data-transfer-project by google.

the class MastodonActivityExport method export.

@Override
public ExportResult<SocialActivityContainerResource> export(UUID jobId, CookiesAndUrlAuthData authData, Optional<ExportInformation> exportInformation) throws Exception {
    checkState(authData.getCookies().size() == 1, "Exactly 1 cookie expected: %s", authData.getCookies());
    String maxId = null;
    if (exportInformation.isPresent()) {
        StringPaginationToken pageData = (StringPaginationToken) exportInformation.get().getPaginationData();
        if (!Strings.isNullOrEmpty(pageData.getToken())) {
            maxId = pageData.getToken();
        }
    }
    MastodonHttpUtilities utilities = new MastodonHttpUtilities(authData.getCookies().get(0), authData.getUrl());
    Account account = utilities.getAccount();
    Status[] statuses = utilities.getStatuses(maxId);
    List<SocialActivityModel> activityList = new ArrayList<>(statuses.length);
    SocialActivityActor actor = new SocialActivityActor("acct:" + account.getUsername() + "@" + utilities.getHostName(), account.getDisplayName(), account.getUrl());
    ContinuationData continuationData = null;
    if (statuses.length > 0) {
        String lastId = null;
        for (Status status : statuses) {
            activityList.add(statusToActivity(account, status, utilities));
            lastId = status.getId();
        }
        continuationData = new ContinuationData(new StringPaginationToken(lastId));
    }
    return new ExportResult<>(continuationData == null ? ResultType.END : ResultType.CONTINUE, new SocialActivityContainerResource(account.getId() + maxId, actor, activityList), continuationData);
}
Also used : Status(org.datatransferproject.transfer.mastodon.model.Status) Account(org.datatransferproject.transfer.mastodon.model.Account) SocialActivityContainerResource(org.datatransferproject.types.common.models.social.SocialActivityContainerResource) ArrayList(java.util.ArrayList) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) SocialActivityModel(org.datatransferproject.types.common.models.social.SocialActivityModel) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken) SocialActivityActor(org.datatransferproject.types.common.models.social.SocialActivityActor) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult)

Example 5 with SocialActivityModel

use of org.datatransferproject.types.common.models.social.SocialActivityModel in project data-transfer-project by google.

the class MastodonActivityImport method importItem.

@Override
public ImportResult importItem(UUID jobId, IdempotentImportExecutor idempotentImportExecutor, CookiesAndUrlAuthData authData, SocialActivityContainerResource data) throws Exception {
    checkState(authData.getCookies().size() == 1, "Exactly 1 cookie expected: %s", authData.getCookies());
    MastodonHttpUtilities utilities = new MastodonHttpUtilities(authData.getCookies().get(0), authData.getUrl());
    for (SocialActivityModel activity : data.getActivities()) {
        if (activity.getType() == SocialActivityType.NOTE) {
            idempotentImportExecutor.executeAndSwallowIOExceptions(activity.getId(), activity.getContent(), () -> {
                postNode(activity, utilities, jobId);
                return 1;
            });
        }
    }
    return ImportResult.OK;
}
Also used : SocialActivityModel(org.datatransferproject.types.common.models.social.SocialActivityModel)

Aggregations

SocialActivityModel (org.datatransferproject.types.common.models.social.SocialActivityModel)8 IOException (java.io.IOException)4 SocialActivityContainerResource (org.datatransferproject.types.common.models.social.SocialActivityContainerResource)4 ArrayList (java.util.ArrayList)3 ImportResult (org.datatransferproject.spi.transfer.provider.ImportResult)3 SocialActivityActor (org.datatransferproject.types.common.models.social.SocialActivityActor)3 SocialActivityAttachment (org.datatransferproject.types.common.models.social.SocialActivityAttachment)3 Blogger (com.google.api.services.blogger.Blogger)2 BlogList (com.google.api.services.blogger.model.BlogList)2 Collection (java.util.Collection)2 UUID (java.util.UUID)2 Collectors (java.util.stream.Collectors)2 IdempotentImportExecutor (org.datatransferproject.spi.transfer.idempotentexecutor.IdempotentImportExecutor)2 ExportResult (org.datatransferproject.spi.transfer.provider.ExportResult)2 Importer (org.datatransferproject.spi.transfer.provider.Importer)2 ContinuationData (org.datatransferproject.spi.transfer.types.ContinuationData)2 StringPaginationToken (org.datatransferproject.types.common.StringPaginationToken)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Credential (com.google.api.client.auth.oauth2.Credential)1