use of org.datatransferproject.types.common.IntPaginationToken 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);
}
}
use of org.datatransferproject.types.common.IntPaginationToken in project data-transfer-project by google.
the class FlickrPhotosExporterTest method exportAlbumInitial.
@Test
public void exportAlbumInitial() 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);
// setup photoset
Photoset photoset = FlickrTestUtils.initializePhotoset("photosetId", "title", "description");
// setup photoset list (aka album view)
int page = 1;
Photosets photosetsList = new Photosets();
photosetsList.setPage(page);
photosetsList.setPages(page + 1);
photosetsList.setPhotosets(Collections.singletonList(photoset));
when(photosetsInterface.getList(anyString(), anyInt(), anyInt(), anyString())).thenReturn(photosetsList);
// run test
FlickrPhotosExporter exporter = new FlickrPhotosExporter(flickr, TransferServiceConfig.getDefaultInstance());
AuthData authData = new TokenSecretAuthData("token", "secret");
ExportResult<PhotosContainerResource> result = exporter.export(UUID.randomUUID(), authData, Optional.empty());
// make sure album and photo information is correct
assertThat(result.getExportedData().getPhotos()).isEmpty();
Collection<PhotoAlbum> albums = result.getExportedData().getAlbums();
assertThat(albums.size()).isEqualTo(1);
assertThat(albums).containsExactly(new PhotoAlbum("photosetId", "title", "description"));
// check continuation information
ContinuationData continuationData = (ContinuationData) result.getContinuationData();
assertThat(continuationData.getPaginationData()).isInstanceOf(IntPaginationToken.class);
assertThat(((IntPaginationToken) continuationData.getPaginationData()).getStart()).isEqualTo(page + 1);
Collection<? extends ContainerResource> subResources = continuationData.getContainerResources();
assertThat(subResources.size()).isEqualTo(1);
assertThat(subResources).containsExactly(new IdOnlyContainerResource("photosetId"));
}
use of org.datatransferproject.types.common.IntPaginationToken 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, TransferServiceConfig.getDefaultInstance());
ExportResult<PhotosContainerResource> result = exporter.export(UUID.randomUUID(), new TokenSecretAuthData("token", "secret"), Optional.of(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);
}
use of org.datatransferproject.types.common.IntPaginationToken in project data-transfer-project by google.
the class FlickrPhotosExporter method getAlbums.
private ExportResult<PhotosContainerResource> getAlbums(PaginationData paginationData, Auth auth) {
ImmutableList.Builder<PhotoAlbum> albumBuilder = ImmutableList.builder();
List<IdOnlyContainerResource> subResources = new ArrayList<>();
int page = paginationData == null ? 1 : ((IntPaginationToken) paginationData).getStart();
Photosets photoSetList;
try {
perUserRateLimiter.acquire();
photoSetList = photosetsInterface.getList(auth.getUser().getId(), PHOTO_SETS_PER_PAGE, page, PHOTOSET_EXTRAS);
} catch (FlickrException e) {
return new ExportResult<>(e);
}
for (Photoset photoSet : photoSetList.getPhotosets()) {
// Saving data to the album allows the target service to recreate the album structure
albumBuilder.add(new PhotoAlbum(photoSet.getId(), photoSet.getTitle(), photoSet.getDescription()));
// Adding subresources tells the framework to recall export to get all the photos
subResources.add(new IdOnlyContainerResource(photoSet.getId()));
}
PaginationData newPage = null;
boolean hasMore = photoSetList.getPage() != photoSetList.getPages() && !photoSetList.getPhotosets().isEmpty();
if (hasMore) {
newPage = new IntPaginationToken(page + 1);
} else {
// No more albums to get, add a resource for albumless items
subResources.add(new IdOnlyContainerResource(""));
}
PhotosContainerResource photosContainerResource = new PhotosContainerResource(albumBuilder.build(), null);
ContinuationData continuationData = new ContinuationData(newPage);
subResources.forEach(resource -> continuationData.addContainerResource(resource));
// Get result type
ResultType resultType = ResultType.CONTINUE;
if (newPage == null) {
resultType = ResultType.END;
}
return new ExportResult<>(resultType, photosContainerResource, continuationData);
}
Aggregations