use of org.dataportabilityproject.types.transfer.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);
AuthData authData = new TokenSecretAuthData("token", "secret");
ExportResult<PhotosContainerResource> result = exporter.export(UUID.randomUUID(), authData);
// 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.dataportabilityproject.types.transfer.models.photos.PhotoAlbum in project data-transfer-project by google.
the class SmugMugPhotosImporter method importItem.
@Override
public ImportResult importItem(UUID jobId, AuthData authData, PhotosContainerResource data) {
try {
String folder = null;
if (!data.getAlbums().isEmpty()) {
SmugMugResponse<SmugMugUserResponse> userResponse = smugMugInterface.makeUserRequest(smugMugInterface.USER_URL);
folder = userResponse.getResponse().getUser().getUris().get("Folder").getUri();
}
for (PhotoAlbum album : data.getAlbums()) {
importSingleAlbum(jobId, folder, album);
}
for (PhotoModel photo : data.getPhotos()) {
importSinglePhoto(jobId, photo);
}
} catch (IOException e) {
// TODO(olsona): we should retry on individual errors
return new ImportResult(ResultType.ERROR, e.getMessage());
}
return ImportResult.OK;
}
use of org.dataportabilityproject.types.transfer.models.photos.PhotoAlbum in project data-transfer-project by google.
the class GooglePhotosExporter method exportAlbums.
private ExportResult<PhotosContainerResource> exportAlbums(TokensAndUrlAuthData authData, Optional<PaginationData> paginationData) {
try {
int startItem = 1;
if (paginationData.isPresent()) {
String token = ((StringPaginationToken) paginationData.get()).getToken();
Preconditions.checkArgument(token.startsWith(ALBUM_TOKEN_PREFIX), "Invalid pagination token " + token);
startItem = Integer.parseInt(token.substring(ALBUM_TOKEN_PREFIX.length()));
}
URL albumUrl = new URL(String.format(URL_ALBUM_FEED_FORMAT, startItem, MAX_RESULTS));
UserFeed albumFeed = getOrCreatePhotosService(authData).getFeed(albumUrl, UserFeed.class);
PaginationData nextPageData = null;
if (albumFeed.getAlbumEntries().size() == MAX_RESULTS) {
int nextPageStart = startItem + MAX_RESULTS;
nextPageData = new StringPaginationToken(ALBUM_TOKEN_PREFIX + nextPageStart);
}
ContinuationData continuationData = new ContinuationData(nextPageData);
List<PhotoAlbum> albums = new ArrayList<>(albumFeed.getAlbumEntries().size());
for (GphotoEntry googleAlbum : albumFeed.getAlbumEntries()) {
// Add album info to list so album can be recreated later
albums.add(new PhotoAlbum(googleAlbum.getGphotoId(), googleAlbum.getTitle().getPlainText(), googleAlbum.getDescription().getPlainText()));
// Add album id to continuation data
continuationData.addContainerResource(new IdOnlyContainerResource(googleAlbum.getGphotoId()));
}
ResultType resultType = ResultType.CONTINUE;
if (nextPageData == null || continuationData.getContainerResources().isEmpty()) {
resultType = ResultType.END;
}
PhotosContainerResource containerResource = new PhotosContainerResource(albums, null);
return new ExportResult<>(resultType, containerResource, continuationData);
} catch (ServiceException | IOException e) {
return new ExportResult<>(ResultType.ERROR, e.getMessage());
}
}
use of org.dataportabilityproject.types.transfer.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 {
photoSetList = photosetsInterface.getList(auth.getUser().getId(), PHOTO_SETS_PER_PAGE, page, PHOTOSET_EXTRAS);
} catch (FlickrException e) {
return new ExportResult<>(ResultType.ERROR, "Error exporting Flickr album: " + e.getErrorMessage());
}
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);
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.dataportabilityproject.types.transfer.models.photos.PhotoAlbum in project data-transfer-project by google.
the class FlickrPhotosImporter method importItem.
@Override
public ImportResult importItem(UUID jobId, AuthData authData, PhotosContainerResource data) {
Auth auth;
try {
auth = FlickrUtils.getAuth(authData, flickr);
} catch (FlickrException e) {
return new ImportResult(ImportResult.ResultType.ERROR, "Error authorizing Flickr Auth: " + e.getErrorMessage());
}
RequestContext.getRequestContext().setAuth(auth);
// Store any album data in the cache because Flickr only allows you to create an album with a
// photo in it, so we have to wait for the first photo to create the album
TempPhotosData tempPhotosData = jobStore.findData(TempPhotosData.class, jobId);
if (tempPhotosData == null) {
tempPhotosData = new TempPhotosData(jobId);
jobStore.create(jobId, tempPhotosData);
}
for (PhotoAlbum album : data.getAlbums()) {
tempPhotosData.addAlbum(CACHE_ALBUM_METADATA_PREFIX + album.getId(), album);
}
jobStore.update(jobId, tempPhotosData);
for (PhotoModel photo : data.getPhotos()) {
try {
String photoId = uploadPhoto(photo);
String oldAlbumId = photo.getAlbumId();
TempPhotosData tempData = jobStore.findData(TempPhotosData.class, jobId);
String newAlbumId = tempData.lookupNewAlbumId(oldAlbumId);
if (Strings.isNullOrEmpty(newAlbumId)) {
// This means that we havent created the new album yet, create the photoset
PhotoAlbum album = tempData.lookupAlbum(CACHE_ALBUM_METADATA_PREFIX + oldAlbumId);
Photoset photoset = photosetsInterface.create(COPY_PREFIX + album.getName(), album.getDescription(), photoId);
tempData.addAlbumId(oldAlbumId, photoset.getId());
} else {
// We've already created a new album, add the photo to the new album
photosetsInterface.addPhoto(newAlbumId, photoId);
}
jobStore.update(jobId, tempData);
} catch (FlickrException | IOException e) {
// TODO: figure out retries
return new ImportResult(ImportResult.ResultType.ERROR, "Error importing item: " + e.getMessage());
}
}
return new ImportResult(ImportResult.ResultType.OK);
}
Aggregations