Skip to content

Commit

Permalink
Merge pull request #98 from nasa/HARMONY-1929
Browse files Browse the repository at this point in the history
Harmony 1929-2: Remove colon from downloaded file name
  • Loading branch information
vinnyinverso authored Dec 5, 2024
2 parents e8b774d + 26b814f commit d70e3a8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
9 changes: 6 additions & 3 deletions harmony/harmony.py
Original file line number Diff line number Diff line change
Expand Up @@ -1268,15 +1268,18 @@ def get_download_filename_from_url(self, url: str) -> str:
Returns:
The filename that will be used to name the downloaded file.
"""
name_result = None
url_no_query = parse.urlunparse(parse.urlparse(url)._replace(query=""))
url_parts = url_no_query.split('/')
original_filename = url_parts[-1]

is_staged_result = self._is_staged_result(url_no_query)
if not is_staged_result:
return original_filename
item_id = url_parts[-2]
return f'{item_id}_{original_filename}'
name_result = original_filename
else:
item_id = url_parts[-2]
name_result = f'{item_id}_{original_filename}'
return name_result.replace(':', '_')

def _download_file(self, url: str, directory: str = '', overwrite: bool = False) -> str:
"""Downloads data, saves it to a file, and returns the filename.
Expand Down
7 changes: 4 additions & 3 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1009,17 +1009,18 @@ def test_download_file(overwrite):

def test_download_opendap_file():
expected_data = bytes('abcde', encoding='utf-8')
expected_filename = 'SC:ATL03.006:264549068'
filename = 'SC:ATL03.006:264549068'
expected_filename = 'SC_ATL03.006_264549068'
query = '?dap4.ce=/ds_surf_type[0:1:4]'
path = 'https://opendap.uat.earthdata.nasa.gov/collections/C1261703111-EEDTEST/granules/'
url = path + expected_filename + query
url = path + filename + query
actual_output = None

with io.BytesIO() as file_obj:
file_obj.write(expected_data)
file_obj.seek(0)
with responses.RequestsMock() as resp_mock:
resp_mock.add(responses.POST, path + expected_filename, body=file_obj.read(), stream=True,
resp_mock.add(responses.POST, path + filename, body=file_obj.read(), stream=True,
match=[responses.matchers.urlencoded_params_matcher({"dap4.ce": "/ds_surf_type[0:1:4]"})])
client = Client(should_validate_auth=False)
actual_output = client._download_file(url, overwrite=False)
Expand Down

0 comments on commit d70e3a8

Please sign in to comment.