use of org.datatransferproject.types.common.models.social.SocialActivityAttachment 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());
}
use of org.datatransferproject.types.common.models.social.SocialActivityAttachment 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);
}
}
use of org.datatransferproject.types.common.models.social.SocialActivityAttachment 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();
}
}
Aggregations