Search in sources :

Example 11 with PaginationData

use of org.datatransferproject.types.common.PaginationData 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 (Strings.isNullOrEmpty(photoSetId)) {
            RequestContext.getRequestContext().setExtras(EXTRAS);
            perUserRateLimiter.acquire();
            photoSetList = photosInterface.getNotInSet(PHOTO_PER_PAGE, page);
            RequestContext.getRequestContext().setExtras(ImmutableList.of());
        } else {
            perUserRateLimiter.acquire();
            photoSetList = photosetsInterface.getPhotos(photoSetId, ImmutableSet.copyOf(EXTRAS), 0, PHOTO_PER_PAGE, page);
        }
    } catch (FlickrException e) {
        return new ExportResult<>(e);
    }
    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 : FlickrException(com.flickr4java.flickr.FlickrException) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult) PhotoAlbum(org.datatransferproject.types.common.models.photos.PhotoAlbum) PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) Photoset(com.flickr4java.flickr.photosets.Photoset) AppCredentials(org.datatransferproject.types.transfer.auth.AppCredentials) REST(com.flickr4java.flickr.REST) TransferServiceConfig(org.datatransferproject.types.transfer.serviceconfig.TransferServiceConfig) RateLimiter(com.google.common.util.concurrent.RateLimiter) ArrayList(java.util.ArrayList) ExportInformation(org.datatransferproject.types.common.ExportInformation) Photo(com.flickr4java.flickr.photos.Photo) Strings(com.google.common.base.Strings) ImmutableList(com.google.common.collect.ImmutableList) PhotoModel(org.datatransferproject.types.common.models.photos.PhotoModel) IntPaginationToken(org.datatransferproject.types.common.IntPaginationToken) Flickr(com.flickr4java.flickr.Flickr) PhotoList(com.flickr4java.flickr.photos.PhotoList) Auth(com.flickr4java.flickr.auth.Auth) ImmutableSet(com.google.common.collect.ImmutableSet) PhotosInterface(com.flickr4java.flickr.photos.PhotosInterface) Collection(java.util.Collection) ResultType(org.datatransferproject.spi.transfer.provider.ExportResult.ResultType) Exporter(org.datatransferproject.spi.transfer.provider.Exporter) UUID(java.util.UUID) PaginationData(org.datatransferproject.types.common.PaginationData) Collectors(java.util.stream.Collectors) IdOnlyContainerResource(org.datatransferproject.types.common.models.IdOnlyContainerResource) Photosets(com.flickr4java.flickr.photosets.Photosets) List(java.util.List) AuthData(org.datatransferproject.types.transfer.auth.AuthData) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) VisibleForTesting(com.google.common.annotations.VisibleForTesting) RequestContext(com.flickr4java.flickr.RequestContext) PhotosetsInterface(com.flickr4java.flickr.photosets.PhotosetsInterface) IntPaginationToken(org.datatransferproject.types.common.IntPaginationToken) PaginationData(org.datatransferproject.types.common.PaginationData) FlickrException(com.flickr4java.flickr.FlickrException) PhotoModel(org.datatransferproject.types.common.models.photos.PhotoModel) Photo(com.flickr4java.flickr.photos.Photo) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) ResultType(org.datatransferproject.spi.transfer.provider.ExportResult.ResultType) PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult)

Example 12 with PaginationData

use of org.datatransferproject.types.common.PaginationData in project data-transfer-project by google.

the class GoogleCalendarExporter method export.

@Override
public ExportResult<CalendarContainerResource> export(UUID jobId, TokensAndUrlAuthData authData, Optional<ExportInformation> exportInformation) {
    if (!exportInformation.isPresent()) {
        return exportCalendars(authData, Optional.empty());
    } else {
        StringPaginationToken paginationToken = (StringPaginationToken) exportInformation.get().getPaginationData();
        if (paginationToken != null && paginationToken.getToken().startsWith(CALENDAR_TOKEN_PREFIX)) {
            // Next thing to export is more calendars
            return exportCalendars(authData, Optional.of(paginationToken));
        } else {
            // Next thing to export is events
            IdOnlyContainerResource idOnlyContainerResource = (IdOnlyContainerResource) exportInformation.get().getContainerResource();
            Optional<PaginationData> pageData = Optional.ofNullable(paginationToken);
            return getCalendarEvents(authData, idOnlyContainerResource.getId(), pageData);
        }
    }
}
Also used : PaginationData(org.datatransferproject.types.common.PaginationData) IdOnlyContainerResource(org.datatransferproject.types.common.models.IdOnlyContainerResource) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken)

Example 13 with PaginationData

use of org.datatransferproject.types.common.PaginationData in project data-transfer-project by google.

the class GoogleCalendarExporter method exportCalendars.

