Search in sources :

Example 36 with IdOnlyContainerResource

use of org.datatransferproject.types.common.models.IdOnlyContainerResource 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);
}
Also used : ArrayList(java.util.ArrayList) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) IOException(java.io.IOException) ResultType(org.datatransferproject.spi.transfer.provider.ExportResult.ResultType) PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) SmugMugAlbum(org.datatransferproject.transfer.smugmug.photos.model.SmugMugAlbum) SmugMugAlbumsResponse(org.datatransferproject.transfer.smugmug.photos.model.SmugMugAlbumsResponse) IdOnlyContainerResource(org.datatransferproject.types.common.models.IdOnlyContainerResource) PhotoAlbum(org.datatransferproject.types.common.models.photos.PhotoAlbum) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult)

Example 37 with IdOnlyContainerResource

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

Example 38 with IdOnlyContainerResource

use of org.datatransferproject.types.common.models.IdOnlyContainerResource in project data-transfer-project by google.

the class FlickrPhotosExporterTest method exportPhotosFromPhotoset.

@Test
public void exportPhotosFromPhotoset() 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);
    // getting photos from a set with id photosetsId and page 1
    int page = 1;
    String photosetsId = "photosetsId";
    ExportInformation exportInformation = new ExportInformation(null, new IdOnlyContainerResource(photosetsId));
    // make lots of photos and add them to PhotoList (also adding pagination information)
    int numPhotos = 4;
    PhotoList<Photo> photosList = new PhotoList<>();
    for (int i = 0; i < numPhotos; i++) {
        photosList.add(FlickrTestUtils.initializePhoto("title" + 1, "url" + i, "description" + i, MEDIA_TYPE));
    }
    photosList.setPage(page);
    photosList.setPages(page + 1);
    when(photosetsInterface.getPhotos(anyString(), anySet(), anyInt(), anyInt(), anyInt())).thenReturn(photosList);
    // run test
    FlickrPhotosExporter exporter = new FlickrPhotosExporter(flickr, TransferServiceConfig.getDefaultInstance());
    ExportResult<PhotosContainerResource> result = exporter.export(UUID.randomUUID(), new TokenSecretAuthData("token", "secret"), Optional.of(exportInformation));
    assertThat(result.getExportedData().getPhotos().size()).isEqualTo(numPhotos);
    assertThat(result.getExportedData().getAlbums()).isEmpty();
    ContinuationData continuationData = (ContinuationData) result.getContinuationData();
    assertThat(continuationData.getContainerResources()).isEmpty();
    assertThat(((IntPaginationToken) continuationData.getPaginationData()).getStart()).isEqualTo(page + 1);
}
Also used : IntPaginationToken(org.datatransferproject.types.common.IntPaginationToken) IntPaginationToken(org.datatransferproject.types.common.IntPaginationToken) Token(org.scribe.model.Token) Photo(com.flickr4java.flickr.photos.Photo) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) ExportInformation(org.datatransferproject.types.common.ExportInformation) PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) PhotoList(com.flickr4java.flickr.photos.PhotoList) TokenSecretAuthData(org.datatransferproject.types.transfer.auth.TokenSecretAuthData) IdOnlyContainerResource(org.datatransferproject.types.common.models.IdOnlyContainerResource) Test(org.junit.Test)

Example 39 with IdOnlyContainerResource

use of org.datatransferproject.types.common.models.IdOnlyContainerResource in project data-transfer-project by google.

the class FacebookPhotosExporterTest method testExportPhoto.

@Test
public void testExportPhoto() throws CopyExceptionWithFailureReason {
    ExportResult<PhotosContainerResource> result = facebookPhotosExporter.export(uuid, new TokensAndUrlAuthData("accessToken", null, null), Optional.of(new ExportInformation(null, new IdOnlyContainerResource(ALBUM_ID))));
    assertEquals(ExportResult.ResultType.END, result.getType());
    PhotosContainerResource exportedData = result.getExportedData();
    assertEquals(1, exportedData.getPhotos().size());
    assertEquals(new PhotoModel(PHOTO_ID + ".jpg", PHOTO_ID, PHOTO_NAME, "image/jpg", PHOTO_ID, ALBUM_ID, false, PHOTO_TIME), exportedData.getPhotos().toArray()[0]);
}
Also used : PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) ExportInformation(org.datatransferproject.types.common.ExportInformation) PhotoModel(org.datatransferproject.types.common.models.photos.PhotoModel) TokensAndUrlAuthData(org.datatransferproject.types.transfer.auth.TokensAndUrlAuthData) IdOnlyContainerResource(org.datatransferproject.types.common.models.IdOnlyContainerResource) Test(org.junit.Test)

Example 40 with IdOnlyContainerResource

use of org.datatransferproject.types.common.models.IdOnlyContainerResource in project data-transfer-project by google.

the class FlickrPhotosExporter method export.

@Override
public ExportResult<PhotosContainerResource> export(UUID jobId, AuthData authData, Optional<ExportInformation> exportInformation) {
    Auth auth;
    try {
        auth = FlickrUtils.getAuth(authData, flickr);
    } catch (FlickrException e) {
        return new ExportResult<>(e);
    }
    RequestContext.getRequestContext().setAuth(auth);
    // in that container instead of the whole user library
    if (exportInformation.isPresent() && exportInformation.get().getContainerResource() instanceof PhotosContainerResource) {
        return exportPhotosContainer((PhotosContainerResource) exportInformation.get().getContainerResource());
    }
    PaginationData paginationData = exportInformation.isPresent() ? exportInformation.get().getPaginationData() : null;
    IdOnlyContainerResource resource = exportInformation.isPresent() ? (IdOnlyContainerResource) exportInformation.get().getContainerResource() : null;
    if (resource != null) {
        return getPhotos(resource, paginationData);
    } else {
        return getAlbums(paginationData, auth);
    }
}
Also used : PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) PaginationData(org.datatransferproject.types.common.PaginationData) FlickrException(com.flickr4java.flickr.FlickrException) Auth(com.flickr4java.flickr.auth.Auth) IdOnlyContainerResource(org.datatransferproject.types.common.models.IdOnlyContainerResource)

Aggregations

IdOnlyContainerResource (org.datatransferproject.types.common.models.IdOnlyContainerResource)43 PhotosContainerResource (org.datatransferproject.types.common.models.photos.PhotosContainerResource)29 ContinuationData (org.datatransferproject.spi.transfer.types.ContinuationData)27 StringPaginationToken (org.datatransferproject.types.common.StringPaginationToken)23 Test (org.junit.Test)21 PhotoAlbum (org.datatransferproject.types.common.models.photos.PhotoAlbum)20 ExportResult (org.datatransferproject.spi.transfer.provider.ExportResult)18 PaginationData (org.datatransferproject.types.common.PaginationData)17 ArrayList (java.util.ArrayList)12 ExportInformation (org.datatransferproject.types.common.ExportInformation)12 PhotoModel (org.datatransferproject.types.common.models.photos.PhotoModel)12 IOException (java.io.IOException)11 ContainerResource (org.datatransferproject.types.common.models.ContainerResource)10 ImmutableList (com.google.common.collect.ImmutableList)7 List (java.util.List)7 Optional (java.util.Optional)7 UUID (java.util.UUID)7 Collectors (java.util.stream.Collectors)7 ResultType (org.datatransferproject.spi.transfer.provider.ExportResult.ResultType)7 IntPaginationToken (org.datatransferproject.types.common.IntPaginationToken)7