Search in sources :

Example 1 with Photo

use of com.restfb.types.Photo 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 Photo

use of com.restfb.types.Photo in project data-transfer-project by google.

the class FacebookPhotosExporterTest method setUp.

@Before
public void setUp() throws IOException {
    FacebookPhotosInterface photosInterface = mock(FacebookPhotosInterface.class);
    // Set up example album
    Album album = new Album();
    album.setId(ALBUM_ID);
    album.setName(ALBUM_NAME);
    album.setDescription(ALBUM_DESCRIPTION);
    ArrayList<Album> albums = new ArrayList<>();
    albums.add(album);
    @SuppressWarnings("unchecked") Connection<Album> albumConnection = mock(Connection.class);
    when(photosInterface.getAlbums(Mockito.any())).thenReturn(albumConnection);
    when(albumConnection.getData()).thenReturn(albums);
    // Set up example photo
    Photo photo = new Photo();
    photo.setId(PHOTO_ID);
    photo.setCreatedTime(PHOTO_TIME);
    Photo.Image image = new Photo.Image();
    image.setSource(PHOTO_SOURCE);
    photo.addImage(image);
    photo.setName(PHOTO_NAME);
    ArrayList<Photo> photos = new ArrayList<>();
    photos.add(photo);
    @SuppressWarnings("unchecked") Connection<Photo> photoConnection = mock(Connection.class);
    when(photosInterface.getPhotos(ALBUM_ID, Optional.empty())).thenReturn(photoConnection);
    when(photoConnection.getData()).thenReturn(photos);
    final ImageStreamProvider imageStreamProvider = mock(ImageStreamProvider.class);
    InputStream inputStream = getClass().getClassLoader().getResourceAsStream("test.jpeg");
    HttpURLConnection connection = mock(HttpURLConnection.class);
    when(imageStreamProvider.getConnection(ArgumentMatchers.anyString())).thenReturn(connection);
    when(connection.getInputStream()).thenReturn(inputStream);
    final TemporaryPerJobDataStore store = mock(TemporaryPerJobDataStore.class);
    facebookPhotosExporter = new FacebookPhotosExporter(new AppCredentials("key", "secret"), photosInterface, null, store, imageStreamProvider);
}
Also used : TemporaryPerJobDataStore(org.datatransferproject.spi.cloud.storage.TemporaryPerJobDataStore) AppCredentials(org.datatransferproject.types.transfer.auth.AppCredentials) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) ImageStreamProvider(org.datatransferproject.transfer.ImageStreamProvider) PhotoAlbum(org.datatransferproject.types.common.models.photos.PhotoAlbum) Album(com.restfb.types.Album) Photo(com.restfb.types.Photo) HttpURLConnection(java.net.HttpURLConnection) Before(org.junit.Before)

Aggregations

Photo (com.restfb.types.Photo)2 ArrayList (java.util.ArrayList)2 FacebookGraphException (com.restfb.exception.FacebookGraphException)1 Album (com.restfb.types.Album)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 TemporaryPerJobDataStore (org.datatransferproject.spi.cloud.storage.TemporaryPerJobDataStore)1 ExportResult (org.datatransferproject.spi.transfer.provider.ExportResult)1 ContinuationData (org.datatransferproject.spi.transfer.types.ContinuationData)1 ImageStreamProvider (org.datatransferproject.transfer.ImageStreamProvider)1 PaginationData (org.datatransferproject.types.common.PaginationData)1 StringPaginationToken (org.datatransferproject.types.common.StringPaginationToken)1 PhotoAlbum (org.datatransferproject.types.common.models.photos.PhotoAlbum)1 PhotoModel (org.datatransferproject.types.common.models.photos.PhotoModel)1 PhotosContainerResource (org.datatransferproject.types.common.models.photos.PhotosContainerResource)1 AppCredentials (org.datatransferproject.types.transfer.auth.AppCredentials)1 Before (org.junit.Before)1