Search in sources :

Example 1 with ExportResult

use of org.datatransferproject.spi.transfer.provider.ExportResult in project data-transfer-project by google.

the class FacebookPhotosExporter method exportAlbums.

private ExportResult<PhotosContainerResource> exportAlbums(TokensAndUrlAuthData authData, Optional<StringPaginationToken> paginationData) throws CopyExceptionWithFailureReason {
    Optional<String> paginationToken = stripTokenPrefix(paginationData, ALBUM_TOKEN_PREFIX);
    // Get albums
    Connection<Album> connection = getOrCreatePhotosInterface(authData).getAlbums(paginationToken);
    PaginationData nextPageData = null;
    String token = connection.getAfterCursor();
    if (!Strings.isNullOrEmpty(token)) {
        nextPageData = new StringPaginationToken(ALBUM_TOKEN_PREFIX + token);
    }
    ContinuationData continuationData = new ContinuationData(nextPageData);
    List<Album> albums = connection.getData();
    if (albums.isEmpty()) {
        return new ExportResult<>(ExportResult.ResultType.END, null, null);
    }
    ArrayList<PhotoAlbum> exportAlbums = new ArrayList<>();
    for (Album album : albums) {
        exportAlbums.add(new PhotoAlbum(album.getId(), album.getName(), album.getDescription()));
        continuationData.addContainerResource(new IdOnlyContainerResource(album.getId()));
    }
    return new ExportResult<>(ExportResult.ResultType.CONTINUE, new PhotosContainerResource(exportAlbums, null), continuationData);
}
Also used : PaginationData(org.datatransferproject.types.common.PaginationData) ArrayList(java.util.ArrayList) PhotoAlbum(org.datatransferproject.types.common.models.photos.PhotoAlbum) Album(com.restfb.types.Album) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) IdOnlyContainerResource(org.datatransferproject.types.common.models.IdOnlyContainerResource) PhotoAlbum(org.datatransferproject.types.common.models.photos.PhotoAlbum) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult)

Example 2 with ExportResult

use of org.datatransferproject.spi.transfer.provider.ExportResult in project data-transfer-project by google.

the class FacebookPhotosExporter method exportPhotos.

private ExportResult<PhotosContainerResource> exportPhotos(UUID jobId, TokensAndUrlAuthData authData, IdOnlyContainerResource containerResource, Optional<StringPaginationToken> paginationData) throws CopyExceptionWithFailureReason {
    Optional<String> paginationToken = stripTokenPrefix(paginationData, PHOTO_TOKEN_PREFIX);
    String albumId = containerResource.getId();
    try {
        Connection<Photo> photoConnection = getOrCreatePhotosInterface(authData).getPhotos(albumId, paginationToken);
        List<Photo> photos = photoConnection.getData();
        if (photos.isEmpty()) {
            return new ExportResult<>(ExportResult.ResultType.END, null);
        }
        ArrayList<PhotoModel> exportPhotos = new ArrayList<>();
        for (Photo photo : photos) {
            final String url = photo.getImages().get(0).getSource();
            final String fbid = photo.getId();
            if (null == url || url.isEmpty()) {
                monitor.severe(() -> String.format("Source was missing or empty for photo %s", fbid));
                continue;
            }
            boolean photoWasGarbage;
            try {
                photoWasGarbage = modifyExifAndStorePhoto(jobId, photo, url, photo.getId());
            } catch (IOException e) {
                monitor.info(() -> String.format("Error while modifying exif or storing photo %s", fbid), e);
                photoWasGarbage = true;
            }
            if (photoWasGarbage) {
                continue;
            }
            exportPhotos.add(new PhotoModel(String.format("%s.jpg", photo.getId()), // store and the url is too long for that.
            photo.getId(), photo.getName(), "image/jpg", photo.getId(), albumId, true, photo.getCreatedTime()));
        }
        String token = photoConnection.getAfterCursor();
        if (Strings.isNullOrEmpty(token)) {
            return new ExportResult<>(ExportResult.ResultType.END, new PhotosContainerResource(null, exportPhotos));
        } else {
            PaginationData nextPageData = new StringPaginationToken(PHOTO_TOKEN_PREFIX + token);
            ContinuationData continuationData = new ContinuationData(nextPageData);
            return new ExportResult<>(ExportResult.ResultType.CONTINUE, new PhotosContainerResource(null, exportPhotos), continuationData);
        }
    } catch (FacebookGraphException e) {
        String message = e.getMessage();
        // In such case, we should skip this object and continue with the rest of the transfer.
        if (message != null && message.contains("code 100, subcode 33")) {
            monitor.info(() -> "Cannot find photos to export, skipping to the next bunch", e);
            return new ExportResult<>(ExportResult.ResultType.END, null);
        }
        throw e;
    }
}
Also used : PaginationData(org.datatransferproject.types.common.PaginationData) PhotoModel(org.datatransferproject.types.common.models.photos.PhotoModel) ArrayList(java.util.ArrayList) Photo(com.restfb.types.Photo) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) IOException(java.io.IOException) PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) FacebookGraphException(com.restfb.exception.FacebookGraphException) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken)

