use of org.datatransferproject.spi.transfer.provider.ExportResult in project data-transfer-project by google.
the class ImgurPhotosExporter method requestNonAlbumPhotos.
/**
* Queries all photos for the account. Chooses photos which are not included to the collection of
* photos from albums.
*
* @param authData authentication information
* @param paginationData pagination information to use for subsequent calls.
*/
private ExportResult<PhotosContainerResource> requestNonAlbumPhotos(TokensAndUrlAuthData authData, PaginationData paginationData, UUID jobId) throws IOException {
int page = paginationData == null ? 0 : ((IntPaginationToken) paginationData).getStart();
String url = format(ALL_PHOTOS_URL_TEMPLATE, page);
Set<PhotoAlbum> albums = new HashSet<>();
List<PhotoModel> photos = new ArrayList<>();
List<Map<String, Object>> items = requestData(authData, url);
boolean hasMore = (items != null && items.size() != 0);
for (Map<String, Object> item : items) {
String photoId = (String) item.get("id");
// Select photos which are not included to the collection of retrieved album photos
if (!albumPhotos.contains(photoId)) {
PhotoModel photoModel = new PhotoModel((String) item.get("name"), (String) item.get("link"), (String) item.get("description"), (String) item.get("type"), (String) item.get("id"), DEFAULT_ALBUM_ID, true);
photos.add(photoModel);
InputStream inputStream = getImageAsStream(photoModel.getFetchableUrl());
jobStore.create(jobId, photoModel.getFetchableUrl(), inputStream);
}
}
if (!containsNonAlbumPhotos && photos.size() > 0) {
// Add album for non-album photos
albums.add(new PhotoAlbum(DEFAULT_ALBUM_ID, "Non-album photos", "Contains non-album photos"));
// Make sure album will not be added multiply times on subsequent calls
containsNonAlbumPhotos = true;
}
PaginationData newPage = null;
if (hasMore) {
newPage = new IntPaginationToken(page + 1);
monitor.info(() -> format("added non-album photos, size: %s", photos.size()));
}
PhotosContainerResource photosContainerResource = new PhotosContainerResource(albums, photos);
ContinuationData continuationData = new ContinuationData(newPage);
ExportResult.ResultType resultType = ExportResult.ResultType.CONTINUE;
if (newPage == null) {
resultType = ExportResult.ResultType.END;
}
return new ExportResult<>(resultType, photosContainerResource, continuationData);
}
use of org.datatransferproject.spi.transfer.provider.ExportResult in project data-transfer-project by google.
the class ImgurPhotosExporter method requestPhotos.
/**
* Exports photos from album.
*
* <p>This request doesn't support pages so it retrieves all photos at once for each album.
*
* @param authData authentication information
* @param resource contains album id
* @param paginationData pagination information to use for subsequent calls.
*/
private ExportResult<PhotosContainerResource> requestPhotos(TokensAndUrlAuthData authData, IdOnlyContainerResource resource, PaginationData paginationData, UUID jobId) throws IOException {
String albumId = resource.getId();
// Means all other albums with photos are imported, so non-album photos can be determined
if (albumId.equals(DEFAULT_ALBUM_ID)) {
return requestNonAlbumPhotos(authData, paginationData, jobId);
}
String url = format(ALBUM_PHOTOS_URL_TEMPLATE, albumId);
List<PhotoModel> photos = new ArrayList<>();
List<Map<String, Object>> items = requestData(authData, url);
for (Map<String, Object> item : items) {
PhotoModel photoModel = new PhotoModel((String) item.get("name"), (String) item.get("link"), (String) item.get("description"), (String) item.get("type"), (String) item.get("id"), albumId, true);
photos.add(photoModel);
InputStream inputStream = getImageAsStream(photoModel.getFetchableUrl());
jobStore.create(jobId, photoModel.getFetchableUrl(), inputStream);
// Save id of each album photo for finding non-album photos later
albumPhotos.add((String) item.get("id"));
}
// This request doesn't support pages
ExportResult.ResultType resultType = ExportResult.ResultType.END;
if (photos.size() > 0) {
monitor.info(() -> format("added albumPhotos, size: %s", photos.size()));
}
PhotosContainerResource photosContainerResource = new PhotosContainerResource(null, photos);
return new ExportResult<>(resultType, photosContainerResource, new ContinuationData(null));
}
use of org.datatransferproject.spi.transfer.provider.ExportResult in project data-transfer-project by google.
the class KoofrPhotosExporter method export.
@Override
public ExportResult<PhotosContainerResource> export(UUID jobId, TokensAndUrlAuthData authData, Optional<ExportInformation> exportInformation) throws CopyExceptionWithFailureReason {
Preconditions.checkNotNull(authData);
KoofrClient koofrClient = koofrClientFactory.create(authData);
KoofrMediaExport export = new KoofrMediaExport(koofrClient, monitor);
try {
export.export();
List<PhotoAlbum> exportAlbums = export.getPhotoAlbums();
List<PhotoModel> exportPhotos = export.getPhotos();
PhotosContainerResource containerResource = new PhotosContainerResource(exportAlbums, exportPhotos);
return new ExportResult<>(ExportResult.ResultType.END, containerResource, null);
} catch (IOException e) {
return new ExportResult<>(e);
}
}
use of org.datatransferproject.spi.transfer.provider.ExportResult in project data-transfer-project by google.
the class KoofrVideosExporter method export.
@Override
public ExportResult<VideosContainerResource> export(UUID jobId, TokensAndUrlAuthData authData, Optional<ExportInformation> exportInformation) throws CopyExceptionWithFailureReason {
Preconditions.checkNotNull(authData);
KoofrClient koofrClient = koofrClientFactory.create(authData);
KoofrMediaExport export = new KoofrMediaExport(koofrClient, monitor);
try {
export.export();
List<VideoAlbum> exportAlbums = export.getVideoAlbums();
List<VideoModel> exportVideos = export.getVideos();
VideosContainerResource containerResource = new VideosContainerResource(exportAlbums, exportVideos);
return new ExportResult<>(ExportResult.ResultType.END, containerResource, null);
} catch (IOException e) {
return new ExportResult<>(e);
}
}
use of org.datatransferproject.spi.transfer.provider.ExportResult in project data-transfer-project by google.
the class TwitterPhotosExporter method export.
@Override
public ExportResult<PhotosContainerResource> export(UUID jobId, TokenSecretAuthData authData, Optional<ExportInformation> exportInformation) {
Twitter twitterApi = TwitterApiWrapper.getInstance(appCredentials, authData);
int pageNumber = 1;
if (exportInformation.isPresent()) {
IntPaginationToken pageToken = (IntPaginationToken) exportInformation.get().getPaginationData();
if (pageToken != null && pageToken.getStart() > 1) {
pageNumber = pageToken.getStart();
}
}
Paging paging = new Paging(pageNumber, PAGE_SIZE);
try {
String page = "" + pageNumber;
long id = twitterApi.getId();
monitor.debug(() -> format("Getting tweets for %s (page %s)", id, page));
ResponseList<Status> statuses = twitterApi.getUserTimeline(id, paging);
List<PhotoModel> photos = new ArrayList<>();
for (Status status : statuses) {
boolean hasMedia = status.getMediaEntities().length > 0;
if (hasMedia && !status.isRetweet()) {
for (MediaEntity mediaEntity : status.getMediaEntities()) {
photos.add(new PhotoModel("Twitter Photo " + mediaEntity.getId(), mediaEntity.getMediaURL(), status.getText(), null, Long.toString(status.getId()), null, false));
}
}
}
boolean moreData = statuses.size() == PAGE_SIZE;
return new ExportResult<>(moreData ? ResultType.CONTINUE : ResultType.END, new PhotosContainerResource(null, photos), moreData ? new ContinuationData(new IntPaginationToken(pageNumber + 1)) : null);
} catch (TwitterException e) {
return new ExportResult<>(e);
}
}
Aggregations