From d650a226099d3c95eb4ffb0343af048492e83b82 Mon Sep 17 00:00:00 2001 From: sharevb Date: Wed, 29 Nov 2023 22:16:37 +0100 Subject: [PATCH] Storm prepare by chunk As of issue #76, PR #77 and to accomodate issue #64, thus case of "many small" or "really big" files, chunk big files in many blocks and blocks request also in chunks if more than 100 blocks need for the transfer of all files --- transferwee.py | 54 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/transferwee.py b/transferwee.py index 3d464f0..8833265 100755 --- a/transferwee.py +++ b/transferwee.py @@ -376,27 +376,39 @@ def _storm_prepare(authorization: str, filenames: List[str]) -> Dict[Any, Any]: Return the parsed JSON response. """ files_bids = [_storm_prepare_item(f) for f in filenames] - j = { - "blocks": [i for sublist in files_bids for i in sublist], - } - requests.options( - _storm_urls(authorization)["WETRANSFER_STORM_BLOCK"], - headers={ - "Origin": "https://wetransfer.com", - "Access-Control-Request-Method": "POST", - "User-Agent": WETRANSFER_USER_AGENT, - }, - ) - r = requests.post( - _storm_urls(authorization)["WETRANSFER_STORM_BLOCK"], - json=j, - headers={ - "Authorization": f"Bearer {authorization}", - "Origin": "https://wetransfer.com", - "User-Agent": WETRANSFER_USER_AGENT, - }, - ) - return {"files_bids": files_bids, "blocks": r.json()} + + blocks = [i for sublist in files_bids for i in sublist] + response = {"ok": True, "data": {"blocks": []}} + chunk_size = 100 + for i in range(0, len(blocks), chunk_size): + j = { + "blocks": blocks[i : i + chunk_size], + } + requests.options( + _storm_urls(authorization)["WETRANSFER_STORM_BLOCK"], + headers={ + "Origin": "https://wetransfer.com", + "Access-Control-Request-Method": "POST", + "User-Agent": WETRANSFER_USER_AGENT, + }, + ) + r = requests.post( + _storm_urls(authorization)["WETRANSFER_STORM_BLOCK"], + json=j, + headers={ + "Authorization": f"Bearer {authorization}", + "Origin": "https://wetransfer.com", + "User-Agent": WETRANSFER_USER_AGENT, + }, + ) + + r_json = r.json() + if not r_json["ok"]: + logger.error(r_json) + response["ok"] = response["ok"] and r_json["ok"] + response["data"]["blocks"] += r_json["data"]["blocks"] + + return {"files_bids": files_bids, "blocks": response} def _storm_finalize_item(