Search in sources :

Example 1 with PhotoAlbum

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"));
}
Also used : IntPaginationToken(org.dataportabilityproject.spi.transfer.types.IntPaginationToken) AuthData(org.dataportabilityproject.types.transfer.auth.AuthData) TokenSecretAuthData(org.dataportabilityproject.types.transfer.auth.TokenSecretAuthData) IntPaginationToken(org.dataportabilityproject.spi.transfer.types.IntPaginationToken) Token(org.scribe.model.Token) ContinuationData(org.dataportabilityproject.spi.transfer.types.ContinuationData) PhotosContainerResource(org.dataportabilityproject.types.transfer.models.photos.PhotosContainerResource) TokenSecretAuthData(org.dataportabilityproject.types.transfer.auth.TokenSecretAuthData) Photoset(com.flickr4java.flickr.photosets.Photoset) Photosets(com.flickr4java.flickr.photosets.Photosets) IdOnlyContainerResource(org.dataportabilityproject.spi.transfer.types.IdOnlyContainerResource) PhotoAlbum(org.dataportabilityproject.types.transfer.models.photos.PhotoAlbum) Test(org.junit.Test)

Example 2 with PhotoAlbum

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;
}
Also used : SmugMugUserResponse(org.dataportabilityproject.transfer.smugmug.photos.model.SmugMugUserResponse) ImportResult(org.dataportabilityproject.spi.transfer.provider.ImportResult) PhotoModel(org.dataportabilityproject.types.transfer.models.photos.PhotoModel) PhotoAlbum(org.dataportabilityproject.types.transfer.models.photos.PhotoAlbum) IOException(java.io.IOException)

Example 3 with PhotoAlbum

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());
    }
}
Also used : PaginationData(org.dataportabilityproject.spi.transfer.types.PaginationData) ArrayList(java.util.ArrayList) ContinuationData(org.dataportabilityproject.spi.transfer.types.ContinuationData) ResultType(org.dataportabilityproject.spi.transfer.provider.ExportResult.ResultType) IOException(java.io.IOException) URL(java.net.URL) PhotosContainerResource(org.dataportabilityproject.types.transfer.models.photos.PhotosContainerResource) ServiceException(com.google.gdata.util.ServiceException) UserFeed(com.google.gdata.data.photos.UserFeed) IdOnlyContainerResource(org.dataportabilityproject.spi.transfer.types.IdOnlyContainerResource) PhotoAlbum(org.dataportabilityproject.types.transfer.models.photos.PhotoAlbum) GphotoEntry(com.google.gdata.data.photos.GphotoEntry) StringPaginationToken(org.dataportabilityproject.spi.transfer.types.StringPaginationToken) ExportResult(org.dataportabilityproject.spi.transfer.provider.ExportResult)

Example 4 with PhotoAlbum

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);
}
Also used : IntPaginationToken(org.dataportabilityproject.spi.transfer.types.IntPaginationToken) PaginationData(org.dataportabilityproject.spi.transfer.types.PaginationData) FlickrException(com.flickr4java.flickr.FlickrException) ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) ContinuationData(org.dataportabilityproject.spi.transfer.types.ContinuationData) ResultType(org.dataportabilityproject.spi.transfer.provider.ExportResult.ResultType) PhotosContainerResource(org.dataportabilityproject.types.transfer.models.photos.PhotosContainerResource) Photoset(com.flickr4java.flickr.photosets.Photoset) Photosets(com.flickr4java.flickr.photosets.Photosets) IdOnlyContainerResource(org.dataportabilityproject.spi.transfer.types.IdOnlyContainerResource) PhotoAlbum(org.dataportabilityproject.types.transfer.models.photos.PhotoAlbum) ExportResult(org.dataportabilityproject.spi.transfer.provider.ExportResult)

Example 5 with PhotoAlbum

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);
}
Also used : TempPhotosData(org.dataportabilityproject.spi.transfer.types.TempPhotosData) ImportResult(org.dataportabilityproject.spi.transfer.provider.ImportResult) FlickrException(com.flickr4java.flickr.FlickrException) Photoset(com.flickr4java.flickr.photosets.Photoset) Auth(com.flickr4java.flickr.auth.Auth) PhotoModel(org.dataportabilityproject.types.transfer.models.photos.PhotoModel) PhotoAlbum(org.dataportabilityproject.types.transfer.models.photos.PhotoAlbum) IOException(java.io.IOException)

Aggregations

PhotoAlbum (org.dataportabilityproject.types.transfer.models.photos.PhotoAlbum)6 ContinuationData (org.dataportabilityproject.spi.transfer.types.ContinuationData)4 IdOnlyContainerResource (org.dataportabilityproject.spi.transfer.types.IdOnlyContainerResource)4 PhotosContainerResource (org.dataportabilityproject.types.transfer.models.photos.PhotosContainerResource)4 Photoset (com.flickr4java.flickr.photosets.Photoset)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 ExportResult (org.dataportabilityproject.spi.transfer.provider.ExportResult)3 ResultType (org.dataportabilityproject.spi.transfer.provider.ExportResult.ResultType)3 FlickrException (com.flickr4java.flickr.FlickrException)2 Photosets (com.flickr4java.flickr.photosets.Photosets)2 ImportResult (org.dataportabilityproject.spi.transfer.provider.ImportResult)2 IntPaginationToken (org.dataportabilityproject.spi.transfer.types.IntPaginationToken)2 PaginationData (org.dataportabilityproject.spi.transfer.types.PaginationData)2 StringPaginationToken (org.dataportabilityproject.spi.transfer.types.StringPaginationToken)2 SmugMugUserResponse (org.dataportabilityproject.transfer.smugmug.photos.model.SmugMugUserResponse)2 PhotoModel (org.dataportabilityproject.types.transfer.models.photos.PhotoModel)2 Auth (com.flickr4java.flickr.auth.Auth)1 ImmutableList (com.google.common.collect.ImmutableList)1 GphotoEntry (com.google.gdata.data.photos.GphotoEntry)1