예제 #1
0
def main():
    """Uses the Docs API to print out the text of a document."""
    credentials = get_credentials()
    http = credentials.authorize(Http())
    docs_service = discovery.build(
        'docs', 'v1', http=http, discoveryServiceUrl=DISCOVERY_DOC)
    drive_service = discovery.build('drive', 'v3', credentials=credentials)
    doc = docs_service.documents().get(documentId=DOCUMENT_ID).execute()
    doc_content = doc.get('body').get('content')
    requests = [
     {
        "replaceAllText" : {
            "replaceText" : sys.argv[3],
            "containsText" : {
                "text" : sys.argv[2],
                "matchCase" : True
            }
        }
     }
    ]

    result = docs_service.documents().batchUpdate(
        documentId=DOCUMENT_ID, body={'requests' : requests}).execute()

    download_request = drive_service.files().export(fileId=DOCUMENT_ID,
                                                          mimeType='application/pdf')
    fh = io.BytesIO()
    downloader = driveHttp.MediaIoBaseDownload(fh, download_request)
    done = False
    while done is False:
        status, done = downloader.next_chunk()
        print("Downloaded %d%%." % int(status.progress() * 100))
    if done is True:
       with open(sys.argv[4], 'wb') as f:
            f.write(fh.getvalue())
def download_file(service, file_id, name, size, path):
    print("Download: " + "['" + file_id + "'" + ", '" + name + "']")
    dir = os.path.join(path, name)
    # filesize =
    # print(str(filesize) + '....' + size)
    if os.path.exists(dir) == True and str(os.path.getsize(dir)) == size:
        print('File alreay exist, skipping...')
        return 0

    file_io_base = open(dir, 'wb')

    request = service.files().get_media(fileId=file_id)
    media_request = http.MediaIoBaseDownload(file_io_base, request)
    while True:
        try:
            status, done = media_request.next_chunk()
        except errors.HttpError as error:
            print('An error occurred: %s' % error)
            return
        if status:
            print("... %d%%." % int(status.progress() * 100))
        if done:
            print('Download Complete')
            file_io_base.close()
            return 0
예제 #3
0
def download_file(service, file_id, filename):
    """Download a Drive file's content to the local filesystem.
    Args:
      service   : Drive API Service instance.
      file_id   : ID of the Drive file that will downloaded.
      local_fd  : io.Base or file object, the stream that the Drive file's contents will be written to.
    """
    fh = io.FileIO(filename, 'wb')
    request = service.files().get_media(fileId=file_id)
    media_request = http.MediaIoBaseDownload(fh, request)

    while True:
        try:
            download_progress, done = media_request.next_chunk()

        except Exception as e:
            print(f"An error occurred: {e}")

        if download_progress:
            print(
                f"Download Progress: {int(download_progress.progress() * 100)}"
            )
        if done:
            print("Download Complete")
            break
예제 #4
0
def download_file(service, file_id, destination):
    """ Function to download a file stored in Google Drive
    Args:
        service (Google Drive API): object to interact with the content
        file_id (str): Google Drive ID for the file to retrieve
        destination (str): local path to save the file

    Returns:
        Dictionary with the metadata from the downloaded file
    """

    info = service.files().get(fileId=file_id).execute()
    # Google Docs files
    if info["mimeType"] == "application/vnd.google-apps.document":
        request = service.files().export_media(fileId=info['id'],
                                               mimeType="application/vnd"
                                               ".oasis.opendocument"
                                               ".text")
    # Files stored in Google Drive
    else:
        request = service.files().get_media(fileId=info['id'])

    fh = io.BytesIO()
    downloader = http.MediaIoBaseDownload(fh, request)
    done = False
    while done is False:
        status, done = downloader.next_chunk()
        # print("Download %d%%." % int(status.progress() * 100))

    write_bytesio_to_file(destination, fh)

    return info
예제 #5
0
    def download_file(self, where_to_download, query, onProgress=None):
        files = self.search_service.find(**query)

        if len(files) == 0:
            print("File not found! '{}'".format(query))
            return

        if len(files) > 1:
            print("There are multiple files! Select only one: {}".format(
                [x['name'] for x in files]))
            return

        file = files[0]
        print("File found! '{}'".format(file['name']))

        print("Downloading File...")

        fullpath = os.path.join(where_to_download, file['name'])
        print("Saving in {}".format(fullpath))

        file_stream = open(fullpath, "wb")

        request = self.service.files().get_media(fileId=file['id'])
        media_request = http.MediaIoBaseDownload(file_stream, request)

        done = False
        while done is False:
            status, done = media_request.next_chunk()
            if onProgress:
                onProgress(int((status.progress() if status else 1) * 100))

        print("Download Complete!")