private ExportResult<CalendarContainerResource> exportCalendars(TokensAndUrlAuthData authData, Optional<PaginationData> pageData) {
    Calendar.CalendarList.List listRequest;
    CalendarList listResult;
    // Get calendar information
    try {
        listRequest = getOrCreateCalendarInterface(authData).calendarList().list();
        if (pageData.isPresent()) {
            StringPaginationToken paginationToken = (StringPaginationToken) pageData.get();
            Preconditions.checkState(paginationToken.getToken().startsWith(CALENDAR_TOKEN_PREFIX), "Token is not applicable");
            listRequest.setPageToken(((StringPaginationToken) pageData.get()).getToken().substring(CALENDAR_TOKEN_PREFIX.length()));
        }
        listResult = listRequest.execute();
    } catch (IOException e) {
        return new ExportResult<>(e);
    }
    // Set up continuation data
    PaginationData nextPageData = null;
    if (listResult.getNextPageToken() != null) {
        nextPageData = new StringPaginationToken(CALENDAR_TOKEN_PREFIX + listResult.getNextPageToken());
    }
    ContinuationData continuationData = new ContinuationData(nextPageData);
    // Process calendar list
    List<CalendarModel> calendarModels = new ArrayList<>(listResult.getItems().size());
    for (CalendarListEntry calendarData : listResult.getItems()) {
        CalendarModel model = convertToCalendarModel(calendarData);
        continuationData.addContainerResource(new IdOnlyContainerResource(calendarData.getId()));
        calendarModels.add(model);
    }
    CalendarContainerResource calendarContainerResource = new CalendarContainerResource(calendarModels, null);
    // Get result type
    ExportResult.ResultType resultType = ResultType.CONTINUE;
    if (calendarModels.isEmpty()) {
        resultType = ResultType.END;
    }
    return new ExportResult<>(resultType, calendarContainerResource, continuationData);
}
Also used : PaginationData(org.datatransferproject.types.common.PaginationData) ArrayList(java.util.ArrayList) ResultType(org.datatransferproject.spi.transfer.provider.ExportResult.ResultType) CalendarModel(org.datatransferproject.types.common.models.calendar.CalendarModel) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) IOException(java.io.IOException) CalendarContainerResource(org.datatransferproject.types.common.models.calendar.CalendarContainerResource) CalendarListEntry(com.google.api.services.calendar.model.CalendarListEntry) IdOnlyContainerResource(org.datatransferproject.types.common.models.IdOnlyContainerResource) CalendarList(com.google.api.services.calendar.model.CalendarList) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult)

Example 14 with PaginationData

use of org.datatransferproject.types.common.PaginationData in project data-transfer-project by google.

the class GoogleMailExporter method export.

@Override
public ExportResult<MailContainerResource> export(UUID id, TokensAndUrlAuthData authData, Optional<ExportInformation> exportInformation) {
    // Create a new gmail service for the authorized user
    Gmail gmail = getOrCreateGmail(authData);
    Messages.List request = null;
    try {
        request = gmail.users().messages().list(USER).setMaxResults(PAGE_SIZE);
    } catch (IOException e) {
        return new ExportResult<>(e);
    }
    if (exportInformation.isPresent() && exportInformation.get().getPaginationData() != null) {
        request.setPageToken(((StringPaginationToken) exportInformation.get().getPaginationData()).getToken());
    }
    ListMessagesResponse response = null;
    try {
        response = request.execute();
    } catch (IOException e) {
        return new ExportResult<>(e);
    }
    List<MailMessageModel> results = new ArrayList<>(response.getMessages().size());
    // as we can't store all the mail messages in memory at once.
    for (Message listMessage : response.getMessages()) {
        Message getResponse = null;
        try {
            getResponse = gmail.users().messages().get(USER, listMessage.getId()).setFormat("raw").execute();
        } catch (IOException e) {
            return new ExportResult<>(e);
        }
        // TODO: note this doesn't transfer things like labels
        results.add(new MailMessageModel(getResponse.getRaw(), getResponse.getLabelIds()));
    }
    PaginationData newPage = null;
    ResultType resultType = ResultType.END;
    if (response.getNextPageToken() != null) {
        newPage = new StringPaginationToken(response.getNextPageToken());
        resultType = ResultType.CONTINUE;
    }
    MailContainerResource mailContainerResource = new MailContainerResource(null, results);
    return new ExportResult<>(resultType, mailContainerResource, new ContinuationData(newPage));
}
Also used : Messages(com.google.api.services.gmail.Gmail.Users.Messages) PaginationData(org.datatransferproject.types.common.PaginationData) Message(com.google.api.services.gmail.model.Message) Gmail(com.google.api.services.gmail.Gmail) MailContainerResource(org.datatransferproject.types.common.models.mail.MailContainerResource) ArrayList(java.util.ArrayList) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) IOException(java.io.IOException) ResultType(org.datatransferproject.spi.transfer.provider.ExportResult.ResultType) ListMessagesResponse(com.google.api.services.gmail.model.ListMessagesResponse) MailMessageModel(org.datatransferproject.types.common.models.mail.MailMessageModel) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult)

