Last active
May 17, 2023 14:58
-
-
Save keflavich/46cbf66a4c5ba7a9d64753acdfeeb036 to your computer and use it in GitHub Desktop.
download ALMA-IMF data using dataverse API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests, os | |
# put your secret API key here: | |
api_key = <dataverse_API_key> | |
dataverse_server='https://dataverse.harvard.edu' | |
dataset_id = 6565108 | |
persisentId = 'doi:10.7910/DVN/CJ3YXU' | |
url_list = f'{dataverse_server}/api/datasets/{dataset_id}/versions/:draft/files?key={api_key}' | |
filelist_resp = requests.get(url_list) | |
file_metadata = filelist_resp.json() | |
# loop through *all files* and download them if they're not already downloaded | |
for fm in file_metadata['data']: | |
if not os.path.exists(fm['dataFile']['filename']): | |
resp = requests.get(f"{dataverse_server}/api/access/datafile/{fm['dataFile']['id']}", stream=True, | |
params={'key': api_key}, | |
auth=(api_key, ''), ) | |
resp.raise_for_status() | |
with open(fm['dataFile']['filename'], 'wb') as fh: | |
fh.write(resp.content) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment