use of org.datatransferproject.spi.transfer.provider.ExportResult.ResultType in project data-transfer-project by google.
the class GooglePhotosExporter method exportAlbums.
/**
* Note: not all accounts have albums to return. In that case, we just return an empty list of
* albums instead of trying to iterate through a null list.
*/
@VisibleForTesting
ExportResult<PhotosContainerResource> exportAlbums(TokensAndUrlAuthData authData, Optional<PaginationData> paginationData, UUID jobId) throws IOException, InvalidTokenException, PermissionDeniedException {
Optional<String> paginationToken = Optional.empty();
if (paginationData.isPresent()) {
String token = ((StringPaginationToken) paginationData.get()).getToken();
Preconditions.checkArgument(token.startsWith(ALBUM_TOKEN_PREFIX), "Invalid pagination token " + token);
paginationToken = Optional.of(token.substring(ALBUM_TOKEN_PREFIX.length()));
}
AlbumListResponse albumListResponse;
albumListResponse = getOrCreatePhotosInterface(authData).listAlbums(paginationToken);
PaginationData nextPageData;
String token = albumListResponse.getNextPageToken();
List<PhotoAlbum> albums = new ArrayList<>();
GoogleAlbum[] googleAlbums = albumListResponse.getAlbums();
if (Strings.isNullOrEmpty(token)) {
nextPageData = new StringPaginationToken(PHOTO_TOKEN_PREFIX);
} else {
nextPageData = new StringPaginationToken(ALBUM_TOKEN_PREFIX + token);
}
ContinuationData continuationData = new ContinuationData(nextPageData);
if (googleAlbums != null && googleAlbums.length > 0) {
for (GoogleAlbum googleAlbum : googleAlbums) {
// Add album info to list so album can be recreated later
PhotoAlbum photoAlbum = new PhotoAlbum(googleAlbum.getId(), googleAlbum.getTitle(), null);
albums.add(photoAlbum);
monitor.debug(() -> String.format("%s: Google Photos exporting album: %s", jobId, photoAlbum.getId()));
// Add album id to continuation data
continuationData.addContainerResource(new IdOnlyContainerResource(googleAlbum.getId()));
}
}
ResultType resultType = ResultType.CONTINUE;
PhotosContainerResource containerResource = new PhotosContainerResource(albums, null);
return new ExportResult<>(resultType, containerResource, continuationData);
}
use of org.datatransferproject.spi.transfer.provider.ExportResult.ResultType in project data-transfer-project by google.
the class GoogleVideosExporter method exportVideos.
@VisibleForTesting
ExportResult<VideosContainerResource> exportVideos(TokensAndUrlAuthData authData, Optional<StringPaginationToken> paginationData) throws IOException {
Optional<String> paginationToken = paginationData.map(StringPaginationToken::getToken);
MediaItemSearchResponse mediaItemSearchResponse = getOrCreateVideosInterface(authData).listVideoItems(paginationToken);
PaginationData nextPageData = null;
if (!Strings.isNullOrEmpty(mediaItemSearchResponse.getNextPageToken())) {
nextPageData = new StringPaginationToken(mediaItemSearchResponse.getNextPageToken());
}
ContinuationData continuationData = new ContinuationData(nextPageData);
VideosContainerResource containerResource = null;
GoogleMediaItem[] mediaItems = mediaItemSearchResponse.getMediaItems();
if (mediaItems != null && mediaItems.length > 0) {
List<VideoModel> videos = convertVideosList(mediaItems);
containerResource = new VideosContainerResource(null, videos);
}
ResultType resultType = ResultType.CONTINUE;
if (nextPageData == null) {
resultType = ResultType.END;
}
return new ExportResult<>(resultType, containerResource, continuationData);
}
use of org.datatransferproject.spi.transfer.provider.ExportResult.ResultType in project data-transfer-project by google.
the class SmugMugPhotosExporter method exportAlbums.
private ExportResult<PhotosContainerResource> exportAlbums(StringPaginationToken paginationData, SmugMugInterface smugMugInterface) throws IOException {
SmugMugAlbumsResponse albumsResponse;
try {
// Make request to SmugMug
String albumInfoUri = "";
if (paginationData != null) {
String pageToken = paginationData.getToken();
Preconditions.checkState(pageToken.startsWith(ALBUM_TOKEN_PREFIX), "Invalid pagination token " + pageToken);
albumInfoUri = pageToken.substring(ALBUM_TOKEN_PREFIX.length());
}
albumsResponse = smugMugInterface.getAlbums(albumInfoUri);
} catch (IOException e) {
monitor.severe(() -> "Unable to get AlbumsResponse: ", e);
throw e;
}
// Set up continuation data
StringPaginationToken paginationToken = null;
if (albumsResponse.getPageInfo() != null && albumsResponse.getPageInfo().getNextPage() != null) {
paginationToken = new StringPaginationToken(ALBUM_TOKEN_PREFIX + albumsResponse.getPageInfo().getNextPage());
}
ContinuationData continuationData = new ContinuationData(paginationToken);
// Build album list
List<PhotoAlbum> albumsList = new ArrayList<>();
if (albumsResponse.getAlbums() != null) {
for (SmugMugAlbum album : albumsResponse.getAlbums()) {
albumsList.add(new PhotoAlbum(album.getUri(), album.getName(), album.getDescription()));
continuationData.addContainerResource(new IdOnlyContainerResource(album.getUri()));
}
}
PhotosContainerResource resource = new PhotosContainerResource(albumsList, null);
// Get result type
ResultType resultType = ResultType.CONTINUE;
if (paginationToken == null) {
resultType = ResultType.END;
}
return new ExportResult<>(resultType, resource, continuationData);
}
use of org.datatransferproject.spi.transfer.provider.ExportResult.ResultType in project data-transfer-project by google.
the class SmugMugPhotosExporter method exportPhotos.
private ExportResult<PhotosContainerResource> exportPhotos(IdOnlyContainerResource containerResource, StringPaginationToken paginationData, SmugMugInterface smugMugInterface, UUID jobId) throws IOException {
List<PhotoModel> photoList = new ArrayList<>();
// Make request to SmugMug
String photoInfoUri;
if (paginationData != null) {
String token = paginationData.getToken();
Preconditions.checkState(token.startsWith(PHOTO_TOKEN_PREFIX), "Invalid pagination token " + token);
photoInfoUri = token.substring(PHOTO_TOKEN_PREFIX.length());
} else {
photoInfoUri = containerResource.getId();
}
SmugMugAlbumImageResponse albumImageList;
try {
albumImageList = smugMugInterface.getListOfAlbumImages(photoInfoUri + "!images");
} catch (IOException e) {
monitor.severe(() -> "Unable to get SmugMugAlbumImageResponse");
throw e;
}
// Set up continuation data
StringPaginationToken pageToken = null;
if (albumImageList.getPageInfo().getNextPage() != null) {
pageToken = new StringPaginationToken(PHOTO_TOKEN_PREFIX + albumImageList.getPageInfo().getNextPage());
}
ContinuationData continuationData = new ContinuationData(pageToken);
// Make list of photos - images may be empty if the album provided is empty
List<SmugMugImage> images = albumImageList.getAlbumImages() == null ? ImmutableList.of() : albumImageList.getAlbumImages();
for (SmugMugImage albumImage : images) {
if (!albumImage.isPhoto()) {
continue;
}
String title = albumImage.getTitle();
if (Strings.isNullOrEmpty(title)) {
title = albumImage.getFileName();
}
PhotoModel model = new PhotoModel(title, albumImage.getArchivedUri(), albumImage.getCaption(), getMimeType(albumImage.getFormat()), albumImage.getArchivedUri(), containerResource.getId(), true);
InputStream inputStream = smugMugInterface.getImageAsStream(model.getFetchableUrl());
jobStore.create(jobId, model.getFetchableUrl(), inputStream);
photoList.add(model);
}
PhotosContainerResource resource = new PhotosContainerResource(null, photoList);
// Get result type
ResultType resultType = ResultType.CONTINUE;
if (pageToken == null) {
resultType = ResultType.END;
}
return new ExportResult<>(resultType, resource, continuationData);
}
use of org.datatransferproject.spi.transfer.provider.ExportResult.ResultType in project data-transfer-project by google.
the class GoogleCalendarExporter method getCalendarEvents.
private ExportResult<CalendarContainerResource> getCalendarEvents(TokensAndUrlAuthData authData, String id, Optional<PaginationData> pageData) {
Calendar.Events.List listRequest;
Events listResult;
// Get event information
try {
listRequest = getOrCreateCalendarInterface(authData).events().list(id).setMaxAttendees(GoogleStaticObjects.MAX_ATTENDEES);
if (pageData.isPresent()) {
StringPaginationToken paginationToken = (StringPaginationToken) pageData.get();
Preconditions.checkState(paginationToken.getToken().startsWith(EVENT_TOKEN_PREFIX), "Token is not applicable");
listRequest.setPageToken(((StringPaginationToken) pageData.get()).getToken().substring(EVENT_TOKEN_PREFIX.length()));
}
listResult = listRequest.execute();
} catch (IOException e) {
return new ExportResult<>(e);
}
// Set up continuation data
PaginationData nextPageData = null;
if (listResult.getNextPageToken() != null) {
nextPageData = new StringPaginationToken(EVENT_TOKEN_PREFIX + listResult.getNextPageToken());
}
ContinuationData continuationData = new ContinuationData(nextPageData);
// Process event list
List<CalendarEventModel> eventModels = new ArrayList<>(listResult.getItems().size());
for (Event eventData : listResult.getItems()) {
CalendarEventModel model = convertToCalendarEventModel(id, eventData);
eventModels.add(model);
}
CalendarContainerResource calendarContainerResource = new CalendarContainerResource(null, eventModels);
// Get result type
ExportResult.ResultType resultType = ResultType.CONTINUE;
if (nextPageData == null) {
resultType = ResultType.END;
}
return new ExportResult<>(resultType, calendarContainerResource, continuationData);
}
Aggregations