예제 #6
0
    def download_file(self, __id: str, __path: Path) -> None:
        """ Download the file by ID.

        :param __id: string, file ID.
        :param __path: Path, path to download the file.
        :return: None.
        """
        # m_type = mime_type(__path)
        _request = self.__drive.files().get_media(fileId=__id)
        _fh = io.BytesIO()
        _downloader = http.MediaIoBaseDownload(_fh, _request)

        _done = False
        while _done is False:
            try:
                status, _done = _downloader.next_chunk()
            except Exception as e:
                print(f"{e}\nwhile downloading file: {__path}",
                      file=sys.stderr)
                return

            print(f"Download {int(status.progress() * 100)}%")

        with io.open(str(__path), 'wb') as f:
            _fh.seek(10)
            f.write(_fh.read())
예제 #7
0
def download_file(service, file_id, local_fd):
    """
    This will actually download the file from Google Drive.

    :param service:
    :param file_id:
    :param local_fd:
    :return:
    """
    download_progress = None
    request = service.files().get_media(fileId=file_id)
    media_request = http.MediaIoBaseDownload(local_fd, request)
    while True:
        try:
            download_progress, done = media_request.next_chunk()
        except errors.HttpError as error:
            #print('An error occurred: %s' % error)
            break
        if download_progress:
            #print('Download Progress: %d%%' % int(download_progress.progress() * 100))
            pass
        if done:
            #print('Download Complete')
            pass
    if download_progress and download_progress.progress() >= 1.0:
        return True
예제 #8
0
def download_file(service, file_id, local_fd):
    """Download a Drive file's content to the local filesystem.

    Args:
      service: Drive API Service instance.
      file_id: ID of the Drive file that will downloaded.
      local_fd: io.Base or file object, the stream that the Drive file's
          contents will be written to.
    """
    request = service.files().get_media(fileId=file_id)
    media_request = http.MediaIoBaseDownload(local_fd, request)

    while True:
        try:
            download_progress, done = media_request.next_chunk()
        except errors.HttpError, error:
            print 'An error occurred: %s' % error
            return

        if download_progress:
            print 'Download Progress: %d%%' % int(
                download_progress.progress() * 100)
        if done:
            print 'Download Complete'
            return
예제 #9
0
def download_file(service, file_id, local_fd):
    """Download a Drive file's content to the local filesystem.

  Args:
    service: Drive API Service instance.
    file_id: ID of the Drive file that will downloaded.
    local_fd: io.Base or file object, the stream that the Drive file's
        contents will be written to.
  """
    request = service.files().get_media(fileId=file_id)
    print('req-i' + str(request.headers))
    media_request = http.MediaIoBaseDownload(local_fd, request)

    while True:
        try:
            download_progress, done = media_request.next_chunk()
        except errors.HttpError, error:
            print('An error occurred: %s' % error)
            #   print(type(errors.HttpError))
            #   print(json.loads(errors.HttpError.content.decode('utf-8'))['error']['message'])  # works

            return
        if download_progress:
            print('Download Progress: %d%%' %
                  int(download_progress.progress() * 100))
        if done:
            print('Download Complete')
            return
def download_file(file_id, filename='myfile'):
    request = drive_service.files().get_media(fileId=file_id)
    fh = io.FileIO(filename, 'wb')
    downloader = http.MediaIoBaseDownload(fh, request)
    done = False
    while done is False:
        status, done = downloader.next_chunk()
        print "Download %d%%." % int(status.progress() * 100)
예제 #11
0
 def download_file(self, file_id, file_type, export_path):
     request = self.files.export_media(fileId=file_id, mimeType=file_type)
     fh = io.FileIO(export_path, 'wb')
     downloader = http.MediaIoBaseDownload(fh, request)
     done = False
     while done is False:
         status, done = downloader.next_chunk()
     return export_path
예제 #12
0
 def pull(self, name):
     file_id = self.exists(name)['id']
     request = self.files.get_media(fileId=file_id)
     fh = io.FileIO(name, 'wb')
     downloader = http.MediaIoBaseDownload(fh, request)
     done = False
     while done is False:
         status, done = downloader.next_chunk()
