Search in sources :

Example 6 with PhotosModelWrapper

use of org.dataportabilityproject.dataModels.photos.PhotosModelWrapper in project data-transfer-project by google.

the class FlickrPhotoService method getPhotos.

private PhotosModelWrapper getPhotos(String photosetId, Optional<PaginationInformation> paginationInformation) throws IOException {
    try {
        int page = getPage(paginationInformation);
        PhotoList<Photo> photoSetList;
        if (null == photosetId) {
            RequestContext.getRequestContext().setExtras(EXTRAS);
            photoSetList = photosInterface.getNotInSet(PHOTO_PER_PAGE, page);
            RequestContext.getRequestContext().setExtras(ImmutableList.of());
        } else {
            photoSetList = photosetsInterface.getPhotos(photosetId, ImmutableSet.copyOf(EXTRAS), 0, PHOTO_PER_PAGE, page);
        }
        boolean hasMore = photoSetList.getPage() != photoSetList.getPages() && !photoSetList.isEmpty();
        Collection<PhotoModel> photos = photoSetList.stream().map(p -> toCommonPhoto(p, photosetId)).collect(Collectors.toList());
        FlickrPaginationInformation newPage = null;
        if (hasMore) {
            newPage = new FlickrPaginationInformation(page + 1);
        }
        return new PhotosModelWrapper(null, photos, new ContinuationInformation(null, newPage));
    } catch (FlickrException e) {
        throw new IOException("Couldn't fetch photos in album: " + photosetId, e);
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) Importer(org.dataportabilityproject.dataModels.Importer) FlickrException(com.flickr4java.flickr.FlickrException) AppCredentials(org.dataportabilityproject.shared.AppCredentials) LoggerFactory(org.slf4j.LoggerFactory) Photoset(com.flickr4java.flickr.photosets.Photoset) REST(com.flickr4java.flickr.REST) PhotoAlbum(org.dataportabilityproject.dataModels.photos.PhotoAlbum) ArrayList(java.util.ArrayList) Photo(com.flickr4java.flickr.photos.Photo) Strings(com.google.api.client.repackaged.com.google.common.base.Strings) ImmutableList(com.google.common.collect.ImmutableList) PaginationInformation(org.dataportabilityproject.dataModels.PaginationInformation) PhotoModel(org.dataportabilityproject.dataModels.photos.PhotoModel) JobDataCache(org.dataportabilityproject.cloud.interfaces.JobDataCache) Uploader(com.flickr4java.flickr.uploader.Uploader) Flickr(com.flickr4java.flickr.Flickr) PhotoList(com.flickr4java.flickr.photos.PhotoList) Auth(com.flickr4java.flickr.auth.Auth) ImageStreamProvider(org.dataportabilityproject.shared.ImageStreamProvider) ImmutableSet(com.google.common.collect.ImmutableSet) Logger(org.slf4j.Logger) PhotosInterface(com.flickr4java.flickr.photos.PhotosInterface) Resource(org.dataportabilityproject.dataModels.Resource) ExportInformation(org.dataportabilityproject.dataModels.ExportInformation) Collection(java.util.Collection) IOException(java.io.IOException) IdOnlyResource(org.dataportabilityproject.shared.IdOnlyResource) Collectors(java.util.stream.Collectors) Preconditions.checkState(com.google.common.base.Preconditions.checkState) UploadMetaData(com.flickr4java.flickr.uploader.UploadMetaData) Exporter(org.dataportabilityproject.dataModels.Exporter) Photosets(com.flickr4java.flickr.photosets.Photosets) List(java.util.List) ContinuationInformation(org.dataportabilityproject.dataModels.ContinuationInformation) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) RequestContext(com.flickr4java.flickr.RequestContext) PhotosModelWrapper(org.dataportabilityproject.dataModels.photos.PhotosModelWrapper) PhotosetsInterface(com.flickr4java.flickr.photosets.PhotosetsInterface) ContinuationInformation(org.dataportabilityproject.dataModels.ContinuationInformation) FlickrException(com.flickr4java.flickr.FlickrException) PhotoModel(org.dataportabilityproject.dataModels.photos.PhotoModel) PhotosModelWrapper(org.dataportabilityproject.dataModels.photos.PhotosModelWrapper) Photo(com.flickr4java.flickr.photos.Photo) IOException(java.io.IOException)

