use of org.datatransferproject.spi.transfer.provider.ExportResult in project data-transfer-project by google.
the class MicrosoftCalendarExporter method export.
@Override
public ExportResult<CalendarContainerResource> export(UUID jobId, TokensAndUrlAuthData authData, Optional<ExportInformation> exportInformation) {
if (exportInformation.isPresent()) {
// TODO support pagination
throw new UnsupportedOperationException();
}
Request.Builder calendarsBuilder = getBuilder(baseUrl + CALENDARS_SUBPATH, authData);
List<CalendarModel> calendarModels = new ArrayList<>();
try (Response graphResponse = client.newCall(calendarsBuilder.build()).execute()) {
ResponseBody body = graphResponse.body();
if (body == null) {
return new ExportResult<>(new Exception("Error retrieving contacts: response body was null"));
}
String graphBody = new String(body.bytes());
Map graphMap = objectMapper.reader().forType(Map.class).readValue(graphBody);
// TODO String nextLink = (String) graphMap.get(ODATA_NEXT);
// TODO ContinuationData continuationData = nextLink == null
// ? null : new ContinuationData(new GraphPagination(nextLink));
@SuppressWarnings("unchecked") List<Map<String, Object>> rawCalendars = (List<Map<String, Object>>) graphMap.get("value");
if (rawCalendars == null) {
return new ExportResult<>(ExportResult.ResultType.END);
}
for (Map<String, Object> rawCalendar : rawCalendars) {
TransformResult<CalendarModel> result = transformerService.transform(CalendarModel.class, rawCalendar);
if (result.hasProblems()) {
// FIXME log problem
continue;
}
calendarModels.add(result.getTransformed());
}
} catch (IOException e) {
// FIXME log error
e.printStackTrace();
return new ExportResult<>(e);
}
List<CalendarEventModel> calendarEventModels = new ArrayList<>();
for (CalendarModel calendarModel : calendarModels) {
String id = calendarModel.getId();
Request.Builder eventsBuilder = getBuilder(calculateEventsUrl(id), authData);
try (Response graphResponse = client.newCall(eventsBuilder.build()).execute()) {
ResponseBody body = graphResponse.body();
if (body == null) {
return new ExportResult<>(new Exception("Error retrieving calendar: response body was null"));
}
String graphBody = new String(body.bytes());
Map graphMap = objectMapper.reader().forType(Map.class).readValue(graphBody);
// TODO String nextLink = (String) graphMap.get(ODATA_NEXT);
// TODO ContinuationData continuationData = nextLink == null
// ? null : new ContinuationData(new GraphPagination(nextLink));
@SuppressWarnings("unchecked") List<Map<String, Object>> rawEvents = (List<Map<String, Object>>) graphMap.get("value");
if (rawEvents == null) {
return new ExportResult<>(ExportResult.ResultType.END);
}
for (Map<String, Object> rawEvent : rawEvents) {
Map<String, String> properties = new HashMap<>();
properties.put(CALENDAR_ID, id);
TransformResult<CalendarEventModel> result = transformerService.transform(CalendarEventModel.class, rawEvent, properties);
if (result.hasProblems()) {
// FIXME log problem
continue;
}
calendarEventModels.add(result.getTransformed());
}
} catch (IOException e) {
// FIXME log error
e.printStackTrace();
return new ExportResult<>(e);
}
}
CalendarContainerResource resource = new CalendarContainerResource(calendarModels, calendarEventModels);
return new ExportResult<>(ExportResult.ResultType.END, resource, null);
}
use of org.datatransferproject.spi.transfer.provider.ExportResult in project data-transfer-project by google.
the class MicrosoftContactsExporter method doExport.
@SuppressWarnings("unchecked")
private ExportResult<ContactsModelWrapper> doExport(TokensAndUrlAuthData authData, String url) {
Request.Builder graphReqBuilder = new Request.Builder().url(url);
graphReqBuilder.header("Authorization", "Bearer " + authData.getAccessToken());
try (Response graphResponse = client.newCall(graphReqBuilder.build()).execute()) {
ResponseBody body = graphResponse.body();
if (body == null) {
return new ExportResult<>(new Exception("Error retrieving contacts: response body was null"));
}
String graphBody = new String(body.bytes());
Map graphMap = objectMapper.reader().forType(Map.class).readValue(graphBody);
String nextLink = (String) graphMap.get(ODATA_NEXT);
ContinuationData continuationData = nextLink == null ? null : new ContinuationData(new GraphPagination(nextLink));
List<Map<String, Object>> rawContacts = (List<Map<String, Object>>) graphMap.get("value");
if (rawContacts == null) {
return new ExportResult<>(ExportResult.ResultType.END);
}
ContactsModelWrapper wrapper = transform(rawContacts);
return new ExportResult<>(ExportResult.ResultType.CONTINUE, wrapper, continuationData);
} catch (IOException e) {
// FIXME log error
e.printStackTrace();
return new ExportResult<>(e);
}
}
use of org.datatransferproject.spi.transfer.provider.ExportResult 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);
}
use of org.datatransferproject.spi.transfer.provider.ExportResult in project data-transfer-project by google.
the class ImgurPhotosExporter method requestAlbums.
/**
* Exports albums.
*
* @param authData authentication information
* @param paginationData pagination information to use for subsequent calls
*/
private ExportResult<PhotosContainerResource> requestAlbums(TokensAndUrlAuthData authData, PaginationData paginationData) throws IOException {
ImmutableList.Builder<PhotoAlbum> albumBuilder = ImmutableList.builder();
List<IdOnlyContainerResource> albumIds = new ArrayList<>();
int page = paginationData == null ? 0 : ((IntPaginationToken) paginationData).getStart();
String url = format(ALBUMS_URL_TEMPLATE, page);
List<Map<String, Object>> items = requestData(authData, url);
// Request result doesn't indicate if it's the last page
boolean hasMore = (items != null && items.size() != 0);
for (Map<String, Object> item : items) {
albumBuilder.add(new PhotoAlbum((String) item.get("id"), (String) item.get("title"), (String) item.get("description")));
// Save album id for recalling export to get all the photos in albums
albumIds.add(new IdOnlyContainerResource((String) item.get("id")));
}
if (page == 0) {
// For checking non-album photos. Their export should be performed after all the others
// Album will be created later
albumIds.add(new IdOnlyContainerResource(DEFAULT_ALBUM_ID));
}
PaginationData newPage = null;
if (hasMore) {
newPage = new IntPaginationToken(page + 1);
int start = ((IntPaginationToken) newPage).getStart();
monitor.info(() -> format("albums size: %s, newPage: %s", items.size(), start));
}
PhotosContainerResource photosContainerResource = new PhotosContainerResource(albumBuilder.build(), null);
ContinuationData continuationData = new ContinuationData(newPage);
albumIds.forEach(continuationData::addContainerResource);
ExportResult.ResultType resultType = ExportResult.ResultType.CONTINUE;
if (newPage == null) {
resultType = ExportResult.ResultType.END;
}
return new ExportResult<>(resultType, photosContainerResource, continuationData);
}
use of org.datatransferproject.spi.transfer.provider.ExportResult in project data-transfer-project by google.
the class InstagramPhotoExporter method exportPhotos.
private ExportResult<PhotosContainerResource> exportPhotos(TokensAndUrlAuthData authData, Optional<PaginationData> pageData) {
Preconditions.checkNotNull(authData);
MediaResponse response;
try {
response = makeRequest(MEDIA_URL, MediaResponse.class, authData);
} catch (IOException e) {
return new ExportResult<>(e);
}
List<PhotoModel> photos = new ArrayList<>();
// TODO: check out paging.
for (MediaFeedData photo : response.getData()) {
// TODO json mapping is broken.
String photoId = photo.getId();
String url = photo.getImages().getStandardResolution().getUrl();
String text = (photo.getCaption() != null) ? photo.getCaption().getText() : null;
photos.add(new PhotoModel("Instagram photo: " + photoId, url, text, null, photoId, FAKE_ALBUM_ID, false));
}
List<PhotoAlbum> albums = new ArrayList<>();
if (!photos.isEmpty() && !pageData.isPresent()) {
albums.add(new PhotoAlbum(FAKE_ALBUM_ID, "Imported Instagram Photos", "Photos imported from instagram"));
}
return new ExportResult<>(ResultType.END, new PhotosContainerResource(albums, photos));
}
Aggregations