di skrip ini, pydrive2 kecepatan download nya seperti terbatas 30-40MB/s, tidak seperti…
- User
di skrip ini, pydrive2 kecepatan download nya seperti terbatas 30-40MB/s, tidak seperti gdown, gdown jika mendownload file sangat cepat sesuai dengan kecepatan google colab, coba di cek mungkin ada pembatasan kecepatan sehingga sangat lambat pydrive2 waktu download.
from google.colab import auth from googleapiclient.discovery import build from googleapiclient.http import MediaIoBaseDownload from tqdm.notebook import tqdm import io import time
Authenticate and create the service client for Drive API
auth.authenticate_user() drive_service = build('drive', 'v3')
ID of the file you want to download
file_id = '1xw1LnqYFcVZvfPbZpoBbYHeq1moS6X5R' # @param {type:"string"}
Destination path where you want to save the file
dst_pth = '/content/TEST-661-UC.mp4' # @param {type:"string"}
Request the file metadata to get the file size
file_metadata = drive_service.files().get(fileId=file_id, fields='size').execute() file_size = int(file_metadata.get('size', 0))
Request the file
request = drive_service.files().get_media(fileId=file_id)
Create a file handle to write the file to
fh = io.FileIO(dst_pth, mode='wb')
Create a downloader object
downloader = MediaIoBaseDownload(fh, request)
Custom format for the progress bar
custom_tqdm_format = '{desc} |{bar}| {n_fmt}/{total_fmt} [{rate_noinv_fmt}{postfix}]'
def adjust_units(size_in_bytes): units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'] unit_index = 0 while size_in_bytes >= 1024 and unit_index < len(units) - 1: size_in_bytes /= 1024.0 unit_index += 1 return size_in_bytes, units[unit_index]
Create a progress bar object
pbar = tqdm(total=file_size, desc='Downloading', unit='B', unit_scale=True, unit_divisor=1024, bar_format=custom_tqdm_format)
Start the download process and update the progress bar
done = False start_time = time.time() while not done: status, done = downloader.next_chunk() elapsed_time = time.time() - start_time download_speed = (status.resumable_progress / elapsed_time) if elapsed_time > 0 else 0 pbar.set_postfix_str(f'{download_speed / (1024 * 1024):,.2f} MB/s, Elapsed: {elapsed_time:,.2f} s') pbar.update(status.resumable_progress - pbar.n) # Update progress
Close the file handle after downloading is complete
fh.close() pbar.close()
print(f'File downloaded to {dst_pth}')
- Assistant