Example 7 with PhotosModelWrapper

use of org.dataportabilityproject.dataModels.photos.PhotosModelWrapper in project data-transfer-project by google.

the class SmugMugPhotoService method getImages.

private PhotosModelWrapper getImages(IdOnlyResource resource, Optional<PaginationInformation> paginationInformation) throws IOException {
    List<PhotoModel> photos = new ArrayList<>();
    String url;
    if (paginationInformation.isPresent()) {
        url = ((StringPaginationToken) paginationInformation.get()).getId();
    } else {
        String id = resource.getId();
        url = "/api/v2/album/" + id + "!images";
    }
    StringPaginationToken pageToken = null;
    SmugMugResponse<SmugMugAlbumInfoResponse> albumInfoResponse = makeAlbumInfoRequest(url);
    if (albumInfoResponse.getResponse().getImages() != null) {
        for (SmugMugAlbumImage image : albumInfoResponse.getResponse().getImages()) {
            String title = image.getTitle();
            if (Strings.isNullOrEmpty(title)) {
                title = image.getFileName();
            }
            try {
                photos.add(new PhotoModel(title, this.authConsumer.sign(image.getArchivedUri()), image.getCaption(), image.getFormat(), resource.getId()));
            } catch (OAuthException e) {
                throw new IOException("Couldn't sign: " + image.getArchivedUri(), e);
            }
        }
        if (albumInfoResponse.getResponse().getPageInfo().getNextPage() != null) {
            pageToken = new StringPaginationToken(albumInfoResponse.getResponse().getPageInfo().getNextPage());
        }
    }
    return new PhotosModelWrapper(null, photos, new ContinuationInformation(null, pageToken));
}
Also used : ContinuationInformation(org.dataportabilityproject.dataModels.ContinuationInformation) PhotoModel(org.dataportabilityproject.dataModels.photos.PhotoModel) SmugMugAlbumImage(org.dataportabilityproject.serviceProviders.smugmug.model.SmugMugAlbumImage) OAuthException(oauth.signpost.exception.OAuthException) ArrayList(java.util.ArrayList) PhotosModelWrapper(org.dataportabilityproject.dataModels.photos.PhotosModelWrapper) IOException(java.io.IOException) StringPaginationToken(org.dataportabilityproject.shared.StringPaginationToken) SmugMugAlbumInfoResponse(org.dataportabilityproject.serviceProviders.smugmug.model.SmugMugAlbumInfoResponse)

Example 8 with PhotosModelWrapper

use of org.dataportabilityproject.dataModels.photos.PhotosModelWrapper in project data-transfer-project by google.

the class FlickrPhotoServiceTest method exportPhotosFromPhotoset.

@Test
public void exportPhotosFromPhotoset() throws FlickrException, IOException {
    // Situation: getting photos from a set with id photosetsId and page 1
    int page = 1;
    String photosetsId = "photosetsId";
    ExportInformation exportInformation = new ExportInformation(Optional.of(new IdOnlyResource(photosetsId)), Optional.empty());
    // Make a bunch of photos, add them to PhotoList, and add pagination information
    int numPhotos = 4;
    PhotoList<Photo> listOfPhotos = new PhotoList<>();
    for (int i = 0; i < numPhotos; i++) {
        Photo photo = initializePhoto("title" + i, "url" + i, "description" + i);
        listOfPhotos.add(photo);
    }
    listOfPhotos.setPage(page);
    listOfPhotos.setPages(page + 1);
    when(photosetsInterface.getPhotos(anyString(), anySet(), anyInt(), anyInt(), anyInt())).thenReturn(listOfPhotos);
    // Run test
    PhotosModelWrapper result = photoService.export(exportInformation);
    assertThat(result.getPhotos().size()).isEqualTo(numPhotos);
    assertThat(result.getAlbums()).isEmpty();
    assertThat(result.getContinuationInformation().getSubResources()).isEmpty();
    assertThat(result.getContinuationInformation().getPaginationInformation()).isEqualTo(new FlickrPaginationInformation(page + 1));
}
Also used : ExportInformation(org.dataportabilityproject.dataModels.ExportInformation) PhotoList(com.flickr4java.flickr.photos.PhotoList) PhotosModelWrapper(org.dataportabilityproject.dataModels.photos.PhotosModelWrapper) Photo(com.flickr4java.flickr.photos.Photo) Matchers.anyString(org.mockito.Matchers.anyString) IdOnlyResource(org.dataportabilityproject.shared.IdOnlyResource) Test(org.junit.Test)

