Search in sources :

Example 1 with PhotoList

use of com.flickr4java.flickr.photos.PhotoList in project data-transfer-project by google.

the class FlickrPhotosExporter method getPhotos.

private ExportResult<PhotosContainerResource> getPhotos(IdOnlyContainerResource resource, PaginationData paginationData) {
    String photoSetId = resource.getId();
    int page = paginationData == null ? 1 : ((IntPaginationToken) paginationData).getStart();
    PhotoList<Photo> photoSetList;
    try {
        if (photoSetId == null) {
            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);
        }
    } catch (FlickrException e) {
        return new ExportResult<>(ResultType.ERROR, "Error exporting Flickr photo: " + e.getErrorMessage());
    }
    boolean hasMore = photoSetList.getPage() != photoSetList.getPages() && !photoSetList.isEmpty();
    Collection<PhotoModel> photos = photoSetList.stream().map(p -> toCommonPhoto(p, photoSetId)).collect(Collectors.toList());
    PaginationData newPage = null;
    if (hasMore) {
        newPage = new IntPaginationToken(page + 1);
    }
    // Get result type
    ResultType resultType = ResultType.CONTINUE;
    if (newPage == null) {
        resultType = ResultType.END;
    }
    PhotosContainerResource photosContainerResource = new PhotosContainerResource(null, photos);
    return new ExportResult<>(resultType, photosContainerResource, new ContinuationData(newPage));
}
Also used : IdOnlyContainerResource(org.dataportabilityproject.spi.transfer.types.IdOnlyContainerResource) PhotoAlbum(org.dataportabilityproject.types.transfer.models.photos.PhotoAlbum) FlickrException(com.flickr4java.flickr.FlickrException) ExportResult(org.dataportabilityproject.spi.transfer.provider.ExportResult) LoggerFactory(org.slf4j.LoggerFactory) IntPaginationToken(org.dataportabilityproject.spi.transfer.types.IntPaginationToken) Photoset(com.flickr4java.flickr.photosets.Photoset) REST(com.flickr4java.flickr.REST) ArrayList(java.util.ArrayList) Photo(com.flickr4java.flickr.photos.Photo) Strings(com.google.common.base.Strings) ImmutableList(com.google.common.collect.ImmutableList) ContinuationData(org.dataportabilityproject.spi.transfer.types.ContinuationData) AuthData(org.dataportabilityproject.types.transfer.auth.AuthData) Flickr(com.flickr4java.flickr.Flickr) PhotoList(com.flickr4java.flickr.photos.PhotoList) Auth(com.flickr4java.flickr.auth.Auth) PaginationData(org.dataportabilityproject.spi.transfer.types.PaginationData) ExportInformation(org.dataportabilityproject.spi.transfer.types.ExportInformation) ImmutableSet(com.google.common.collect.ImmutableSet) Exporter(org.dataportabilityproject.spi.transfer.provider.Exporter) Logger(org.slf4j.Logger) PhotosInterface(com.flickr4java.flickr.photos.PhotosInterface) PhotosContainerResource(org.dataportabilityproject.types.transfer.models.photos.PhotosContainerResource) Collection(java.util.Collection) ResultType(org.dataportabilityproject.spi.transfer.provider.ExportResult.ResultType) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Photosets(com.flickr4java.flickr.photosets.Photosets) List(java.util.List) PhotoModel(org.dataportabilityproject.types.transfer.models.photos.PhotoModel) Preconditions(com.google.common.base.Preconditions) AppCredentials(org.dataportabilityproject.types.transfer.auth.AppCredentials) VisibleForTesting(com.google.common.annotations.VisibleForTesting) RequestContext(com.flickr4java.flickr.RequestContext) PhotosetsInterface(com.flickr4java.flickr.photosets.PhotosetsInterface) IntPaginationToken(org.dataportabilityproject.spi.transfer.types.IntPaginationToken) PaginationData(org.dataportabilityproject.spi.transfer.types.PaginationData) FlickrException(com.flickr4java.flickr.FlickrException) PhotoModel(org.dataportabilityproject.types.transfer.models.photos.PhotoModel) Photo(com.flickr4java.flickr.photos.Photo) ContinuationData(org.dataportabilityproject.spi.transfer.types.ContinuationData) ResultType(org.dataportabilityproject.spi.transfer.provider.ExportResult.ResultType) PhotosContainerResource(org.dataportabilityproject.types.transfer.models.photos.PhotosContainerResource) ExportResult(org.dataportabilityproject.spi.transfer.provider.ExportResult)

