commit a149fe5d63a277c85a956e9e36734665a0b9f10c
parent 0e863240900676a332671c0fac260210b4975a0a
Author: archiveanon <>
Date: Sat, 28 Oct 2023 05:22:10 +0000
Use tqdm to display file upload progress
Diffstat:
1 file changed, 27 insertions(+), 8 deletions(-)
diff --git a/src/gofile/api.py b/src/gofile/api.py
@@ -11,7 +11,12 @@ import msgspec
# normally I'd prefer using standard modules but streaming POST data is really important
import requests
-from requests_toolbelt.multipart.encoder import MultipartEncoder
+from requests_toolbelt.multipart.encoder import MultipartEncoder, MultipartEncoderMonitor
+
+try:
+ import tqdm
+except ImportError:
+ tqdm = None
T = TypeVar("T")
@@ -112,13 +117,27 @@ def upload_single(
if folder_id:
post_data["folderId"] = folder_id
- multipart_data = MultipartEncoder(fields=post_data)
- upload_result = _gofile_api_post(
- f"https://{server}.gofile.io/uploadFile",
- data=multipart_data,
- headers={"Content-Type": multipart_data.content_type},
- type=GofileUploadResult,
- )
+ file_size = file.stat().st_size
+ with tqdm.tqdm(
+ desc=file.name,
+ total=file_size,
+ unit="B",
+ unit_scale=True,
+ unit_divisor=1024,
+ dynamic_ncols=True,
+ ) as progress:
+ multipart_data = MultipartEncoder(fields=post_data)
+ monitor = MultipartEncoderMonitor(
+ multipart_data,
+ lambda monitor: progress.update(monitor.bytes_read - progress.n),
+ )
+
+ upload_result = _gofile_api_post(
+ f"https://{server}.gofile.io/uploadFile",
+ data=monitor,
+ headers={"Content-Type": multipart_data.content_type},
+ type=GofileUploadResult,
+ )
return GofileUpload(file, upload_result)