Example 9 with PhotosModelWrapper

use of org.dataportabilityproject.dataModels.photos.PhotosModelWrapper in project data-transfer-project by google.

the class FlickrPhotoServiceTest method exportAlbumInitial.

@Test
public void exportAlbumInitial() throws IOException, FlickrException {
    // Set up initial export information, such as what FlickrPhotoService would see when a transfer
    // is initiated
    ExportInformation emptyExportInfo = new ExportInformation(Optional.empty(), Optional.empty());
    // Set up auth
    when(user.getId()).thenReturn("userId");
    // Set up photoset
    String photosetId = "photosetId";
    String photosetTitle = "title";
    String photosetDescription = "description";
    Photoset photoset = initializePhotoset(photosetId, photosetTitle, photosetDescription);
    // Set up photosets list (aka album view)
    int page = 1;
    Photosets photosetList = new Photosets();
    photosetList.setPage(page);
    photosetList.setPages(page + 1);
    photosetList.setPhotosets(Collections.singletonList(photoset));
    when(photosetsInterface.getList(anyString(), anyInt(), anyInt(), anyString())).thenReturn(photosetList);
    // Run test
    PhotosModelWrapper result = photoService.export(emptyExportInfo);
    // Make sure album/photo information is correct
    assertThat(result.getPhotos()).isEmpty();
    Collection<PhotoAlbum> albums = result.getAlbums();
    assertThat(albums.size()).isEqualTo(1);
    assertThat(albums).containsExactly(new PhotoAlbum(photosetId, photosetTitle, photosetDescription));
    // Make sure continuation information is correct
    ContinuationInformation continuationInformation = result.getContinuationInformation();
    assertThat((FlickrPaginationInformation) continuationInformation.getPaginationInformation()).isEqualTo(new FlickrPaginationInformation(page + 1));
    Collection<? extends Resource> subResources = continuationInformation.getSubResources();
    assertThat(subResources.size()).isEqualTo(1);
    assertThat(subResources).containsExactly(new IdOnlyResource(photosetId));
}
Also used : ExportInformation(org.dataportabilityproject.dataModels.ExportInformation) ContinuationInformation(org.dataportabilityproject.dataModels.ContinuationInformation) Photoset(com.flickr4java.flickr.photosets.Photoset) Photosets(com.flickr4java.flickr.photosets.Photosets) PhotosModelWrapper(org.dataportabilityproject.dataModels.photos.PhotosModelWrapper) Matchers.anyString(org.mockito.Matchers.anyString) PhotoAlbum(org.dataportabilityproject.dataModels.photos.PhotoAlbum) IdOnlyResource(org.dataportabilityproject.shared.IdOnlyResource) Test(org.junit.Test)

Example 10 with PhotosModelWrapper

use of org.dataportabilityproject.dataModels.photos.PhotosModelWrapper in project data-transfer-project by google.

the class FlickrPhotoServiceTest method importStoresAlbumsInJobCache.

