annotate gaiac_pm_data_pulling/gaiac_data_download.py @ 3:8d9833c09049 draft default tip

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