Example usage for com.google.api.client.googleapis.media MediaHttpDownloader setDirectDownloadEnabled

List of usage examples for com.google.api.client.googleapis.media MediaHttpDownloader setDirectDownloadEnabled

Introduction

In this page you can find the example usage for com.google.api.client.googleapis.media MediaHttpDownloader setDirectDownloadEnabled.

Prototype

public MediaHttpDownloader setDirectDownloadEnabled(boolean directDownloadEnabled) 

Source Link

Document

Returns whether direct media download is enabled or disabled.

Usage

From source file:api.Captions.java

License:Apache License

/**
 * Saves caption track to file. (Not permitted for most videos.)
 *
 * @param captionId id/* w w  w .j a v  a2s  .  com*/
 * @throws IOException (can be thrown by caption.download method)
 */
private static void downloadCaption(String captionId) throws IOException {
    Download captionDownload = yt.captions().download(captionId).setTfmt(SRT);
    MediaHttpDownloader downloader = captionDownload.getMediaHttpDownloader();
    downloader.setDirectDownloadEnabled(false);
    MediaHttpDownloaderProgressListener downloadProgressListener = new MediaHttpDownloaderProgressListener() {
        public void progressChanged(MediaHttpDownloader downloader) throws IOException {
            switch (downloader.getDownloadState()) {
            case MEDIA_IN_PROGRESS:
                System.out.println("Download in progress");
                System.out.println("Download percentage: " + downloader.getProgress());
                break;
            // This value is set after the entire media file has
            //  been successfully downloaded.
            case MEDIA_COMPLETE:
                System.out.println("Download Completed!");
                break;
            // This value indicates that the download process has
            //  not started yet.
            case NOT_STARTED:
                System.out.println("Download Not Started!");
                break;
            }
        }
    };
    downloader.setProgressListener(downloadProgressListener);

    OutputStream outputFile = new FileOutputStream("captionFile.srt");
    captionDownload.executeAndDownloadTo(outputFile);
}

From source file:com.datafrog.mms.bak.DriveSample.java

License:Apache License

/** Downloads a file using either resumable or direct media download. */
private static void downloadFile(final boolean useDirectDownload, final File uploadedFile) throws IOException {
    // create parent directory (if necessary)
    java.io.File parentDir = new java.io.File(DIR_FOR_DOWNLOADS);
    if (!parentDir.exists() && !parentDir.mkdirs()) {
        throw new IOException("Unable to create parent directory");
    }/*from   ww  w. j a va2s  .co  m*/
    OutputStream out = new FileOutputStream(new java.io.File(parentDir, uploadedFile.getTitle()));

    MediaHttpDownloader downloader = new MediaHttpDownloader(httpTransport,
            drive.getRequestFactory().getInitializer());
    downloader.setDirectDownloadEnabled(useDirectDownload);
    downloader.setProgressListener(new FileDownloadProgressListener());
    System.out.println("DownLoadURL : " + uploadedFile.getDownloadUrl());
    downloader.download(new GenericUrl(uploadedFile.getDownloadUrl()), out);
}

From source file:com.github.valentin.fedoskin.fb2me.desktop.drive.DriveSample.java

License:Apache License

/** Downloads a file using either resumable or direct media download. */
private static void downloadFile(boolean useDirectDownload, File uploadedFile) throws IOException {
    // create parent directory (if necessary)
    java.io.File parentDir = new java.io.File(DIR_FOR_DOWNLOADS);
    if (!parentDir.exists() && !parentDir.mkdirs()) {
        throw new IOException("Unable to create parent directory");
    }/* ww w.j  av  a2s  .c o  m*/
    OutputStream out = new FileOutputStream(new java.io.File(parentDir, uploadedFile.getTitle()));
    Drive.Files.Get get = drive.files().get(uploadedFile.getId());
    MediaHttpDownloader downloader = get.getMediaHttpDownloader();
    downloader.setDirectDownloadEnabled(useDirectDownload);
    downloader.setProgressListener(new FileDownloadProgressListener());
    downloader.download(new GenericUrl(uploadedFile.getDownloadUrl()), out);
}

From source file:com.google.gcloud.spi.DefaultStorageRpc.java

License:Open Source License

