Search in sources :

Example 6 with VideosContainerResource

use of org.datatransferproject.types.common.models.videos.VideosContainerResource in project data-transfer-project by google.

the class KoofrVideosImporterTest method testImportItemFromURLWithoutAlbum.

@Test
public void testImportItemFromURLWithoutAlbum() throws Exception {
    server.enqueue(new MockResponse().setResponseCode(200).setBody("123"));
    server.enqueue(new MockResponse().setResponseCode(200).setBody("4567"));
    when(client.ensureVideosFolder()).thenReturn("/root/Videos");
    UUID jobId = UUID.randomUUID();
    Collection<VideoAlbum> albums = ImmutableList.of();
    Collection<VideoModel> videos = ImmutableList.of(new VideoModel("video1.mp4", server.url("/1.mp4").toString(), "A video 1", "video/mp4", "video1", null, false), new VideoModel("video2.mp4", server.url("/2.mp4").toString(), "A video 2", "video/mp4", "video2", null, false));
    VideosContainerResource resource = spy(new VideosContainerResource(albums, videos));
    importer.importItem(jobId, executor, authData, resource);
    InOrder clientInOrder = Mockito.inOrder(client);
    clientInOrder.verify(client).ensureVideosFolder();
    clientInOrder.verify(client).fileExists(eq("/root/Videos/video1.mp4"));
    clientInOrder.verify(client).uploadFile(eq("/root/Videos"), eq("video1.mp4"), any(), eq("video/mp4"), isNull(), eq("A video 1"));
    clientInOrder.verify(client).fileExists(eq("/root/Videos/video2.mp4"));
    clientInOrder.verify(client).uploadFile(eq("/root/Videos"), eq("video2.mp4"), any(), eq("video/mp4"), isNull(), eq("A video 2"));
    clientInOrder.verifyNoMoreInteractions();
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) InOrder(org.mockito.InOrder) VideosContainerResource(org.datatransferproject.types.common.models.videos.VideosContainerResource) UUID(java.util.UUID) VideoAlbum(org.datatransferproject.types.common.models.videos.VideoAlbum) VideoModel(org.datatransferproject.types.common.models.videos.VideoModel) Test(org.junit.Test)

Example 7 with VideosContainerResource

use of org.datatransferproject.types.common.models.videos.VideosContainerResource in project data-transfer-project by google.

the class KoofrVideosImporterTest method testSkipNotFoundVideo.

@Test
public void testSkipNotFoundVideo() throws Exception {
    server.enqueue(new MockResponse().setResponseCode(404).setBody("4567"));
    UUID jobId = UUID.randomUUID();
    Collection<VideoAlbum> albums = ImmutableList.of();
    Collection<VideoModel> videos = ImmutableList.of(new VideoModel("not_found_video_1.mp4", server.url("/not_found.mp4").toString(), "Video not founded in CDN", "video/mp4", "not_found_video_1", null, false));
    VideosContainerResource resource = spy(new VideosContainerResource(albums, videos));
    importer.importItem(jobId, executor, authData, resource);
    InOrder clientInOrder = Mockito.inOrder(client);
    clientInOrder.verifyNoMoreInteractions();
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) InOrder(org.mockito.InOrder) VideosContainerResource(org.datatransferproject.types.common.models.videos.VideosContainerResource) UUID(java.util.UUID) VideoAlbum(org.datatransferproject.types.common.models.videos.VideoAlbum) VideoModel(org.datatransferproject.types.common.models.videos.VideoModel) Test(org.junit.Test)

Example 8 with VideosContainerResource

use of org.datatransferproject.types.common.models.videos.VideosContainerResource in project data-transfer-project by google.

the class GoogleVideosExporter method exportVideos.