예제 #13
0
def downoad_file_and_store(service, file_id):
    try:
        file_info = get_file_info(service, file_id)
        if (file_info == -1):
            return -1, -1
        download_dir = os.path.join(settings.TEMP_DOWNLOAD_PATH, file_id)
        path_download = os.path.join(download_dir, str(file_info["name"]))
        path_fileinfo = os.path.join(download_dir, "fileinfo")

        if not os.path.exists(download_dir):
            os.makedirs(download_dir)
        else:
            try:
                if os.path.exists(path_fileinfo):
                    json_data = json.load(open(path_fileinfo))
                    if "modifiedTime" in json_data:
                        modified_time = json_data["modifiedTime"]
                    else:
                        modified_time = ""
                    if "createdTime" in json_data:
                        created_time = json_data["createdTime"]
                    else:
                        created_time = ""
                    if (created_time == file_info["createdTime"]
                            and modified_time == file_info["modifiedTime"]
                            and os.path.exists(path_download)
                            and os.path.getsize(path_download) == long(
                                file_info["size"])):
                        return str(path_download), str(file_info["name"])
                    else:
                        pass
                else:
                    pass
            except:
                pass
        if file_info["mimeType"] in array_google_mimeType:
            mimeTypeExport = ""
            for mime in list_google_mimeType:
                if mime[0] == file_info["mimeType"]:
                    mimeTypeExport = mime[2]
                    break
            request = service.files().export_media(fileId=file_id,
                                                   mimeType=mimeTypeExport)
        else:
            request = service.files().get_media(fileId=file_id)
        fh = open(path_download, 'wb')
        downloader = http.MediaIoBaseDownload(fh, request)
        done = False
        while done is False:
            status, done = downloader.next_chunk()
            print("Downloaded " + str(file_info["name"]) + " " +
                  str(int(status.progress() * 100)) + "%")
        fh.close()
        json.dump(file_info, open(path_fileinfo, 'w+'))
        return str(path_download), str(file_info["name"])
    except Exception as e:
        print(e)
        return -1, -1
예제 #14
0
def table2sql(request, id=0):
    bucket_name = ''
    if id:

        storage = Storage(CredentialsModel, 'id', request.user, 'credential')
        credential = storage.get()
        http1 = httplib2.Http()

        http2 = credential.authorize(http1)
        service = build("drive", "v2", http=http2)
        #        files = service.files().list().execute()
        #        request = service.files().get_media(fileId=id)
        #        local_fd  = open('workfile', 'w');
        #        media_request = http.MediaIoBaseDownload(local_fd, request)
        #        close f;

        # Retry transport and file IO errors.
        RETRYABLE_ERRORS = (httplib2.HttpLib2Error, IOError)

        # Number of times to retry failed downloads.
        NUM_RETRIES = 5

        # Number of bytes to send/receive in each request.
        CHUNKSIZE = 2 * 1024 * 1024

        # Mimetype to use if one can't be guessed from the file extension.
        DEFAULT_MIMETYPE = 'application/octet-stream'

        print 'Building download request...'
        filename = 'xxx'
        f = file(filename, 'w')
        request = service.files().get_media(fileId=id)
        media = http.MediaIoBaseDownload(f, request, chunksize=CHUNKSIZE)

        print 'Downloading bucket: %s object: %s to file: %s' % (bucket_name,
                                                                 id, filename)

        progressless_iters = 0
        done = False
        while not done:
            error = None
            try:
                progress, done = media.next_chunk()
                if progress:
                    print_with_carriage_return('Download %d%%.' %
                                               int(progress.progress() * 100))
            except HttpError, err:
                error = err
                if err.resp.status < 500:
                    raise
            except RETRYABLE_ERRORS, err:
                error = err

            if error:
                progressless_iters += 1
                handle_progressless_iter(error, progressless_iters)
            else:
                progressless_iters = 0
예제 #15
0
def download(service=None, file_id=None, file_name='download.jpg'):
    request = service.files().get_media(fileId=file_id)

    with open(file_name, 'wb') as f:
        downloader = http.MediaIoBaseDownload(f, request)
        done = False
        while not done:
            status, done = downloader.next_chunk()
            print("Download %d%%." % int(status.progress() * 100))
예제 #16
0
 def export(self, spreadsheet_id, fformat):
     request = self.driveService.files().export(fileId=spreadsheet_id, mimeType=fformat.value.split(':')[0])
     import io
     fh = io.FileIO(spreadsheet_id+fformat.value.split(':')[1], 'wb')
     downloader = ghttp.MediaIoBaseDownload(fh, request)
     done = False
     while done is False:
         status, done = downloader.next_chunk()
         print("Download %d%%." % int(status.progress() * 100))
