Search in sources :

Example 6 with FlickrException

use of com.flickr4java.flickr.FlickrException 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 7 with FlickrException

use of com.flickr4java.flickr.FlickrException 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)

Example 8 with FlickrException

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

the class FlickrAuthDataGenerator method generateAuthData.

@Override
public AuthData generateAuthData(String callbackBaseUrl, String authCode, String id, AuthData initialAuthData, String extra) {
    Preconditions.checkArgument(Strings.isNullOrEmpty(extra), "Extra data not expected");
    Preconditions.checkNotNull(initialAuthData, "Earlier auth data not expected for Flickr flow");
    AuthInterface authInterface = flickr.getAuthInterface();
    Token token = fromAuthData(initialAuthData);
    Token requestToken = authInterface.getAccessToken(token, new Verifier(authCode));
    try {
        authInterface.checkToken(requestToken);
    } catch (FlickrException e) {
        logger.warn("Problem verifying auth token {}", e);
        return null;
    }
    return new TokenSecretAuthData(requestToken.getToken(), requestToken.getSecret());
}
Also used : AuthInterface(com.flickr4java.flickr.auth.AuthInterface) TokenSecretAuthData(org.dataportabilityproject.types.transfer.auth.TokenSecretAuthData) FlickrException(com.flickr4java.flickr.FlickrException) Token(org.scribe.model.Token) Verifier(org.scribe.model.Verifier)

Example 9 with FlickrException

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

the class FlickrPhotoService method importItem.

@Override
public void importItem(PhotosModelWrapper modelWrapper) throws IOException {
    // TODO(olsona): what should we do with the continuation information?
    try {
        for (PhotoAlbum album : modelWrapper.getAlbums()) {
            // Store the data in the cache because Flickr only allows you
            // to create an album with a photo in it so we need to wait for
            // the first photo to create the album.
            String key = CACHE_ALBUM_METADATA_PREFIX + album.getId();
            jobDataCache.store(key, album);
        }
        for (PhotoModel photo : modelWrapper.getPhotos()) {
            String photoId = uploadPhoto(photo);
            String oldAlbumId = photo.getAlbumId();
            if (!jobDataCache.hasKey(oldAlbumId)) {
                PhotoAlbum album = jobDataCache.getData(CACHE_ALBUM_METADATA_PREFIX + oldAlbumId, PhotoAlbum.class);
                Photoset photoset = photosetsInterface.create(COPY_PREFIX + album.getName(), album.getDescription(), photoId);
                jobDataCache.store(oldAlbumId, photoset.getId());
            } else {
                String newAlbumId = jobDataCache.getData(oldAlbumId, String.class);
                photosetsInterface.addPhoto(newAlbumId, photoId);
            }
        }
    } catch (FlickrException e) {
        throw new IOException("Problem communicating with serviceProviders.flickr", e);
    }
}
Also used : FlickrException(com.flickr4java.flickr.FlickrException) Photoset(com.flickr4java.flickr.photosets.Photoset) PhotoModel(org.dataportabilityproject.dataModels.photos.PhotoModel) PhotoAlbum(org.dataportabilityproject.dataModels.photos.PhotoAlbum) IOException(java.io.IOException)

Example 10 with FlickrException

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

the class FlickrPhotoService method getAlbums.

private PhotosModelWrapper getAlbums(Optional<PaginationInformation> paginationInformation) throws IOException {
    try {
        ImmutableList.Builder<PhotoAlbum> results = ImmutableList.builder();
        List<IdOnlyResource> subResources = new ArrayList<>();
        int page = getPage(paginationInformation);
        Photosets photoSetList = photosetsInterface.getList(auth.getUser().getId(), PHOTO_SETS_PER_PAGE, page, PHOTOSET_EXTRAS);
        for (Photoset photoset : photoSetList.getPhotosets()) {
            // Saving data to the album allows the target service
            // to recreate the album structure.
            results.add(new PhotoAlbum(photoset.getId(), photoset.getTitle(), photoset.getDescription()));
            // Adding sub-resources tells the framework to re-call
            // export to get all the photos.
            subResources.add(new IdOnlyResource(photoset.getId()));
        }
        FlickrPaginationInformation newPage = null;
        boolean hasMore = photoSetList.getPage() != photoSetList.getPages() && !photoSetList.getPhotosets().isEmpty();
        if (hasMore) {
            newPage = new FlickrPaginationInformation(page + 1);
        }
        return new PhotosModelWrapper(results.build(), null, new ContinuationInformation(subResources, newPage));
    } catch (FlickrException e) {
        throw new IOException("Couldn't fetch albums", e);
    }
}
Also used : FlickrException(com.flickr4java.flickr.FlickrException) ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) PhotosModelWrapper(org.dataportabilityproject.dataModels.photos.PhotosModelWrapper) IdOnlyResource(org.dataportabilityproject.shared.IdOnlyResource) IOException(java.io.IOException) ContinuationInformation(org.dataportabilityproject.dataModels.ContinuationInformation) Photoset(com.flickr4java.flickr.photosets.Photoset) Photosets(com.flickr4java.flickr.photosets.Photosets) PhotoAlbum(org.dataportabilityproject.dataModels.photos.PhotoAlbum)

Aggregations

FlickrException (com.flickr4java.flickr.FlickrException)12 IOException (java.io.IOException)7 Auth (com.flickr4java.flickr.auth.Auth)6 Photoset (com.flickr4java.flickr.photosets.Photoset)6 ArrayList (java.util.ArrayList)5 Photosets (com.flickr4java.flickr.photosets.Photosets)4 ImmutableList (com.google.common.collect.ImmutableList)4 AuthInterface (com.flickr4java.flickr.auth.AuthInterface)3 Photo (com.flickr4java.flickr.photos.Photo)3 PhotoList (com.flickr4java.flickr.photos.PhotoList)3 List (java.util.List)3 PhotoAlbum (org.dataportabilityproject.dataModels.photos.PhotoAlbum)3 IdOnlyContainerResource (org.dataportabilityproject.spi.transfer.types.IdOnlyContainerResource)3 PaginationData (org.dataportabilityproject.spi.transfer.types.PaginationData)3 PhotoAlbum (org.dataportabilityproject.types.transfer.models.photos.PhotoAlbum)3 Token (org.scribe.model.Token)3 Flickr (com.flickr4java.flickr.Flickr)2 REST (com.flickr4java.flickr.REST)2 RequestContext (com.flickr4java.flickr.RequestContext)2 PhotosInterface (com.flickr4java.flickr.photos.PhotosInterface)2