Skip to content

Commit 16e352c

Browse files
committed
Updating the function to get data from dataverse
1 parent ed18a68 commit 16e352c

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

hands-on/session II/1.Tutorial.ipynb

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -368,9 +368,25 @@
368368
"def get_data_from_dataverse(data):\n",
369369
" file_url = data.get(\"url\")\n",
370370
" name_file = data.get(\"name_file\")\n",
371-
" resp = requests.get(file_url)\n",
372-
" with open(name_file, \"wb\") as f:\n",
373-
" f.write(resp.content)\n",
371+
" if not file_url or not name_file:\n",
372+
" raise ValueError(\"Missing 'url' or 'name_file' in data\")\n",
373+
"\n",
374+
" path = Path(name_file)\n",
375+
"\n",
376+
" if path.exists():\n",
377+
" print(f\"skipping {name_file} (already exists)\\n\")\n",
378+
" return str(path)\n",
379+
"\n",
380+
" resp = requests.get(file_url, stream=True, timeout=60)\n",
381+
" resp.raise_for_status()\n",
382+
"\n",
383+
" with open(path, \"wb\") as f:\n",
384+
" print(f\"Downloading {name_file}\\n\")\n",
385+
" for chunk in resp.iter_content(chunk_size=1024 * 1024):\n",
386+
" if chunk:\n",
387+
" f.write(chunk)\n",
388+
"\n",
389+
" return str(path)\n",
374390
"\n",
375391
"\n",
376392
"print(\"Your download is starting...\")\n",
@@ -1133,7 +1149,7 @@
11331149
],
11341150
"metadata": {
11351151
"kernelspec": {
1136-
"display_name": "Python 3 (ipykernel)",
1152+
"display_name": "NSDF-Tutorial",
11371153
"language": "python",
11381154
"name": "python3"
11391155
},
@@ -1148,11 +1164,6 @@
11481164
"nbconvert_exporter": "python",
11491165
"pygments_lexer": "ipython3",
11501166
"version": "3.10.14"
1151-
},
1152-
"vscode": {
1153-
"interpreter": {
1154-
"hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
1155-
}
11561167
}
11571168
},
11581169
"nbformat": 4,

0 commit comments

Comments
 (0)