@Test
public void importStoresAlbumsInJobCache() throws IOException, FlickrException {
    // Set up input: a single photo album with a single photo
    PhotosModelWrapper wrapper = new PhotosModelWrapper(Collections.singletonList(PHOTO_ALBUM), Collections.singletonList(PHOTO_MODEL), new ContinuationInformation(null, null));
    // Set up mocks
    when(imageStreamProvider.get(FETCHABLE_URL)).thenReturn(bufferedInputStream);
    when(uploader.upload(any(BufferedInputStream.class), any(UploadMetaData.class))).thenReturn(FLICKR_PHOTO_ID);
    String flickrAlbumTitle = FlickrPhotoService.COPY_PREFIX + ALBUM_NAME;
    Photoset photoSet = initializePhotoset(FLICKR_ALBUM_ID, flickrAlbumTitle, ALBUM_DESCRIPTION);
    when(photosetsInterface.create(flickrAlbumTitle, ALBUM_DESCRIPTION, FLICKR_PHOTO_ID)).thenReturn(photoSet);
    // Run test
    photoService.importItem(wrapper);
    // Verify the image stream provider got the correct url
    verify(imageStreamProvider).get(FETCHABLE_URL);
    // Verify the correct photo information was uploaded
    ArgumentCaptor<UploadMetaData> uploadMetaDataArgumentCaptor = ArgumentCaptor.forClass(UploadMetaData.class);
    verify(uploader).upload(eq(bufferedInputStream), uploadMetaDataArgumentCaptor.capture());
    UploadMetaData actualUploadMetaData = uploadMetaDataArgumentCaptor.getValue();
    assertThat(actualUploadMetaData.getTitle()).isEqualTo(FlickrPhotoService.COPY_PREFIX + PHOTO_TITLE);
    assertThat(actualUploadMetaData.getDescription()).isEqualTo(PHOTO_DESCRIPTION);
    // Verify the photosets interface got the command to create the correct album
    verify(photosetsInterface).create(flickrAlbumTitle, ALBUM_DESCRIPTION, FLICKR_PHOTO_ID);
    // Check jobDataCache contents
    String expectedAlbumKey = FlickrPhotoService.CACHE_ALBUM_METADATA_PREFIX + ALBUM_ID;
    assertThat(jobDataCache.hasKey(expectedAlbumKey)).isTrue();
    assertThat(jobDataCache.getData(expectedAlbumKey, PhotoAlbum.class)).isEqualTo(PHOTO_ALBUM);
    assertThat(jobDataCache.getData(ALBUM_ID, String.class)).isEqualTo(FLICKR_ALBUM_ID);
}
Also used : ContinuationInformation(org.dataportabilityproject.dataModels.ContinuationInformation) BufferedInputStream(java.io.BufferedInputStream) Photoset(com.flickr4java.flickr.photosets.Photoset) PhotosModelWrapper(org.dataportabilityproject.dataModels.photos.PhotosModelWrapper) UploadMetaData(com.flickr4java.flickr.uploader.UploadMetaData) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Aggregations

PhotosModelWrapper (org.dataportabilityproject.dataModels.photos.PhotosModelWrapper)10 ContinuationInformation (org.dataportabilityproject.dataModels.ContinuationInformation)8 ArrayList (java.util.ArrayList)7 PhotoAlbum (org.dataportabilityproject.dataModels.photos.PhotoAlbum)6 IdOnlyResource (org.dataportabilityproject.shared.IdOnlyResource)6 IOException (java.io.IOException)5 Photoset (com.flickr4java.flickr.photosets.Photoset)4 PhotoModel (org.dataportabilityproject.dataModels.photos.PhotoModel)4 Photosets (com.flickr4java.flickr.photosets.Photosets)3 ExportInformation (org.dataportabilityproject.dataModels.ExportInformation)3 Resource (org.dataportabilityproject.dataModels.Resource)3 Test (org.junit.Test)3 Matchers.anyString (org.mockito.Matchers.anyString)3 FlickrException (com.flickr4java.flickr.FlickrException)2 Photo (com.flickr4java.flickr.photos.Photo)2 PhotoList (com.flickr4java.flickr.photos.PhotoList)2 UploadMetaData (com.flickr4java.flickr.uploader.UploadMetaData)2 ImmutableList (com.google.common.collect.ImmutableList)2 GphotoEntry (com.google.gdata.data.photos.GphotoEntry)2 ServiceException (com.google.gdata.util.ServiceException)2