@Override
public byte[] read(StorageObject from, Map<Option, ?> options, long position, int bytes)
        throws StorageException {
    try {//w w w  . ja  v a2 s . c om
        Get req = storage.objects().get(from.getBucket(), from.getName());
        req.setIfMetagenerationMatch(IF_METAGENERATION_MATCH.getLong(options))
                .setIfMetagenerationNotMatch(IF_METAGENERATION_NOT_MATCH.getLong(options))
                .setIfGenerationMatch(IF_GENERATION_MATCH.getLong(options))
                .setIfGenerationNotMatch(IF_GENERATION_NOT_MATCH.getLong(options));
        MediaHttpDownloader downloader = req.getMediaHttpDownloader();
        // todo: Fix int casting (https://github.com/google/google-api-java-client/issues/937)
        downloader.setContentRange(position, (int) position + bytes);
        downloader.setDirectDownloadEnabled(true);
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        req.executeMediaAndDownloadTo(output);
        return output.toByteArray();
    } catch (IOException ex) {
        throw translate(ex);
    }
}

From source file:com.mrmq.uyoutube.data.Captions.java

License:Apache License

/**
 * Downloads a caption track for a YouTube video. (captions.download)
 *
 * @param captionId The id parameter specifies the caption ID for the resource
 * that is being downloaded. In a caption resource, the id property specifies the
 * caption track's ID.//from  ww  w.  j ava2  s. com
 * @throws IOException
 */
private static void downloadCaption(String captionId) throws IOException {
    // Create an API request to the YouTube Data API's captions.download
    // method to download an existing caption track.
    Download captionDownload = youtube.captions().download(captionId).setTfmt(SRT);

    // Set the download type and add an event listener.
    MediaHttpDownloader downloader = captionDownload.getMediaHttpDownloader();

    // Indicate whether direct media download is enabled. A value of
    // "True" indicates that direct media download is enabled and that
    // the entire media content will be downloaded in a single request.
    // A value of "False," which is the default, indicates that the
    // request will use the resumable media download protocol, which
    // supports the ability to resume a download operation after a
    // network interruption or other transmission failure, saving
    // time and bandwidth in the event of network failures.
    downloader.setDirectDownloadEnabled(false);

    // Set the download state for the caption track file.
    MediaHttpDownloaderProgressListener downloadProgressListener = new MediaHttpDownloaderProgressListener() {
        @Override
        public void progressChanged(MediaHttpDownloader downloader) throws IOException {
            switch (downloader.getDownloadState()) {
            case MEDIA_IN_PROGRESS:
                System.out.println("Download in progress");
                System.out.println("Download percentage: " + downloader.getProgress());
                break;
            // This value is set after the entire media file has
            //  been successfully downloaded.
            case MEDIA_COMPLETE:
                System.out.println("Download Completed!");
                break;
            // This value indicates that the download process has
            //  not started yet.
            case NOT_STARTED:
                System.out.println("Download Not Started!");
                break;
            }
        }
    };
    downloader.setProgressListener(downloadProgressListener);

    OutputStream outputFile = new FileOutputStream("captionFile.srt");
    // Download the caption track.
    captionDownload.executeAndDownloadTo(outputFile);
}

From source file:de.jlo.talendcomp.google.drive.DriveHelper.java

License:Apache License

private void downloadByUrl(String fileDownloadUrl, String localFilePath, boolean createDirs) throws Exception {
    checkPrerequisits();//from w ww. ja  va 2s  .c  om
    lastDownloadedFilePath = null;
    lastDownloadedFileSize = 0;
    File localFile = new File(localFilePath);
    if (createDirs && localFile.getParentFile().exists() == false) {
        if (localFile.getParentFile().mkdirs() == false) {
            throw new Exception("Unable to create parent directory: " + localFile.getParent());
        }
    }
    OutputStream fileOut = new BufferedOutputStream(new FileOutputStream(localFile));
    try {
        MediaHttpDownloader downloader = new MediaHttpDownloader(HTTP_TRANSPORT,
                driveService.getRequestFactory().getInitializer());
        downloader.setDirectDownloadEnabled(false);
        downloader.setProgressListener(new MediaHttpDownloaderProgressListener() {

            @Override
            public void progressChanged(MediaHttpDownloader downloader) throws IOException {
                info("File status: " + downloader.getDownloadState());
                info("Bytes downloaded:" + downloader.getNumBytesDownloaded());
            }

        });
        downloader.download(new GenericUrl(fileDownloadUrl), fileOut);
        if (fileOut != null) {
            fileOut.flush();
            fileOut.close();
            fileOut = null;
        }
        lastDownloadedFilePath = localFile.getAbsolutePath();
        lastDownloadedFileSize = localFile.length();
    } catch (Exception e) {
        if (fileOut != null) {
            fileOut.flush();
            fileOut.close();
            fileOut = null;
        }
        Thread.sleep(200);
        if (localFile.canWrite()) {
            System.err.println("Download failed. Remove incomplete file: " + localFile.getAbsolutePath());
            localFile.delete();
        }
        throw e;
    }
}

