From c37a656e69e3dfee141129e5fc064fefc50c385e Mon Sep 17 00:00:00 2001 From: James Bourbeau Date: Wed, 29 Nov 2023 16:01:38 -0600 Subject: [PATCH] Allow single file URL inputs for ``earthaccess.download`` (#347) Co-authored-by: Matt Fisher --- earthaccess/api.py | 6 ++++-- tests/integration/test_api.py | 7 ++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/earthaccess/api.py b/earthaccess/api.py index 843aba65..b2714312 100644 --- a/earthaccess/api.py +++ b/earthaccess/api.py @@ -166,7 +166,7 @@ def login(strategy: str = "all", persist: bool = False) -> Auth: def download( - granules: Union[DataGranule, List[DataGranule], List[str]], + granules: Union[DataGranule, List[DataGranule], str, List[str]], local_path: Union[str, None], provider: Optional[str] = None, threads: int = 8, @@ -177,7 +177,7 @@ def download( * If we run it outside AWS (us-west-2 region) and the dataset is cloud hostes we'll use HTTP links Parameters: - granules: a granule, list of granules, or a list of granule links (HTTP) + granules: a granule, list of granules, a granule link (HTTP), or a list of granule links (HTTP) local_path: local directory to store the remote data granules provider: if we download a list of URLs we need to specify the provider. threads: parallel number of threads to use to download the files, adjust as necessary, default = 8 @@ -188,6 +188,8 @@ def download( provider = _normalize_location(provider) if isinstance(granules, DataGranule): granules = [granules] + elif isinstance(granules, str): + granules = [granules] try: results = earthaccess.__store__.get(granules, local_path, provider, threads) except AttributeError as err: diff --git a/tests/integration/test_api.py b/tests/integration/test_api.py index bde37b05..6fa1ccea 100644 --- a/tests/integration/test_api.py +++ b/tests/integration/test_api.py @@ -69,13 +69,18 @@ def test_granules_search_returns_valid_results(kwargs): @pytest.mark.parametrize("selection", [0, slice(None)]) -def test_earthaccess_api_can_download_granules(tmp_path, selection): +@pytest.mark.parametrize("use_url", [True, False]) +def test_download(tmp_path, selection, use_url): results = earthaccess.search_data( count=2, short_name="ATL08", cloud_hosted=True, bounding_box=(-92.86, 16.26, -91.58, 16.97), ) + if use_url: + # Download via file URL string instead of DataGranule object + results = [r.data_links(access="indirect") for r in results] + results = sum(results, start=[]) # flatten to a list of strings result = results[selection] files = earthaccess.download(result, str(tmp_path)) assertions.assertIsInstance(files, list)