Skip to content

Commit

Permalink
add helper to download ZIP files from Zenodo
Browse files Browse the repository at this point in the history
  • Loading branch information
psybers committed Dec 11, 2023
1 parent 0f7b2a6 commit e9e7aae
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ RUN set -e \
#
&& apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends --yes \
make=4.3-4.1 \
curl=7.74.0-1.3+deb11u10 \
#
# clean-up
#
Expand Down
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ VERBOSE:=
ZIP:=zip
ZIPOPTIONS:=-u -r
ZIPIGNORES:=-x \*.DS_Store\* -x \*.gitkeep\* -x data/csv/\*
ZENODO_DOWNLOAD=$(PYTHON) bin/zenodo-download.py

DOWNLOAD:=$(PYTHON) bin/download.py $(VERBOSE)
BOATOCSV:=$(PYTHON) bin/boa-to-csv.py
Expand Down Expand Up @@ -74,6 +75,16 @@ zenodo:
$(PYTHON) bin/zenodo.py


##########################
# downloading from Zenodo
#
get-data:
$(ZENODO_DOWNLOAD) data.zip

get-cache:
$(ZENODO_DOWNLOAD) data-cache.zip


################
# clean targets
#
Expand Down
60 changes: 60 additions & 0 deletions bin/zenodo-download.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env python3
# coding: utf-8

import json
import os
from pathlib import Path
import subprocess
import sys


JSON_FILENAME = '.zenodo.json'


def get_deposition_id():
if 'prereserve_doi' in json_data:
if isinstance(json_data['prereserve_doi'], dict) and 'recid' in json_data['prereserve_doi']:
return json_data['prereserve_doi']['recid']
return None


if __name__ == '__main__':
if len(sys.argv) != 2:
print('usage: zenodo-download.py <filename>')
sys.exit(-1)
filename = sys.argv[1]

# make sure we have a JSON metadata file
if not Path(JSON_FILENAME).exists():
print('no Zenodo JSON metadata file found')
sys.exit(-1)

with open(JSON_FILENAME) as f:
json_data = json.load(f)

# ensure there is a deposition
deposition_id = get_deposition_id()
if not deposition_id:
print('unable to find a Zenodo deposition')
sys.exit(-1)

# download the file
print('downloading file...')

try:
os.remove(filename)
except:
pass

url = f'https://zenodo.org/records/{deposition_id}/files/{filename}?download=1'
proc = subprocess.run(['curl', '-L', '--fail-with-body', '-o', filename, url])

if proc.returncode == 0:
print('download complete')
else:
print(f'error downloading file "{filename}" from "{url}"')
try:
os.remove(filename)
except:
pass
sys.exit(-1)

0 comments on commit e9e7aae

Please sign in to comment.