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));
}
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);
}
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());
}
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);
}
}
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);
}
}
Aggregations