Skip to content

Instantly share code, notes, and snippets.

@keflavich
Last active May 17, 2023 14:58
Show Gist options
  • Save keflavich/46cbf66a4c5ba7a9d64753acdfeeb036 to your computer and use it in GitHub Desktop.
Save keflavich/46cbf66a4c5ba7a9d64753acdfeeb036 to your computer and use it in GitHub Desktop.
download ALMA-IMF data using dataverse API
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