use of org.datatransferproject.types.common.models.photos.PhotoAlbum 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.types.common.models.photos.PhotoAlbum in project data-transfer-project by google.
the class FlickrPhotosExporterTest method exportAlbumInitial.
@Test
public void exportAlbumInitial() throws FlickrException {
// set up auth, flickr service
when(user.getId()).thenReturn("userId");
when(authInterface.checkToken(any(Token.class))).thenReturn(auth);
when(flickr.getPhotosetsInterface()).thenReturn(photosetsInterface);
when(flickr.getPhotosInterface()).thenReturn(photosInterface);
when(flickr.getAuthInterface()).thenReturn(authInterface);
// setup photoset
Photoset photoset = FlickrTestUtils.initializePhotoset("photosetId", "title", "description");
// setup photoset list (aka album view)
int page = 1;
Photosets photosetsList = new Photosets();
photosetsList.setPage(page);
photosetsList.setPages(page + 1);
photosetsList.setPhotosets(Collections.singletonList(photoset));
when(photosetsInterface.getList(anyString(), anyInt(), anyInt(), anyString())).thenReturn(photosetsList);
// run test
FlickrPhotosExporter exporter = new FlickrPhotosExporter(flickr, TransferServiceConfig.getDefaultInstance());
AuthData authData = new TokenSecretAuthData("token", "secret");
ExportResult<PhotosContainerResource> result = exporter.export(UUID.randomUUID(), authData, Optional.empty());
// make sure album and photo information is correct
assertThat(result.getExportedData().getPhotos()).isEmpty();
Collection<PhotoAlbum> albums = result.getExportedData().getAlbums();
assertThat(albums.size()).isEqualTo(1);
assertThat(albums).containsExactly(new PhotoAlbum("photosetId", "title", "description"));
// check continuation information
ContinuationData continuationData = (ContinuationData) result.getContinuationData();
assertThat(continuationData.getPaginationData()).isInstanceOf(IntPaginationToken.class);
assertThat(((IntPaginationToken) continuationData.getPaginationData()).getStart()).isEqualTo(page + 1);
Collection<? extends ContainerResource> subResources = continuationData.getContainerResources();
assertThat(subResources.size()).isEqualTo(1);
assertThat(subResources).containsExactly(new IdOnlyContainerResource("photosetId"));
}
use of org.datatransferproject.types.common.models.photos.PhotoAlbum in project data-transfer-project by google.
the class FlickrPhotosExporter method getAlbums.
private ExportResult<PhotosContainerResource> getAlbums(PaginationData paginationData, Auth auth) {
ImmutableList.Builder<PhotoAlbum> albumBuilder = ImmutableList.builder();
List<IdOnlyContainerResource> subResources = new ArrayList<>();
int page = paginationData == null ? 1 : ((IntPaginationToken) paginationData).getStart();
Photosets photoSetList;
try {
perUserRateLimiter.acquire();
photoSetList = photosetsInterface.getList(auth.getUser().getId(), PHOTO_SETS_PER_PAGE, page, PHOTOSET_EXTRAS);
} catch (FlickrException e) {
return new ExportResult<>(e);
}
for (Photoset photoSet : photoSetList.getPhotosets()) {
// Saving data to the album allows the target service to recreate the album structure
albumBuilder.add(new PhotoAlbum(photoSet.getId(), photoSet.getTitle(), photoSet.getDescription()));
// Adding subresources tells the framework to recall export to get all the photos
subResources.add(new IdOnlyContainerResource(photoSet.getId()));
}
PaginationData newPage = null;
boolean hasMore = photoSetList.getPage() != photoSetList.getPages() && !photoSetList.getPhotosets().isEmpty();
if (hasMore) {
newPage = new IntPaginationToken(page + 1);
} else {
// No more albums to get, add a resource for albumless items
subResources.add(new IdOnlyContainerResource(""));
}
PhotosContainerResource photosContainerResource = new PhotosContainerResource(albumBuilder.build(), null);
ContinuationData continuationData = new ContinuationData(newPage);
subResources.forEach(resource -> continuationData.addContainerResource(resource));
// Get result type
ResultType resultType = ResultType.CONTINUE;
if (newPage == null) {
resultType = ResultType.END;
}
return new ExportResult<>(resultType, photosContainerResource, continuationData);
}
use of org.datatransferproject.types.common.models.photos.PhotoAlbum in project data-transfer-project by google.
the class GooglePhotosExporter method exportPhotosContainer.
private ExportResult<PhotosContainerResource> exportPhotosContainer(PhotosContainerResource container, TokensAndUrlAuthData authData) throws IOException, InvalidTokenException, PermissionDeniedException {
ImmutableList.Builder<PhotoAlbum> albumBuilder = ImmutableList.builder();
ImmutableList.Builder<PhotoModel> photosBuilder = ImmutableList.builder();
List<IdOnlyContainerResource> subResources = new ArrayList<>();
for (PhotoAlbum album : container.getAlbums()) {
GoogleAlbum googleAlbum = getOrCreatePhotosInterface(authData).getAlbum(album.getId());
albumBuilder.add(new PhotoAlbum(googleAlbum.getId(), googleAlbum.getTitle(), null));
// Adding subresources tells the framework to recall export to get all the photos
subResources.add(new IdOnlyContainerResource(googleAlbum.getId()));
}
for (PhotoModel photo : container.getPhotos()) {
GoogleMediaItem googleMediaItem = getOrCreatePhotosInterface(authData).getMediaItem(photo.getDataId());
photosBuilder.add(convertToPhotoModel(Optional.empty(), googleMediaItem));
}
PhotosContainerResource photosContainerResource = new PhotosContainerResource(albumBuilder.build(), photosBuilder.build());
ContinuationData continuationData = new ContinuationData(null);
subResources.forEach(resource -> continuationData.addContainerResource(resource));
return new ExportResult<>(ResultType.CONTINUE, photosContainerResource, continuationData);
}
use of org.datatransferproject.types.common.models.photos.PhotoAlbum in project data-transfer-project by google.
the class GooglePhotosImporter method importItem.
@Override
public ImportResult importItem(UUID jobId, IdempotentImportExecutor idempotentImportExecutor, TokensAndUrlAuthData authData, PhotosContainerResource data) throws Exception {
if (data == null) {
// Nothing to do
return ImportResult.OK;
}
// Uploads album metadata
if (data.getAlbums() != null && data.getAlbums().size() > 0) {
for (PhotoAlbum album : data.getAlbums()) {
idempotentImportExecutor.executeAndSwallowIOExceptions(album.getId(), album.getName(), () -> importSingleAlbum(jobId, authData, album));
}
}
long bytes = importPhotos(data.getPhotos(), idempotentImportExecutor, jobId, authData);
final ImportResult result = ImportResult.OK;
return result.copyWithBytes(bytes);
}
Aggregations