Example 3 with ExportResult

use of org.datatransferproject.spi.transfer.provider.ExportResult in project data-transfer-project by google.

the class FacebookVideosExporter method exportVideos.

private ExportResult<VideosContainerResource> exportVideos(TokensAndUrlAuthData authData, Optional<StringPaginationToken> paginationData) throws CopyExceptionWithFailureReason {
    Optional<String> paginationToken = paginationData.map(StringPaginationToken::getToken);
    try {
        Connection<Video> videoConnection = getOrCreateVideosInterface(authData).getVideos(paginationToken);
        List<Video> videos = videoConnection.getData();
        if (videos.isEmpty()) {
            return new ExportResult<>(ExportResult.ResultType.END, null);
        }
        ArrayList<VideoModel> exportVideos = new ArrayList<>();
        for (Video video : videos) {
            final String url = video.getSource();
            final String fbid = video.getId();
            if (null == url || url.isEmpty()) {
                monitor.severe(() -> String.format("Source was missing or empty for video %s", fbid));
                continue;
            }
            exportVideos.add(new VideoModel(String.format("%s.mp4", fbid), url, video.getDescription(), "video/mp4", fbid, null, false));
        }
        String token = videoConnection.getAfterCursor();
        if (Strings.isNullOrEmpty(token)) {
            return new ExportResult<>(ExportResult.ResultType.END, new VideosContainerResource(null, exportVideos));
        } else {
            PaginationData nextPageData = new StringPaginationToken(token);
            ContinuationData continuationData = new ContinuationData(nextPageData);
            return new ExportResult<>(ExportResult.ResultType.CONTINUE, new VideosContainerResource(null, exportVideos), continuationData);
        }
    } catch (FacebookGraphException e) {
        String message = e.getMessage();
        // In such case, we should skip this object and continue with the rest of the transfer.
        if (message != null && message.contains("code 100, subcode 33")) {
            monitor.info(() -> "Cannot find videos to export, skipping to the next bunch", e);
            return new ExportResult<>(ExportResult.ResultType.END, null);
        }
        throw e;
    }
}
Also used : PaginationData(org.datatransferproject.types.common.PaginationData) ArrayList(java.util.ArrayList) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) VideoModel(org.datatransferproject.types.common.models.videos.VideoModel) Video(com.restfb.types.Video) VideosContainerResource(org.datatransferproject.types.common.models.videos.VideosContainerResource) FacebookGraphException(com.restfb.exception.FacebookGraphException) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult)

Example 4 with ExportResult

use of org.datatransferproject.spi.transfer.provider.ExportResult in project data-transfer-project by google.

the class SolidContactsExport method export.

@Override
public ExportResult<ContactsModelWrapper> export(UUID jobId, CookiesAndUrlAuthData authData, Optional<ExportInformation> exportInformation) throws Exception {
    if (exportInformation.isPresent()) {
        throw new IllegalStateException("Currently solid doesn't support paged exports");
    }
    checkState(authData.getCookies().size() == 1, "Exactly 1 cookie expected: %s", authData.getCookies());
    SolidUtilities solidUtilities = new SolidUtilities(authData.getCookies().get(0));
    String url = authData.getUrl();
    List<List<VCard>> vCards = explore(url, solidUtilities);
    // TODO: This flattens all the address books together, which isn't perfect.
    List<VCard> allCards = new ArrayList<>();
    vCards.forEach(allCards::addAll);
    return new ExportResult<>(ResultType.END, new ContactsModelWrapper(Ezvcard.write(allCards).go()));
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ContactsModelWrapper(org.datatransferproject.types.common.models.contacts.ContactsModelWrapper) VCard(ezvcard.VCard) SolidUtilities(org.datatransferproject.transfer.solid.SolidUtilities) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult)