Example 15 with PaginationData

use of org.datatransferproject.types.common.PaginationData in project data-transfer-project by google.

the class GooglePhotosExporter method exportAlbums.

/**
 * Note: not all accounts have albums to return. In that case, we just return an empty list of
 * albums instead of trying to iterate through a null list.
 */
@VisibleForTesting
ExportResult<PhotosContainerResource> exportAlbums(TokensAndUrlAuthData authData, Optional<PaginationData> paginationData, UUID jobId) throws IOException, InvalidTokenException, PermissionDeniedException {
    Optional<String> paginationToken = Optional.empty();
    if (paginationData.isPresent()) {
        String token = ((StringPaginationToken) paginationData.get()).getToken();
        Preconditions.checkArgument(token.startsWith(ALBUM_TOKEN_PREFIX), "Invalid pagination token " + token);
        paginationToken = Optional.of(token.substring(ALBUM_TOKEN_PREFIX.length()));
    }
    AlbumListResponse albumListResponse;
    albumListResponse = getOrCreatePhotosInterface(authData).listAlbums(paginationToken);
    PaginationData nextPageData;
    String token = albumListResponse.getNextPageToken();
    List<PhotoAlbum> albums = new ArrayList<>();
    GoogleAlbum[] googleAlbums = albumListResponse.getAlbums();
    if (Strings.isNullOrEmpty(token)) {
        nextPageData = new StringPaginationToken(PHOTO_TOKEN_PREFIX);
    } else {
        nextPageData = new StringPaginationToken(ALBUM_TOKEN_PREFIX + token);
    }
    ContinuationData continuationData = new ContinuationData(nextPageData);
    if (googleAlbums != null && googleAlbums.length > 0) {
        for (GoogleAlbum googleAlbum : googleAlbums) {
            // Add album info to list so album can be recreated later
            PhotoAlbum photoAlbum = new PhotoAlbum(googleAlbum.getId(), googleAlbum.getTitle(), null);
            albums.add(photoAlbum);
            monitor.debug(() -> String.format("%s: Google Photos exporting album: %s", jobId, photoAlbum.getId()));
            // Add album id to continuation data
            continuationData.addContainerResource(new IdOnlyContainerResource(googleAlbum.getId()));
        }
    }
    ResultType resultType = ResultType.CONTINUE;
    PhotosContainerResource containerResource = new PhotosContainerResource(albums, null);
    return new ExportResult<>(resultType, containerResource, continuationData);
}
Also used : AlbumListResponse(org.datatransferproject.datatransfer.google.mediaModels.AlbumListResponse) PaginationData(org.datatransferproject.types.common.PaginationData) ArrayList(java.util.ArrayList) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) ResultType(org.datatransferproject.spi.transfer.provider.ExportResult.ResultType) PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) IdOnlyContainerResource(org.datatransferproject.types.common.models.IdOnlyContainerResource) PhotoAlbum(org.datatransferproject.types.common.models.photos.PhotoAlbum) GoogleAlbum(org.datatransferproject.datatransfer.google.mediaModels.GoogleAlbum) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

PaginationData (org.datatransferproject.types.common.PaginationData)30 ContinuationData (org.datatransferproject.spi.transfer.types.ContinuationData)23 StringPaginationToken (org.datatransferproject.types.common.StringPaginationToken)18 ExportResult (org.datatransferproject.spi.transfer.provider.ExportResult)17 IdOnlyContainerResource (org.datatransferproject.types.common.models.IdOnlyContainerResource)15 ArrayList (java.util.ArrayList)12 ResultType (org.datatransferproject.spi.transfer.provider.ExportResult.ResultType)11 ExportInformation (org.datatransferproject.types.common.ExportInformation)11 PhotosContainerResource (org.datatransferproject.types.common.models.photos.PhotosContainerResource)11 Test (org.junit.Test)9 InOrder (org.mockito.InOrder)8 VisibleForTesting (com.google.common.annotations.VisibleForTesting)7 IOException (java.io.IOException)7 PhotoAlbum (org.datatransferproject.types.common.models.photos.PhotoAlbum)7 ImmutableList (com.google.common.collect.ImmutableList)5 PhotoModel (org.datatransferproject.types.common.models.photos.PhotoModel)5 IntPaginationToken (org.datatransferproject.types.common.IntPaginationToken)4 ContainerResource (org.datatransferproject.types.common.models.ContainerResource)4 CalendarContainerResource (org.datatransferproject.types.common.models.calendar.CalendarContainerResource)4 FlickrException (com.flickr4java.flickr.FlickrException)3