Search in sources :

Example 6 with SocialActivityModel

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

the class MastodonActivityExport method statusToActivity.

private SocialActivityModel statusToActivity(Account account, Status status, MastodonHttpUtilities utilities) {
    String contentString = status.getContent();
    Matcher matcher = RAW_CONTENT_PATTERN.matcher(contentString);
    if (matcher.matches()) {
        contentString = matcher.group(1);
    }
    return new SocialActivityModel(status.getUri(), status.getCreatedAt(), SocialActivityType.NOTE, null, null, null, contentString, status.getUrl());
}
Also used : Matcher(java.util.regex.Matcher) SocialActivityModel(org.datatransferproject.types.common.models.social.SocialActivityModel)

Example 7 with SocialActivityModel

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

the class GoogleBloggerImporter method importItem.

@Override
public ImportResult importItem(UUID jobId, IdempotentImportExecutor idempotentExecutor, TokensAndUrlAuthData authData, SocialActivityContainerResource data) throws Exception {
    Blogger blogger = getOrCreateBloggerService(authData);
    BlogList blogList = blogger.blogs().listByUser("self").execute();
    // NB: we are just publishing everything to the first blog, which is a bit of a hack,
    // but there is no API to create a new blog.
    String blogId = blogList.getItems().get(0).getId();
    for (SocialActivityModel activity : data.getActivities()) {
        if (activity.getType() == SocialActivityType.NOTE || activity.getType() == SocialActivityType.POST) {
            try {
                insertActivity(idempotentExecutor, data.getActor(), activity, blogId, authData);
            } catch (IOException | RuntimeException e) {
                throw new IOException("Couldn't import: " + activity, e);
            }
        }
    }
    return new ImportResult(ResultType.OK);
}
Also used : Blogger(com.google.api.services.blogger.Blogger) ImportResult(org.datatransferproject.spi.transfer.provider.ImportResult) BlogList(com.google.api.services.blogger.model.BlogList) SocialActivityModel(org.datatransferproject.types.common.models.social.SocialActivityModel) IOException(java.io.IOException)

Example 8 with SocialActivityModel

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

the class DaybookPostsImporter method insertActivity.

private String insertActivity(IdempotentImportExecutor executor, SocialActivityModel activity, TokensAndUrlAuthData authData) throws IOException {
    Map<String, String> imageMap = new HashMap<>();
    Map<String, String> linkMap = new HashMap<>();
    String content = activity.getContent() == null ? "" : activity.getContent();
    String title = activity.getTitle() == null ? "" : activity.getTitle();
    String location = activity.getLocation() == null || activity.getLocation().getName() == null ? "" : activity.getLocation().getName();
    String published = activity.getPublished().toString() == null ? "" : activity.getPublished().toString();
    Request.Builder requestBuilder = new Request.Builder().url(baseUrl);
    requestBuilder.header("token", authData.getAccessToken());
    FormBody.Builder builder = new FormBody.Builder().add("type", "POSTS");
    builder.add("exporter", JobMetadata.getExportService());
    builder.add("content", content);
    builder.add("title", title);
    builder.add("location", location);
    builder.add("published", published);
    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.
    if (!linkAttachments.isEmpty()) {
        for (SocialActivityAttachment attachment : linkAttachments) {
            linkMap.put(attachment.getName(), attachment.getUrl());
        }
        try {
            String json = objectMapper.writeValueAsString(linkMap);
            builder.add("link", json);
        } catch (JsonProcessingException e) {
            monitor.info(() -> String.format("Error processing JSON: %s", e.getMessage()));
        }
    }
    if (!imageAttachments.isEmpty()) {
        for (SocialActivityAttachment image : imageAttachments) {
            imageMap.put(image.getName() != null ? image.getName() : image.getUrl(), image.getUrl());
        }
        try {
            String json = objectMapper.writeValueAsString(imageMap);
            builder.add("image", json);
        } catch (JsonProcessingException e) {
            monitor.info(() -> String.format("Error processing JSON: %s", e.getMessage()));
        }
    }
    FormBody formBody = builder.build();
    requestBuilder.post(formBody);
    try (Response response = client.newCall(requestBuilder.build()).execute()) {
        int code = response.code();
        // Though sometimes it returns error code for success requests
        if (code < 200 || code > 299) {
            throw new IOException(String.format("Error occurred in request for adding entry, message: %s", response.message()));
        }
        return response.message();
    }
}
Also used : IdempotentImportExecutor(org.datatransferproject.spi.transfer.idempotentexecutor.IdempotentImportExecutor) ImportResult(org.datatransferproject.spi.transfer.provider.ImportResult) SocialActivityAttachment(org.datatransferproject.types.common.models.social.SocialActivityAttachment) Collection(java.util.Collection) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SocialActivityContainerResource(org.datatransferproject.types.common.models.social.SocialActivityContainerResource) SocialActivityType(org.datatransferproject.types.common.models.social.SocialActivityType) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) HashMap(java.util.HashMap) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Monitor(org.datatransferproject.api.launcher.Monitor) TokensAndUrlAuthData(org.datatransferproject.types.transfer.auth.TokensAndUrlAuthData) okhttp3(okhttp3) JobMetadata(org.datatransferproject.transfer.JobMetadata) Map(java.util.Map) SocialActivityAttachmentType(org.datatransferproject.types.common.models.social.SocialActivityAttachmentType) Importer(org.datatransferproject.spi.transfer.provider.Importer) SocialActivityModel(org.datatransferproject.types.common.models.social.SocialActivityModel) HashMap(java.util.HashMap) SocialActivityAttachment(org.datatransferproject.types.common.models.social.SocialActivityAttachment) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

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