From e507fbfa81217961248cf7236f21c43f236784f7 Mon Sep 17 00:00:00 2001 From: Andrew Byrd Date: Fri, 19 Apr 2024 23:51:26 +0800 Subject: [PATCH] download 1MB chunks streaming save under human readable filename. report progress and display filename. use "with" syntax, raise errors, no file closing. --- batch-result-download.ipynb | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/batch-result-download.ipynb b/batch-result-download.ipynb index 25e2cdd..b95e951 100644 --- a/batch-result-download.ipynb +++ b/batch-result-download.ipynb @@ -85,13 +85,15 @@ " status = r.status_code\n", " \n", " if (status == 200):\n", - " zipRequest = requests.get(r.json()['url'], verify = False)\n", - "\n", - " # Save result to a local .zip file\n", - " with open('results/' + analysisId + '.zip', 'wb') as f:\n", - " for chunk in zipRequest.iter_content(chunk_size=128):\n", - " f.write(chunk)\n", - " f.close()" + " with requests.get(r.json()['url'], verify = False, stream=True) as zipRequest:\n", + " zipRequest.raise_for_status()\n", + " humanName = r.json()['humanName']\n", + " print('Saving results to local file: ' + humanName)\n", + " with open(humanName, 'wb') as f:\n", + " for (i, chunk) in enumerate(zipRequest.iter_content(chunk_size=1024*1024)):\n", + " print(i, \"MB\")\n", + " f.write(chunk)\n", + " print(\"Done.\")\n" ] } ],