Example 5 with ExportResult

use of org.datatransferproject.spi.transfer.provider.ExportResult in project data-transfer-project by google.

the class RememberTheMilkTasksExporter method exportTask.

private ExportResult exportTask(RememberTheMilkService service, IdOnlyContainerResource resource) {
    String oldListId = resource.getId();
    GetListResponse oldList = null;
    try {
        oldList = service.getList(oldListId);
    } catch (IOException e) {
        return new ExportResult(e);
    }
    List<TaskList> taskLists = oldList.tasks.list;
    List<TaskModel> tasks = new ArrayList<>();
    for (TaskList taskList : taskLists) {
        if (taskList.taskseries != null) {
            for (TaskSeries taskSeries : taskList.taskseries) {
                // TODO: figure out what to do with notes
                String notesStr = taskSeries.notes == null ? "" : taskSeries.notes.toString();
                for (Task task : taskSeries.tasks) {
                    // TODO: How to handle case with multiple tasks in a series?  Is this good enough?
                    Instant completedTime = null;
                    Instant dueTime = null;
                    if (task.completed != null && !Strings.isNullOrEmpty(task.completed)) {
                        completedTime = Instant.parse(task.completed);
                    }
                    if (task.due != null && !Strings.isNullOrEmpty(task.due)) {
                        dueTime = Instant.parse(task.due);
                    }
                    tasks.add(new TaskModel(oldListId, taskSeries.name, notesStr, completedTime, dueTime));
                }
            }
        }
    }
    TaskContainerResource taskContainerResource = new TaskContainerResource(null, tasks);
    // TODO: what do we do with pagination data?
    return new ExportResult(ResultType.CONTINUE, taskContainerResource, null);
}
Also used : GetListResponse(org.datatransferproject.transfer.rememberthemilk.model.tasks.GetListResponse) Task(org.datatransferproject.transfer.rememberthemilk.model.tasks.Task) TaskList(org.datatransferproject.transfer.rememberthemilk.model.tasks.TaskList) Instant(java.time.Instant) ArrayList(java.util.ArrayList) IOException(java.io.IOException) TaskContainerResource(org.datatransferproject.types.common.models.tasks.TaskContainerResource) TaskSeries(org.datatransferproject.transfer.rememberthemilk.model.tasks.TaskSeries) TaskModel(org.datatransferproject.types.common.models.tasks.TaskModel) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult)

Aggregations

ExportResult (org.datatransferproject.spi.transfer.provider.ExportResult)44 ContinuationData (org.datatransferproject.spi.transfer.types.ContinuationData)33 ArrayList (java.util.ArrayList)26 IOException (java.io.IOException)23 StringPaginationToken (org.datatransferproject.types.common.StringPaginationToken)22 PhotosContainerResource (org.datatransferproject.types.common.models.photos.PhotosContainerResource)21 PaginationData (org.datatransferproject.types.common.PaginationData)19 IdOnlyContainerResource (org.datatransferproject.types.common.models.IdOnlyContainerResource)18 PhotoAlbum (org.datatransferproject.types.common.models.photos.PhotoAlbum)16 PhotoModel (org.datatransferproject.types.common.models.photos.PhotoModel)16 ResultType (org.datatransferproject.spi.transfer.provider.ExportResult.ResultType)14 List (java.util.List)11 Optional (java.util.Optional)9 UUID (java.util.UUID)9 Collectors (java.util.stream.Collectors)8 VisibleForTesting (com.google.common.annotations.VisibleForTesting)7 ImmutableList (com.google.common.collect.ImmutableList)7 Collection (java.util.Collection)6 TokensAndUrlAuthData (org.datatransferproject.types.transfer.auth.TokensAndUrlAuthData)6 Before (org.junit.Before)6