@VisibleForTesting
ExportResult<VideosContainerResource> exportVideos(TokensAndUrlAuthData authData, Optional<StringPaginationToken> paginationData) throws IOException {
    Optional<String> paginationToken = paginationData.map(StringPaginationToken::getToken);
    MediaItemSearchResponse mediaItemSearchResponse = getOrCreateVideosInterface(authData).listVideoItems(paginationToken);
    PaginationData nextPageData = null;
    if (!Strings.isNullOrEmpty(mediaItemSearchResponse.getNextPageToken())) {
        nextPageData = new StringPaginationToken(mediaItemSearchResponse.getNextPageToken());
    }
    ContinuationData continuationData = new ContinuationData(nextPageData);
    VideosContainerResource containerResource = null;
    GoogleMediaItem[] mediaItems = mediaItemSearchResponse.getMediaItems();
    if (mediaItems != null && mediaItems.length > 0) {
        List<VideoModel> videos = convertVideosList(mediaItems);
        containerResource = new VideosContainerResource(null, videos);
    }
    ResultType resultType = ResultType.CONTINUE;
    if (nextPageData == null) {
        resultType = ResultType.END;
    }
    return new ExportResult<>(resultType, containerResource, continuationData);
}
Also used : PaginationData(org.datatransferproject.types.common.PaginationData) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) ResultType(org.datatransferproject.spi.transfer.provider.ExportResult.ResultType) VideoModel(org.datatransferproject.types.common.models.videos.VideoModel) MediaItemSearchResponse(org.datatransferproject.datatransfer.google.mediaModels.MediaItemSearchResponse) VideosContainerResource(org.datatransferproject.types.common.models.videos.VideosContainerResource) GoogleMediaItem(org.datatransferproject.datatransfer.google.mediaModels.GoogleMediaItem) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 9 with VideosContainerResource

use of org.datatransferproject.types.common.models.videos.VideosContainerResource in project data-transfer-project by google.

the class GoogleVideosImporter method importItem.

@Override
public ImportResult importItem(UUID jobId, IdempotentImportExecutor executor, TokensAndUrlAuthData authData, VideosContainerResource data) throws Exception {
    if (data == null) {
        // Nothing to do
        return ImportResult.OK;
    }
    PhotosLibraryClient client;
    if (clientsMap.containsKey(jobId)) {
        client = clientsMap.get(jobId);
    } else {
        PhotosLibrarySettings settings = PhotosLibrarySettings.newBuilder().setCredentialsProvider(FixedCredentialsProvider.create(UserCredentials.newBuilder().setClientId(appCredentials.getKey()).setClientSecret(appCredentials.getSecret()).setAccessToken(new AccessToken(authData.getAccessToken(), new Date())).setRefreshToken(authData.getRefreshToken()).build())).build();
        client = PhotosLibraryClient.initialize(settings);
        clientsMap.put(jobId, client);
    }
    long bytes = 0L;
    // Uploads videos
    final Collection<VideoModel> videos = data.getVideos();
    if (videos != null && videos.size() > 0) {
        Stream<VideoModel> stream = videos.stream().filter(video -> shouldImport(video, executor)).map(this::transformVideoName);
        // We partition into groups of 49 as 50 is the maximum number of items that can be created in
        // one call. (We use 49 to avoid potential off by one errors)
        // https://developers.google.com/photos/library/guides/upload-media#creating-media-item
        final UnmodifiableIterator<List<VideoModel>> batches = Iterators.partition(stream.iterator(), 49);
        while (batches.hasNext()) {
            long batchBytes = importVideoBatch(batches.next(), client, executor);
            bytes += batchBytes;
        }
    }
    final ImportResult result = ImportResult.OK;
    return result.copyWithBytes(bytes);
}
Also used : RandomAccessFile(java.io.RandomAccessFile) DestinationMemoryFullException(org.datatransferproject.spi.transfer.types.DestinationMemoryFullException) Error(com.google.photos.library.v1.upload.UploadMediaItemResponse.Error) ImportResult(org.datatransferproject.spi.transfer.provider.ImportResult) Date(java.util.Date) PhotosLibrarySettings(com.google.photos.library.v1.PhotosLibrarySettings) FixedCredentialsProvider(com.google.api.gax.core.FixedCredentialsProvider) HashMap(java.util.HashMap) AppCredentials(org.datatransferproject.types.transfer.auth.AppCredentials) Iterators(com.google.common.collect.Iterators) ArrayList(java.util.ArrayList) TemporaryPerJobDataStore(org.datatransferproject.spi.cloud.storage.TemporaryPerJobDataStore) Strings(com.google.common.base.Strings) NewMediaItem(com.google.photos.library.v1.proto.NewMediaItem) Pair(org.apache.commons.lang3.tuple.Pair) Map(java.util.Map) Importer(org.datatransferproject.spi.transfer.provider.Importer) UploadMediaItemRequest(com.google.photos.library.v1.upload.UploadMediaItemRequest) NewMediaItemFactory(com.google.photos.library.v1.util.NewMediaItemFactory) Code(com.google.rpc.Code) ImageStreamProvider(org.datatransferproject.transfer.ImageStreamProvider) PhotosLibraryClient(com.google.photos.library.v1.PhotosLibraryClient) IdempotentImportExecutor(org.datatransferproject.spi.transfer.idempotentexecutor.IdempotentImportExecutor) UploadErrorException(org.datatransferproject.spi.transfer.types.UploadErrorException) Collection(java.util.Collection) Status(com.google.rpc.Status) MediaObject(org.datatransferproject.types.common.models.MediaObject) IOException(java.io.IOException) NewMediaItemResult(com.google.photos.library.v1.proto.NewMediaItemResult) UUID(java.util.UUID) UserCredentials(com.google.auth.oauth2.UserCredentials) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) VideosContainerResource(org.datatransferproject.types.common.models.videos.VideosContainerResource) BatchCreateMediaItemsResponse(com.google.photos.library.v1.proto.BatchCreateMediaItemsResponse) List(java.util.List) Stream(java.util.stream.Stream) Monitor(org.datatransferproject.api.launcher.Monitor) TokensAndUrlAuthData(org.datatransferproject.types.transfer.auth.TokensAndUrlAuthData) InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) VideoModel(org.datatransferproject.types.common.models.videos.VideoModel) Preconditions(com.google.common.base.Preconditions) UploadMediaItemResponse(com.google.photos.library.v1.upload.UploadMediaItemResponse) VisibleForTesting(com.google.common.annotations.VisibleForTesting) UnmodifiableIterator(com.google.common.collect.UnmodifiableIterator) AccessToken(com.google.auth.oauth2.AccessToken) InputStream(java.io.InputStream) ImportResult(org.datatransferproject.spi.transfer.provider.ImportResult) AccessToken(com.google.auth.oauth2.AccessToken) PhotosLibrarySettings(com.google.photos.library.v1.PhotosLibrarySettings) ArrayList(java.util.ArrayList) List(java.util.List) PhotosLibraryClient(com.google.photos.library.v1.PhotosLibraryClient) VideoModel(org.datatransferproject.types.common.models.videos.VideoModel) Date(java.util.Date)

