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