| #!/usr/bin/env python3 | |
| """Download one network-tier source file with retries, record sha256 + receipt (raw/net/<ds>/<ver>/<name>.ok.json) and append to | |
| logs/net/downloads.jsonl. URL forms: | |
| https://... plain HTTP(S) (aria2c, 8 connections, resumable) | |
| uqrdm:<dataset-uuid> UQ RDM open-access dataset: resolves the short-lived S3 link via the public | |
| https://api.rdm.uq.edu.au/datasets/<uuid>/download endpoint (the same call the RDM web page makes; | |
| the signed link itself is never logged) | |
| s3:<bucket>/<key> public S3 object via `aws s3 cp --no-sign-request` | |
| Usage: netfetch.py <dataset> <version> <url> <name> --license "..." [--tries 3] [--page URL]""" | |
| import sys, os, json, time, hashlib, subprocess, argparse, datetime, urllib.request | |
| ROOT = "/workspace/security-dataset"; IST = datetime.timezone(datetime.timedelta(hours=5, minutes=30)) | |
| ap = argparse.ArgumentParser(); ap.add_argument("dataset"); ap.add_argument("version"); ap.add_argument("url"); ap.add_argument("name") | |
| ap.add_argument("--license", required=True); ap.add_argument("--tries", type=int, default=3); ap.add_argument("--page", default=None) | |
| ap.add_argument("--s3-region", default="ca-central-1") | |
| a = ap.parse_args() | |
| d = f"{ROOT}/raw/net/{a.dataset}/{a.version}"; os.makedirs(d, exist_ok=True); out = f"{d}/{a.name}" | |
| rec = dict(dataset=a.dataset, version=a.version, url=a.url, page=a.page, name=a.name, path=os.path.relpath(out, ROOT), license=a.license) | |
| def log(r): | |
| with open(f"{ROOT}/logs/net/downloads.jsonl", "a") as f: f.write(json.dumps(r) + "\n") | |
| def resolve(u): | |
| if u.startswith("uqrdm:"): | |
| meta = json.load(urllib.request.urlopen(f"https://api.rdm.uq.edu.au/datasets/{u[6:]}/download", timeout=60)) | |
| if meta.get("accessType") != "OPEN" or not meta.get("url"): raise RuntimeError(f"UQ RDM dataset not open/ready: accessType={meta.get('accessType')}") | |
| return meta["url"] | |
| return u | |
| err = None | |
| for t in range(1, a.tries + 1): | |
| try: | |
| if a.url.startswith("s3:"): | |
| subprocess.run(["aws", "s3", "cp", "--no-sign-request", "--region", a.s3_region, "--only-show-errors", "s3://" + a.url[3:], out], check=True) | |
| else: | |
| u = resolve(a.url) | |
| subprocess.run(["aria2c", "-q", "-x8", "-s8", "-k10M", "--continue=true", "--max-tries=5", "--retry-wait=10", "--file-allocation=none", | |
| "-d", d, "-o", a.name, u], check=True) | |
| err = None; break | |
| except Exception as e: | |
| err = f"attempt {t}: {type(e).__name__}: {str(e)[:300]}"; print(err, flush=True); time.sleep(20 * t) | |
| if err: | |
| rec.update(status="failed", error=f"{a.tries} attempts; last {err}", attempted_ist=datetime.datetime.now(IST).strftime("%Y-%m-%d %H:%M IST")); log(rec) | |
| print(json.dumps(rec)); sys.exit(1) | |
| h = hashlib.sha256() | |
| with open(out, "rb") as f: | |
| for b in iter(lambda: f.read(1 << 24), b""): h.update(b) | |
| rec.update(status="ok", size_bytes=os.path.getsize(out), sha256=h.hexdigest(), downloaded_ist=datetime.datetime.now(IST).strftime("%Y-%m-%d %H:%M IST")) | |
| json.dump(rec, open(out + ".ok.json", "w"), indent=1); log(rec); print(json.dumps(rec)) | |
Xet Storage Details
- Size:
- 3.2 kB
- Xet hash:
- 079843a2850a05d6e5311a2d3bfa89887b576b4401d26513704faf7f5d837957
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.