Python Google驱动API动态chunksize并设置最大下载速度

2024-09-23 22:22:36 发布

您现在位置:Python中文网/ 问答频道 /正文

从googledrive下载文件没有任何问题。我想设置最大下载速度(例如:256kbps)

我做了我想做的,但不能在downlod开始后改变chunksize。如果我能在你的帮助下解决这个问题,一切都会好起来的。 这是我的代码:

        http = credentials.authorize(httplib2.Http())
        drive_service = discovery.build('drive', 'v3', http=http, cache_discovery=False)
        request = drive_service.files().get_media(fileId=link)


        file = drive_service.files().get(fileId=link).execute()
        dosyadi = file['name']
        print(file['name'])
        self.PATH_DRIVE = self.PATH_DRIVE.replace("\n", "")
        yazilacakyer = self.PATH_DRIVE + '\\' + dosyadi
        fh = io.FileIO(yazilacakyer, 'wb')

        KB_PERSEC=1024 #default chunk size 1mb

        if speedlimit!=0:
            KB_PERSEC = speedlimit #if has speed limit on parameter, set chunksizes as speedlimit
        downloader = MediaIoBaseDownload(fh, request, chunksize=1024*KB_PERSEC)


        done = False




        while done is False:
            gecensure = time.time()
            status, done = downloader.next_chunk()
            gecensure=time.time()-gecensure #if downloading chunksize time lower than 1 second then wait 1 sec
            while self.indirilecek_indexler[structakiIndex].indirilecek_indirmeDevamEdiyor==False:
                time.sleep(5)

            self.indirilecek_indexler[structakiIndex].thr_strIndirilenMbToplamMb=self.humanbytes(status.resumable_progress) + ' / ' + self.humanbytes(status.total_size)


            self.indirilecek_indexler[structakiIndex].thr_yuzde=int(status.progress()*100)

            if fark < 1.1 and speedlimit != 0:
                self.indirilecek_indexler[structakiIndex].thr_lblDownloadSpeed = self.humanbytes(KB_PERSEC*1024)
            else:
                self.indirilecek_indexler[structakiIndex].thr_lblDownloadSpeed=self.humanbytes(int(KB_PERSEC/gecensure*KB_PERSEC))



            if speedlimit!=0:
                time.sleep(1)

Tags: selffalseifkbtimestatusdrivethr