Example 2 with PhotoList

use of com.flickr4java.flickr.photos.PhotoList 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);
    ExportResult<PhotosContainerResource> result = exporter.export(UUID.randomUUID(), new TokenSecretAuthData("token", "secret"), 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.dataportabilityproject.spi.transfer.types.IntPaginationToken) IntPaginationToken(org.dataportabilityproject.spi.transfer.types.IntPaginationToken) Token(org.scribe.model.Token) Photo(com.flickr4java.flickr.photos.Photo) ContinuationData(org.dataportabilityproject.spi.transfer.types.ContinuationData) Matchers.anyString(org.mockito.Matchers.anyString) ExportInformation(org.dataportabilityproject.spi.transfer.types.ExportInformation) PhotosContainerResource(org.dataportabilityproject.types.transfer.models.photos.PhotosContainerResource) PhotoList(com.flickr4java.flickr.photos.PhotoList) TokenSecretAuthData(org.dataportabilityproject.types.transfer.auth.TokenSecretAuthData) IdOnlyContainerResource(org.dataportabilityproject.spi.transfer.types.IdOnlyContainerResource) Test(org.junit.Test)

Example 3 with PhotoList

use of com.flickr4java.flickr.photos.PhotoList 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 4 with PhotoList

use of com.flickr4java.flickr.photos.PhotoList 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 5 with PhotoList

use of com.flickr4java.flickr.photos.PhotoList in project osm-contributor by jawg.

the class GetFlickrPhotos method doInBackground.

@Override
protected List<List<Size>> doInBackground(Void... params) {
    // Create search tags list
    ArrayList<String> searchTags = new ArrayList<String>(TAGS);
    searchTags.add(new StringBuilder("osm:").append((featurePoi.getWay()) ? "way" : "node").append("=").append(featurePoi.getBackendId()).toString());
    SearchParameters parameters = new SearchParameters();
    parameters.setLatitude(String.valueOf(latitude));
    parameters.setLongitude(String.valueOf(longitude));
    parameters.setRadius(RADIUS);
    parameters.setTags(searchTags.toArray(new String[searchTags.size()]));
    parameters.setSort(SearchParameters.INTERESTINGNESS_DESC);
    if (!isCancelled()) {
        try {
            PhotoList<Photo> photos = flickr.getPhotosInterface().search(parameters, limitPerPage, nbPage);
            List<List<Size>> photosList = new ArrayList<>();
            for (Photo photo : photos) {
                photosList.add((List<Size>) flickr.getPhotosInterface().getSizes(photo.getId()));
            }
            return photosList;
        } catch (FlickrException e) {
            e.printStackTrace();
        }
    }
    return null;
}
Also used : SearchParameters(com.flickr4java.flickr.photos.SearchParameters) FlickrException(com.flickr4java.flickr.FlickrException) Size(com.flickr4java.flickr.photos.Size) ArrayList(java.util.ArrayList) Photo(com.flickr4java.flickr.photos.Photo) ArrayList(java.util.ArrayList) List(java.util.List) PhotoList(com.flickr4java.flickr.photos.PhotoList)

Aggregations

Photo (com.flickr4java.flickr.photos.Photo)5 PhotoList (com.flickr4java.flickr.photos.PhotoList)5 FlickrException (com.flickr4java.flickr.FlickrException)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Flickr (com.flickr4java.flickr.Flickr)2 REST (com.flickr4java.flickr.REST)2 RequestContext (com.flickr4java.flickr.RequestContext)2 Auth (com.flickr4java.flickr.auth.Auth)2 PhotosInterface (com.flickr4java.flickr.photos.PhotosInterface)2 Photoset (com.flickr4java.flickr.photosets.Photoset)2 Photosets (com.flickr4java.flickr.photosets.Photosets)2 PhotosetsInterface (com.flickr4java.flickr.photosets.PhotosetsInterface)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 Collection (java.util.Collection)2 Collectors (java.util.stream.Collectors)2 ExportInformation (org.dataportabilityproject.dataModels.ExportInformation)2 PhotosModelWrapper (org.dataportabilityproject.dataModels.photos.PhotosModelWrapper)2