use of org.datatransferproject.types.common.models.IdOnlyContainerResource 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.IdOnlyContainerResource in project data-transfer-project by google.
the class GooglePhotosExporter method export.
@Override
public ExportResult<PhotosContainerResource> export(UUID jobId, TokensAndUrlAuthData authData, Optional<ExportInformation> exportInformation) throws IOException, InvalidTokenException, PermissionDeniedException {
if (!exportInformation.isPresent()) {
// Make list of photos contained in albums so they are not exported twice later on
populateContainedPhotosList(jobId, authData);
return exportAlbums(authData, Optional.empty(), jobId);
} else if (exportInformation.get().getContainerResource() instanceof PhotosContainerResource) {
// in that container instead of the whole user library
return exportPhotosContainer((PhotosContainerResource) exportInformation.get().getContainerResource(), authData);
}
/*
* Use the export information to determine whether this export call should export albums or
* photos.
*
* Albums are exported if and only if the export information doesn't hold an album
* already, and the pagination token begins with the album prefix. There must be a pagination
* token for album export since this is isn't the first export operation performed (if it was,
* there wouldn't be any export information at all).
*
* Otherwise, photos are exported. If photos are exported, there may or may not be pagination
* information, and there may or may not be album information. If there is no container
* resource, that means that we're exporting albumless photos and a pagination token must be
* present. The beginning step of exporting albumless photos is indicated by a pagination token
* containing only PHOTO_TOKEN_PREFIX with no token attached, in order to differentiate this
* case for the first step of export (no export information at all).
*/
StringPaginationToken paginationToken = (StringPaginationToken) exportInformation.get().getPaginationData();
IdOnlyContainerResource idOnlyContainerResource = (IdOnlyContainerResource) exportInformation.get().getContainerResource();
boolean containerResourcePresent = idOnlyContainerResource != null;
boolean paginationDataPresent = paginationToken != null;
if (!containerResourcePresent && paginationDataPresent && paginationToken.getToken().startsWith(ALBUM_TOKEN_PREFIX)) {
return exportAlbums(authData, Optional.of(paginationToken), jobId);
} else {
return exportPhotos(authData, Optional.ofNullable(idOnlyContainerResource), Optional.ofNullable(paginationToken), jobId);
}
}
use of org.datatransferproject.types.common.models.IdOnlyContainerResource 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);
}
Aggregations