Example 10 with VideosContainerResource

use of org.datatransferproject.types.common.models.videos.VideosContainerResource in project data-transfer-project by google.

the class KoofrVideosExporter method export.

@Override
public ExportResult<VideosContainerResource> export(UUID jobId, TokensAndUrlAuthData authData, Optional<ExportInformation> exportInformation) throws CopyExceptionWithFailureReason {
    Preconditions.checkNotNull(authData);
    KoofrClient koofrClient = koofrClientFactory.create(authData);
    KoofrMediaExport export = new KoofrMediaExport(koofrClient, monitor);
    try {
        export.export();
        List<VideoAlbum> exportAlbums = export.getVideoAlbums();
        List<VideoModel> exportVideos = export.getVideos();
        VideosContainerResource containerResource = new VideosContainerResource(exportAlbums, exportVideos);
        return new ExportResult<>(ExportResult.ResultType.END, containerResource, null);
    } catch (IOException e) {
        return new ExportResult<>(e);
    }
}
Also used : VideosContainerResource(org.datatransferproject.types.common.models.videos.VideosContainerResource) KoofrClient(org.datatransferproject.transfer.koofr.common.KoofrClient) IOException(java.io.IOException) VideoAlbum(org.datatransferproject.types.common.models.videos.VideoAlbum) VideoModel(org.datatransferproject.types.common.models.videos.VideoModel) KoofrMediaExport(org.datatransferproject.transfer.koofr.common.KoofrMediaExport) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult)

Aggregations

VideosContainerResource (org.datatransferproject.types.common.models.videos.VideosContainerResource)13 VideoModel (org.datatransferproject.types.common.models.videos.VideoModel)11 Test (org.junit.Test)9 UUID (java.util.UUID)6 VideoAlbum (org.datatransferproject.types.common.models.videos.VideoAlbum)5 ArrayList (java.util.ArrayList)3 MockResponse (okhttp3.mockwebserver.MockResponse)3 ExportResult (org.datatransferproject.spi.transfer.provider.ExportResult)3 ImportResult (org.datatransferproject.spi.transfer.provider.ImportResult)3 InOrder (org.mockito.InOrder)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 IOException (java.io.IOException)2 ContinuationData (org.datatransferproject.spi.transfer.types.ContinuationData)2 StringPaginationToken (org.datatransferproject.types.common.StringPaginationToken)2 TokensAndUrlAuthData (org.datatransferproject.types.transfer.auth.TokensAndUrlAuthData)2 FixedCredentialsProvider (com.google.api.gax.core.FixedCredentialsProvider)1 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)1 AccessToken (com.google.auth.oauth2.AccessToken)1 UserCredentials (com.google.auth.oauth2.UserCredentials)1 Preconditions (com.google.common.base.Preconditions)1