예제 #17
0
 def download_to(self, path):
     resource = service.files().get_media(fileId=self.id)
     file = open(path + self.properties['name'], "wb")
     media_request = http.MediaIoBaseDownload(file, resource)
     try:
         done = False
         while not done:
             download_progress, done = media_request.next_chunk()
     except errors.HttpError as error:
         print('An error occurred: %s' % error)
예제 #18
0
 def do_GET(self):
     f = self.send_head()
     if f:
         media_request = http.MediaIoBaseDownload(self.wfile, f)
         done = False
         while done is False:
             try:
                 download_progress, done = media_request.next_chunk()
             except Exception as e:
                 print(e)
                 self.send_error(HTTPStatus.INTERNAL_SERVER_ERROR)
예제 #19
0
def export_file(file_id, filepath):
    request = drive_service.files().export_media(fileId=file_id,
                                                 mimeType='text/csv')
    fh = io.BytesIO()
    downloader = ghttp.MediaIoBaseDownload(fh, request)
    done = False
    while done is False:
        status, done = downloader.next_chunk()
        print("Download %d%%." % int(status.progress() * 100))
    with io.open(filepath, 'wb') as f:
        fh.seek(0)
        f.write(fh.read())
예제 #20
0
        def start(self, file_handle=None):  #, first_process = True):
            if file_handle is None:
                self.file_handle = io.BytesIO()
            else:
                self.file_handle = file_handle

            self.downloader = http.MediaIoBaseDownload(self.file_handle,
                                                       self.request)
            self.done = False
            self.status = None

            self.part = 0
예제 #21
0
 def download_file(self, file_id):
     """
     Download a file given it's file id.
     :param file_id: File ID of the file to be downloaded.
     :return: ByteIO object of the file which can be saved to a file in the local system.
     """
     request = self.service.files().get_media(fileId=file_id)
     file_handler = io.BytesIO()
     downloader = http.MediaIoBaseDownload(file_handler, request)
     done = False
     while not done:
         status, done = downloader.next_chunk()
     return file_handler
예제 #22
0
    def save_raw_spreadsheet(self, sheet_id, spreadsheet_file):
        request = self.drive_api.files().export_media(
            fileId=sheet_id,
            mimeType=
            'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
        )

        with open(spreadsheet_file, 'wb') as fh:
            downloader = http.MediaIoBaseDownload(fh, request)

            done = False
            while done is False:
                status, done = downloader.next_chunk()
def getFile(service, folderID, fileID, fileName, destPath, verbose=False):
    """Retrieves a File from a google drive Folder. Currently we only
    support binary files so you can not get docs,spreadhseets, etc.
    Args:
        service: google drive service instance to use
        folderID: Parent Folder's ID from which to get the file
        fileID: Id of the file that needs to be retrieved from google drive
        fileName: Name to use for the local file
        destPath: destination path to use for the local file
        verbose: print debugging information
    Returns:
        File that was requested or error if error occured
    To Do:
        Check if File and folder exist
        Possible check if File is not 0 length before putting it up on google drive
    """
    drive_service = service
    os.chdir(destPath)
    local_fd = open(fileName, 'w')
    if verbose:
        print('Received FileID = ' + fileID, file=sys.stderr)
        print('Destination received: {0}'.format(destPath), file=sys.stderr)
    request = drive_service.files().get_media(fileId=fileID)

    alternateLinkFilePath = os.path.join(destPath, 'filelink.txt')
    fileUrlRequest = drive_service.files().get(fileId=fileID).execute()
    fileDownloadUrl = fileUrlRequest['webContentLink']

    if verbose:
        print('Headers: {0}'.format(request.headers), file=sys.stderr)
        print('URI: {0}'.format(request.uri), file=sys.stderr)

    media_request = http.MediaIoBaseDownload(local_fd, request)

    while True:
        try:
            download_progress, done = media_request.next_chunk()
        except errors.HttpError, error:
            #print 'An error occurred: %s' % error
            return error
        if download_progress:
            print('Download Progress: %d%%' %
                  int(download_progress.progress() * 100),
                  file=sys.stderr)
        if done:
            #print 'Download Complete'
            with open(alternateLinkFilePath, 'w') as fh:
                fh.write(fileDownloadUrl)
            print('Download Artifacts: {0}'.format(fileDownloadUrl),
                  file=sys.stderr)
            return
 def download_file(service_download, file_id, local_fd):
     request = service_download.files().get_media(fileId=file_id)
     media_request = http.MediaIoBaseDownload(local_fd, request)
     while True:
         try:
             download_progress, done = media_request.next_chunk()
         except errors.HttpError as error:
             print('An error occurred: %s' % error)
             return
         if download_progress:
             print('Download Progress: %d%%' % int(download_progress.progress() * 100))
         if done:
             print('Download Complete')
             return
