Search in sources :

Example 1 with FacebookGraphException

use of com.restfb.exception.FacebookGraphException 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 2 with FacebookGraphException

use of com.restfb.exception.FacebookGraphException 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)

Aggregations

FacebookGraphException (com.restfb.exception.FacebookGraphException)2 ArrayList (java.util.ArrayList)2 ExportResult (org.datatransferproject.spi.transfer.provider.ExportResult)2 ContinuationData (org.datatransferproject.spi.transfer.types.ContinuationData)2 PaginationData (org.datatransferproject.types.common.PaginationData)2 StringPaginationToken (org.datatransferproject.types.common.StringPaginationToken)2 Photo (com.restfb.types.Photo)1 Video (com.restfb.types.Video)1 IOException (java.io.IOException)1 PhotoModel (org.datatransferproject.types.common.models.photos.PhotoModel)1 PhotosContainerResource (org.datatransferproject.types.common.models.photos.PhotosContainerResource)1 VideoModel (org.datatransferproject.types.common.models.videos.VideoModel)1 VideosContainerResource (org.datatransferproject.types.common.models.videos.VideosContainerResource)1