Mercurial > repos > jay > gaiac_regression_plot
annotate gaiac_pm_data_pulling/datapulling.py @ 1:9d28091f26dd draft
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit a733fec1915a1f6cf5f07092dc33426f765b6cb3-dirty
author | jay |
---|---|
date | Thu, 15 May 2025 22:50:33 +0000 |
parents | |
children |
rev | line source |
---|---|
1
9d28091f26dd
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit a733fec1915a1f6cf5f07092dc33426f765b6cb3-dirty
jay
parents:
diff
changeset
|
1 import requests |
9d28091f26dd
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit a733fec1915a1f6cf5f07092dc33426f765b6cb3-dirty
jay
parents:
diff
changeset
|
2 import sys |
9d28091f26dd
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit a733fec1915a1f6cf5f07092dc33426f765b6cb3-dirty
jay
parents:
diff
changeset
|
3 |
9d28091f26dd
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit a733fec1915a1f6cf5f07092dc33426f765b6cb3-dirty
jay
parents:
diff
changeset
|
4 def download_files_from_server(urls): |
9d28091f26dd
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit a733fec1915a1f6cf5f07092dc33426f765b6cb3-dirty
jay
parents:
diff
changeset
|
5 """ |
9d28091f26dd
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit a733fec1915a1f6cf5f07092dc33426f765b6cb3-dirty
jay
parents:
diff
changeset
|
6 Function to download files from a FastAPI server. |
9d28091f26dd
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit a733fec1915a1f6cf5f07092dc33426f765b6cb3-dirty
jay
parents:
diff
changeset
|
7 |
9d28091f26dd
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit a733fec1915a1f6cf5f07092dc33426f765b6cb3-dirty
jay
parents:
diff
changeset
|
8 Args: |
9d28091f26dd
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit a733fec1915a1f6cf5f07092dc33426f765b6cb3-dirty
jay
parents:
diff
changeset
|
9 ip (str): The IP address of the FastAPI server. |
9d28091f26dd
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit a733fec1915a1f6cf5f07092dc33426f765b6cb3-dirty
jay
parents:
diff
changeset
|
10 file_names (list): List of file names to download. |
9d28091f26dd
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit a733fec1915a1f6cf5f07092dc33426f765b6cb3-dirty
jay
parents:
diff
changeset
|
11 |
9d28091f26dd
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit a733fec1915a1f6cf5f07092dc33426f765b6cb3-dirty
jay
parents:
diff
changeset
|
12 Returns: |
9d28091f26dd
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit a733fec1915a1f6cf5f07092dc33426f765b6cb3-dirty
jay
parents:
diff
changeset
|
13 None |
9d28091f26dd
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit a733fec1915a1f6cf5f07092dc33426f765b6cb3-dirty
jay
parents:
diff
changeset
|
14 """ |
9d28091f26dd
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit a733fec1915a1f6cf5f07092dc33426f765b6cb3-dirty
jay
parents:
diff
changeset
|
15 for url in urls: |
9d28091f26dd
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit a733fec1915a1f6cf5f07092dc33426f765b6cb3-dirty
jay
parents:
diff
changeset
|
16 file_name = url.split('/')[len(url.split('/'))-1] |
9d28091f26dd
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit a733fec1915a1f6cf5f07092dc33426f765b6cb3-dirty
jay
parents:
diff
changeset
|
17 response = requests.get(url) |
9d28091f26dd
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit a733fec1915a1f6cf5f07092dc33426f765b6cb3-dirty
jay
parents:
diff
changeset
|
18 if response.status_code == 200: |
9d28091f26dd
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit a733fec1915a1f6cf5f07092dc33426f765b6cb3-dirty
jay
parents:
diff
changeset
|
19 with open(f"{file_name}", "wb") as f: |
9d28091f26dd
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit a733fec1915a1f6cf5f07092dc33426f765b6cb3-dirty
jay
parents:
diff
changeset
|
20 f.write(response.content) |
9d28091f26dd
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit a733fec1915a1f6cf5f07092dc33426f765b6cb3-dirty
jay
parents:
diff
changeset
|
21 print(f"File {file_name} downloaded successfully!") |
9d28091f26dd
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit a733fec1915a1f6cf5f07092dc33426f765b6cb3-dirty
jay
parents:
diff
changeset
|
22 else: |
9d28091f26dd
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit a733fec1915a1f6cf5f07092dc33426f765b6cb3-dirty
jay
parents:
diff
changeset
|
23 print(f"Failed to download {file_name}. Status code: {response.status_code}") |
9d28091f26dd
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit a733fec1915a1f6cf5f07092dc33426f765b6cb3-dirty
jay
parents:
diff
changeset
|
24 |
9d28091f26dd
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit a733fec1915a1f6cf5f07092dc33426f765b6cb3-dirty
jay
parents:
diff
changeset
|
25 if __name__=="__main__": |
9d28091f26dd
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit a733fec1915a1f6cf5f07092dc33426f765b6cb3-dirty
jay
parents:
diff
changeset
|
26 |
9d28091f26dd
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit a733fec1915a1f6cf5f07092dc33426f765b6cb3-dirty
jay
parents:
diff
changeset
|
27 if len(sys.argv) > 1: |
9d28091f26dd
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit a733fec1915a1f6cf5f07092dc33426f765b6cb3-dirty
jay
parents:
diff
changeset
|
28 files = sys.argv[1].split(',') |
9d28091f26dd
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit a733fec1915a1f6cf5f07092dc33426f765b6cb3-dirty
jay
parents:
diff
changeset
|
29 download_files_from_server( files) |