예제 #25
0
    def download_file(self, file_id):
        """
        Download a file whose ID is fild_id
        :param file_id: File ID
        :return: Downloaded bytes
        """
        request = self.service.files().get_media(fileId=file_id)
        fh = io.BytesIO()
        downloader = http.MediaIoBaseDownload(fh, request)
        done = False
        while done is False:
            status, done = downloader.next_chunk()
            self._logger.info("Download %d%%" % int(status.progress() * 100))

        return fh.getvalue()
예제 #26
0
파일: test.py 프로젝트: tate11/WooGenerator
def download_media_csv(service, fildId, local_fd):
    request = service.files().get_media(fileId=fildId, acknowledgeAbuse=True)
    print(request), dir(request), dir(request.uri), request.uri
    media_request = http.MediaIoBaseDownload(local_fd, request)
    while True:
        try:
            download_progress, done = media_request.next_chunk()
        except errors.HttpError as error:
            print 'An error occurred: %s' % error
            return
        if download_progress:
            print 'Download Progress: %d%%' % int(
                download_progress.progress() * 100)
        if done:
            print 'Download Complete'
            return
예제 #27
0
def exportFile(DRIVE, fileid, mimetype, filename):
	'''
	Accepts a file ID, mimeType, and filename and attempts to export the
	file from Google Drive to the given mimeType.
	'''

	# Create a request for the file to export
	request = DRIVE.files().export_media(fileId=fileid, mimeType=mimetype)
	# Open a stream up to write bytes to the given filename
	fh = io.FileIO(filename, 'wb')
	# Create a downloader from the request to the file stream
	downloader = http.MediaIoBaseDownload(fh, request)
	done = False
	# Download chunks of the file until done
	while done is False:
		status, done = downloader.next_chunk()
예제 #28
0
def get_file(fileId, fileName, year, user):
    global FILES_DOWNLOADED
    filePath = BACKUP_DIR + "/" + user + "/" + str(year) + "/" + fileName
    if (not os.path.exists(filePath)):
        request = DRIVE_SERVICE.files().get_media(fileId=fileId)
        fh = io.FileIO(filePath + ".inprogress", mode='wb')
        downloader = http.MediaIoBaseDownload(fh, request)
        done = False
        while done is False:
            status, done = downloader.next_chunk()
            print("Download {0}% (file {1} of max {2})".format(
                int(status.progress() * 100), (FILES_DOWNLOADED + 1),
                MAX_FILES_TO_DOWNLOAD))
        fh.close()
        os.rename(filePath + ".inprogress", filePath)
        FILES_DOWNLOADED += 1
예제 #29
0
def download_file(service, file_id, local_fd):
    """Download a Drive file's content to the local filesystem.

  Args:
    service: Drive API Service instance.
    file_id: ID of the Drive file that will downloaded.
    local_fd: io.Base or file object, the stream that the Drive file's
        contents will be written to.
  """
    request = service.files().get_media(fileId=file_id)
    fh = io.BytesIO()
    downloader = http.MediaIoBaseDownload(local_fd, request)
    done = False
    while done is False:
        status, done = downloader.next_chunk()
        print("Download %d%%." % int(status.progress() * 100))
예제 #30
0
    def getFile(self, FolderName, FileName):
        retObj = ''
        fileId = self.getFileID(FolderName, FileName)
        fileMeta = self.getFileDetails(FolderName, FileName)
        fm = json.dumps(fileMeta)
        fd = json.loads(fm)
        fileSize = '0'

        if 'size' in fd:
            fileSize = fd['size']

            if fileSize != '0':

                try:
                    print('Download Starting.')
                    request = self.SERVICE.files().get_media(fileId=fileId)
                    fh = io.BytesIO()
                    downloader = http.MediaIoBaseDownload(fh, request)
                    done = False

                    while done is False:
                        status, done = downloader.next_chunk()
                        print("Download %d%%." % int(status.progress() * 100))

                    print('Download Finished.')

                    retObj = fh.getvalue()

                except urllib3.exceptions.HTTPError as error:
                    print('HTTP Error occured!')

                    if hasattr(error, 'code'):
                        print('Error Code:  ' + str(error.code))

                    if hasattr(error, 'reason'):
                        print('Error Code:  ' + str(error.reason))

                except Exception as error:
                    print("An error has occured!")
                    print(error)
                    exit(409)

            else:
                print('File ({0}) has a size of 0'.format(FileName))
                retObj = ''

        return retObj