|
| 1 | +import shutil |
| 2 | + |
| 3 | +import requests |
| 4 | + |
| 5 | +from cumulus_lambda_functions.stage_in_out.download_granules_abstract import DownloadGranulesAbstract |
| 6 | +import json |
| 7 | +import logging |
| 8 | +import os |
| 9 | + |
| 10 | +LOGGER = logging.getLogger(__name__) |
| 11 | + |
| 12 | + |
| 13 | +class DownloadGranulesHttp(DownloadGranulesAbstract): |
| 14 | + |
| 15 | + def __init__(self) -> None: |
| 16 | + super().__init__() |
| 17 | + |
| 18 | + def __set_props_from_env(self): |
| 19 | + missing_keys = [k for k in [self.STAC_JSON, self.DOWNLOAD_DIR_KEY] if k not in os.environ] |
| 20 | + if len(missing_keys) > 0: |
| 21 | + raise ValueError(f'missing environment keys: {missing_keys}') |
| 22 | + self._retrieve_stac_json() |
| 23 | + self._setup_download_dir() |
| 24 | + return self |
| 25 | + |
| 26 | + def __get_downloading_urls(self, granules_result: list): |
| 27 | + if len(granules_result) < 1: |
| 28 | + LOGGER.warning(f'cannot find any granules') |
| 29 | + return [] |
| 30 | + downloading_urls = [k['assets'] for k in granules_result] |
| 31 | + return downloading_urls |
| 32 | + |
| 33 | + def __download_one_granule(self, assets: dict): |
| 34 | + """ |
| 35 | + sample assets |
| 36 | + { |
| 37 | + "data": { |
| 38 | + "href": "s3://am-uds-dev-cumulus-internal/ATMS_SCIENCE_Group___1/P1570515ATMSSCIENCEAAT16017044853900.PDS", |
| 39 | + "title": "P1570515ATMSSCIENCEAAT16017044853900.PDS", |
| 40 | + "description": "P1570515ATMSSCIENCEAAT16017044853900.PDS" |
| 41 | + }, |
| 42 | + "metadata__data": { |
| 43 | + "href": "s3://am-uds-dev-cumulus-internal/ATMS_SCIENCE_Group___1/P1570515ATMSSCIENCEAAT16017044853901.PDS", |
| 44 | + "title": "P1570515ATMSSCIENCEAAT16017044853901.PDS", |
| 45 | + "description": "P1570515ATMSSCIENCEAAT16017044853901.PDS" |
| 46 | + }, |
| 47 | + "metadata__xml": { |
| 48 | + "href": "s3://am-uds-dev-cumulus-internal/ATMS_SCIENCE_Group___1/P1570515ATMSSCIENCEAAT16017044853901.PDS.xml", |
| 49 | + "title": "P1570515ATMSSCIENCEAAT16017044853901.PDS.xml", |
| 50 | + "description": "P1570515ATMSSCIENCEAAT16017044853901.PDS.xml" |
| 51 | + }, |
| 52 | + "metadata__cmr": { |
| 53 | + "href": "s3://am-uds-dev-cumulus-internal/ATMS_SCIENCE_Group___1/P1570515ATMSSCIENCEAAT16017044853900.PDS.cmr.xml", |
| 54 | + "title": "P1570515ATMSSCIENCEAAT16017044853900.PDS.cmr.xml", |
| 55 | + "description": "P1570515ATMSSCIENCEAAT16017044853900.PDS.cmr.xml" |
| 56 | + } |
| 57 | + } |
| 58 | + :param assets: |
| 59 | + :return: |
| 60 | + """ |
| 61 | + error_log = [] |
| 62 | + local_item = {} |
| 63 | + for k, v in assets.items(): |
| 64 | + local_item[k] = v |
| 65 | + try: |
| 66 | + LOGGER.debug(f'downloading: {v["href"]}') |
| 67 | + downloading_response = requests.get(v['href']) |
| 68 | + downloading_response.raise_for_status() |
| 69 | + downloading_response.raw.decode_content = True |
| 70 | + local_file_path = os.path.join(self._download_dir, os.path.basename(v["href"])) |
| 71 | + with open(local_file_path, 'wb') as f: |
| 72 | + shutil.copyfileobj(downloading_response.raw, f) |
| 73 | + local_item[k]['href'] = local_file_path |
| 74 | + except Exception as e: |
| 75 | + LOGGER.exception(f'failed to download {v}') |
| 76 | + local_item[k]['description'] = f'download failed. {str(e)}' |
| 77 | + error_log.append(v) |
| 78 | + return local_item, error_log |
| 79 | + |
| 80 | + def download(self, **kwargs) -> list: |
| 81 | + self.__set_props_from_env() |
| 82 | + downloading_urls = self.__get_downloading_urls(self._granules_json) |
| 83 | + error_list = [] |
| 84 | + local_items = [] |
| 85 | + for each in downloading_urls: |
| 86 | + LOGGER.debug(f'working on {each}') |
| 87 | + local_item, current_error_list = self.__download_one_granule(each) |
| 88 | + local_items.append({'assets': local_item}) |
| 89 | + error_list.extend(current_error_list) |
| 90 | + if len(error_list) > 0: |
| 91 | + with open(f'{self._download_dir}/error.log', 'w') as error_file: |
| 92 | + error_file.write(json.dumps(error_list, indent=4)) |
| 93 | + return local_items |
0 commit comments