From source file:de.pfabulist.googledrive.GoogleDriveElsewhere.java

License:BSD License

@Override
public void get(String id, long version, OutputStream os) {

    try {/*from   w ww . jav a  2s . c om*/
        File metadata = drive.files().get(id).execute();
        if (!metadata.getTitle().equals("" + version)) {
            Log.info("no such version");
            return;
        }
        MediaHttpDownloader downloader = new MediaHttpDownloader(httpTransport,
                drive.getRequestFactory().getInitializer());
        downloader.setDirectDownloadEnabled(true); // todo false for large files ?
        downloader.setProgressListener(new FileDownloadProgressListener());
        downloader.download(new GenericUrl(metadata.getDownloadUrl()), os);
    } catch (IOException e) {
        throw u(e);
    }
}

From source file:google.drive.example.v2.cmdline.DriveSample.java

License:Apache License

/** Downloads a file using either resumable or direct media download. */
private static void downloadFile(boolean useDirectDownload, File uploadedFile) throws IOException {
    // create parent directory (if necessary)
    java.io.File parentDir = new java.io.File(DIR_FOR_DOWNLOADS);
    if (!parentDir.exists() && !parentDir.mkdirs()) {
        throw new IOException("Unable to create parent directory");
    }/* w ww  .j  a v  a2s.c  o m*/

    // OutputStream out = new FileOutputStream(new java.io.File(parentDir, uploadedFile.getTitle()));
    OutputStream out = new FileOutputStream(new java.io.File(parentDir, uploadedFile.getName()));

    MediaHttpDownloader downloader = new MediaHttpDownloader(httpTransport,
            drive.getRequestFactory().getInitializer());
    downloader.setDirectDownloadEnabled(useDirectDownload);
    downloader.setProgressListener(new FileDownloadProgressListener());
    // downloader.download(new GenericUrl(uploadedFile.getDownloadUrl()), out);
}

From source file:net.nharyes.drivecopy.srvc.DriveSdoImpl.java

License:Apache License

@Override
public EntryBO downloadEntry(TokenBO token, EntryBO entry) throws SdoException {

    try {//from  w w  w .ja v a  2s  .  c  o  m

        // get file
        Drive service = getService(token);
        Get get = service.files().get(entry.getId());
        MediaHttpDownloader downloader = new MediaHttpDownloader(httpTransport,
                service.getRequestFactory().getInitializer());
        downloader.setDirectDownloadEnabled(false);
        downloader.setProgressListener(fileDownloadProgressListener);
        File file = get.execute();

        // check download URL and size
        if (file.getDownloadUrl() != null && file.getDownloadUrl().length() > 0) {

            // download file
            FileOutputStream fout = new FileOutputStream(entry.getFile());
            downloader.download(new GenericUrl(file.getDownloadUrl()), fout);
            fout.flush();
            fout.close();

            // return entry
            entry.setMd5Sum(file.getMd5Checksum());
            return entry;

        } else {

            // the file doesn't have any content stored on Drive
            throw new ItemNotFoundException(String.format(
                    "Remote file with id '%s' doesn't have any content stored on Drive", entry.getId()));
        }

    } catch (IOException ex) {

        // re-throw exception
        throw new SdoException(ex.getMessage(), ex);
    }
}

From source file:org.apache.manifoldcf.crawler.connectors.googledrive.GoogleDriveSession.java

License:Apache License

/** Get a stream representing the specified document.
*///w w w .  ja v a 2  s .  com
public void getGoogleDriveOutputStream(XThreadInputStream inputStream, String documentURI) throws IOException {
    // Create an object that implements outputstream but pushes everything through to the designated input stream
    OutputStream outputStream = new XThreadOutputStream(inputStream);
    try {
        MediaHttpDownloader downloader = new MediaHttpDownloader(HTTP_TRANSPORT,
                drive.getRequestFactory().getInitializer());
        downloader.setDirectDownloadEnabled(false);
        downloader.download(new GenericUrl(documentURI), outputStream);
    } finally {
        // Make sure it is closed and flushed
        outputStream.close();
    }
}