|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import json |
| 4 | +import urllib.request |
| 5 | +import hashlib |
| 6 | +import sys |
| 7 | +import os |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | + |
| 11 | +def calculate_md5(filename): |
| 12 | + """Calculate MD5 hash of a file.""" |
| 13 | + md5_hash = hashlib.md5() |
| 14 | + with open(filename, "rb") as f: |
| 15 | + for chunk in iter(lambda: f.read(4096), b""): |
| 16 | + md5_hash.update(chunk) |
| 17 | + return md5_hash.hexdigest() |
| 18 | + |
| 19 | + |
| 20 | +def download_zenodo_files(output_dir: Path): |
| 21 | + """ |
| 22 | + Download all files from Zenodo record 14338424 and verify their checksums. |
| 23 | + |
| 24 | + Args: |
| 25 | + output_dir: Directory where files should be downloaded |
| 26 | + """ |
| 27 | + try: |
| 28 | + print("Fetching files from Zenodo record 14338424...") |
| 29 | + with urllib.request.urlopen("https://zenodo.org/api/records/14338424") as response: |
| 30 | + data = json.loads(response.read()) |
| 31 | + |
| 32 | + # Create output directory if it doesn't exist |
| 33 | + output_dir.mkdir(parents=True, exist_ok=True) |
| 34 | + |
| 35 | + for filename, file_info in data["files"]["entries"].items(): |
| 36 | + output_file = output_dir / filename |
| 37 | + print(f"Downloading {filename}...") |
| 38 | + url = file_info["links"]["content"] |
| 39 | + expected_md5 = file_info["checksum"].split(":")[1] |
| 40 | + |
| 41 | + # Download file |
| 42 | + urllib.request.urlretrieve(url, output_file) |
| 43 | + |
| 44 | + # Verify checksum |
| 45 | + actual_md5 = calculate_md5(output_file) |
| 46 | + if actual_md5 == expected_md5: |
| 47 | + print(f"✓ Verified {filename}") |
| 48 | + else: |
| 49 | + print(f"✗ Checksum verification failed for {filename}") |
| 50 | + print(f"Expected: {expected_md5}") |
| 51 | + print(f"Got: {actual_md5}") |
| 52 | + sys.exit(1) |
| 53 | + |
| 54 | + print("\nAll files downloaded and verified successfully!") |
| 55 | + |
| 56 | + except Exception as e: |
| 57 | + print(f"Error: {str(e)}", file=sys.stderr) |
| 58 | + sys.exit(1) |
| 59 | + |
| 60 | +if __name__ == "__main__": |
| 61 | + if len(sys.argv) != 2: |
| 62 | + print("Usage: download_zenodo.py <output_directory>") |
| 63 | + sys.exit(1) |
| 64 | + |
| 65 | + output_dir = Path(sys.argv[1]) |
| 66 | + download_zenodo_files(output_dir) |
0 commit comments